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

Commit

Permalink
Added Demo.py and information in readme about the repository and how …
Browse files Browse the repository at this point in the history
…to run minie from within python
  • Loading branch information
mmxgn committed Sep 15, 2018
1 parent 51d0c8f commit 4cc2a08
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
<img src="https://gkiril.github.io/minie/images/minie_logo.png" align="right" width="150" />

# MinIEpy - Python wrapper for MinIE Open Information Extraction system

I did this fork because I wanted to be able to use MinIE from within python. I made some changes to the original java source code because pyjnius has some bugs regarding accessing java enum types. The main problem was that I could not access the MinIE.Mode enumerator and I had to swap the values with integer values where I needed an interface with python. *DISCLAIMER*: I know little Java, if someone can help me there I would be appreciated.

## Installation

First compile MinIE and package everything to a single `.jar`:

```
$ mvn clean compile
...
$ mvn assembly:assembly -DdescriptorId=jar-with-dependencies
...
```

Secondly, install `pyjnius`. An example for local pip3 installation:
```
$ pip3 install pyjnius --user
```

## Testing
Change to `src/main/python/tests/minie/`:

```
$ cd src/main/python/tests/minie/
```

Run `Demo.py`

```
$ python3 Demo.py
```

Note that this assumes that you have environment variable `$JAVA_HOME` set before running it. If you don't either add `export JAVA_HOME=/path/to/your/jvm` in your `.bashrc` or edit `Demo.py` and uncomment (line 19):

```
os.environ['JAVA_HOME'] = '/usr/lib/jvm/default'
```
## Using MinIE with your python scripts

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).

# MinIE - Open Information Extraction system

An Open Information Extraction system, providing useful extractions:
Expand Down
59 changes: 59 additions & 0 deletions src/main/python/tests/minie/Demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 15:08:18 2018
@author: Emmanouil Theofanis Chourdakis <e.t.chourdakis@qmul.ac.uk>
This re-implements src/main/java/tests/minie/Demo.java with python
in order to showcase how minie can be used with python.
"""

# Change CLASSPATH to point to the minie jar archive,
import os
os.environ['CLASSPATH'] = "../../../../../target/minie-0.0.1-SNAPSHOT.jar"

# Uncomment to point to your java home (an example is given for arch linux)
# if you don't have it as an environment variable already.
# os.environ['JAVA_HOME'] = '/usr/lib/jvm/default'

# Import java classes in python with pyjnius' autoclass (might take some time)
from jnius import autoclass

CoreNLPUtils = autoclass('de.uni_mannheim.utils.coreNLP.CoreNLPUtils')
AnnotatedProposition = autoclass('de.uni_mannheim.minie.annotation.AnnotatedProposition')
MinIE = autoclass('de.uni_mannheim.minie.MinIE')
StanfordCoreNLP = autoclass('edu.stanford.nlp.pipeline.StanfordCoreNLP')
String = autoclass('java.lang.String')

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

# Input sentence
sentence = "The Joker believes that the hero Batman was not actually born in foggy Gotham City."

# Generate the extractions (With SAFE mode (mode = 2))
# NOTE: sentence must be wrapped into String, else it won't work.
minie = MinIE(String(sentence), parser, 2)

# Print out the extrations
print("Input sentence:", sentence)
print("=============================")
print("Extractions:")

# getPropositions() below returns an ObjectArrayList. Its elements can be accessed
# as a python list by the .elements() method
for ap in minie.getPropositions().elements():

# Some elements might by null so we don't process them.
if ap is not None:
print("\tTriple:", ap.getTripleAsString())
print("\tFactuality:", ap.getFactualityAsString())
if ap.getAttribution().getAttributionPhrase() is not None:
print("\tAttribution:", ap.getAttribution().toStringCompact());
else:
print("\tAttribution: NONE")
print("\t----------");

print("DONE!")

0 comments on commit 4cc2a08

Please sign in to comment.