This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
forked from uma-pi1/minie
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial bindings for MinIE and AnnotatedProposition
- Loading branch information
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
from .miniepy import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pyjnius>=1.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
) | ||
|