Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Added initial bindings for MinIE and AnnotatedProposition
Browse files Browse the repository at this point in the history
  • Loading branch information
mmxgn committed Sep 15, 2018
1 parent 4cc2a08 commit b07f716
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@ os.environ['JAVA_HOME'] = '/usr/lib/jvm/default'
You can view `src/main/python/tests/minie/Demo.py` for an example to
how to get extracted triples. I am planning on implementing a python package that provides a sufficiently good wrapper. For the moment, make sure that your `os.environ['CLASSPATH']` variable points to the minie jar file relative to where you will run your script from (or even better provide an absolute path).

## Python bindings (Experimental and incomplete)

To install the python bindings switch to `src/main/python/`:

`$ cd src/main/python`

And run:

`$ python3 setup.py build`

And install:
`$ python3 setup.py install -u`

Then you can write a script like the following:

```
from miniepy import *
jar_file = "path/to/minie.jar"
# Instantiate minie
minie = MinIE(jar_file)
# Sentence to extract triples from
sentence = "The Joker believes that the hero Batman was not actually born in foggy Gotham City."
# Get proposition triples
triples = [p.triple for p in minie.get_propositions(sentence)]
print("Original sentence:")
print('\t{}'.format(sentence))
print("Extracted triples:")
for t in triples:
print("\t{}".format(t))
```

*NOTE:* Bindings are incomplete, I will be adding functionality when I need it.


# MinIE - Open Information Extraction system

An Open Information Extraction system, providing useful extractions:
Expand Down
4 changes: 4 additions & 0 deletions src/main/python/miniepy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from .miniepy import *
99 changes: 99 additions & 0 deletions src/main/python/miniepy/miniepy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 16:05:31 2018
@author: Emmanouil Theofanis Chourdakis <e.t.chourdakis@qmul.ac.uk>
A python wrapper for MinIE
"""

import os
from jnius import autoclass

class AnnotatedProposition:
def __init__(self, prop):
self.java_obj = prop

def __str__(self):
return self.java_obj.toString()

@property
def subject(self):
return self.java_obj.triple.get(0).toString()

@property
def object(self):
return self.java_obj.triple.get(2).toString()

@property
def relation(self):
return self.java_obj.triple.get(1).toString()

@property
def triple(self):
return tuple([self.java_obj.triple.get(n).toString() \
for n in range(self.java_obj.triple.size())])

class MinIE:
def __init__(self,
jar_file = None,
java_home = None,
):
if java_home:
os.environ['JAVA_HOME'] = java_home

if jar_file:
if 'CLASSPATH' in os.environ:
os.environ['CLASSPATH'] = "{}:{}".format(jar_file,
os.environ['CLASSPATH'])
else:
os.environ['CLASSPATH'] = jar_file

CoreNLPUtils = autoclass('de.uni_mannheim.utils.coreNLP.CoreNLPUtils')

# Dependency parsing pipeline initialization
self.parser = CoreNLPUtils.StanfordDepNNParser()

def get_propositions(self,sentence, mode = 'SAFE'):
""" returns a list of proposition extracted from sentence """

String = autoclass('java.lang.String')


if mode == 'AGGRESSIVE':
nmode = 0
elif mode == 'DICTIONARY':
nmode = 1
elif mode == 'SAFE':
nmode = 2
elif mode == 'COMPLETE':
nmode = 3

MinIE = autoclass('de.uni_mannheim.minie.MinIE')

self.minie_obj = MinIE(String(sentence), self.parser, nmode)

propositions = [
AnnotatedProposition(prop) for prop in self.minie_obj.getPropositions().elements()
if prop is not None ]

return propositions


if __name__ == "__main__":
sentence = 'The Joker believes that the hero Batman was not actually born in foggy Gotham City.'
print("Original sentence:")
print('\t{}'.format(sentence))
# Extract triples

# Get MinIE instance
minie = MinIE(jar_file='../../../../target/minie-0.0.1-SNAPSHOT.jar')

# Get proposition triples
triples = [p.triple for p in minie.get_propositions(sentence)]

# Print them
print("Extracted triples:")
for t in triples:
print("\t{}".format(t))
1 change: 1 addition & 0 deletions src/main/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyjnius>=1.1.1
19 changes: 19 additions & 0 deletions src/main/python/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 16:54:08 2018
@author: Emmanouil Theofanis Chourdakis
"""

from distutils.core import setup

setup(name='miniepy',
version='0.0.1',
description='Python wrappers for the MinIE information extraction system',
author='Emmanouil Theofanis Chourdakis',
author_email='e.t.chourdakis@qmul.ac.uk',
url='https://github.com/mmxgn/miniepy',
packages=['miniepy'],
)

0 comments on commit b07f716

Please sign in to comment.