Skip to main content

Minimalist wrapper around Stanford OpenIE

Project description

Python3 wrapper for Stanford OpenIE

Stanford NLP Wrapper CI

Open information extraction (open IE) refers to the extraction of structured relation triples from plain text, such that the schema for these relations does not need to be specified in advance. For example, Barack Obama was born in Hawaii would create a triple (Barack Obama; was born in; Hawaii), corresponding to the open domain relation "was born in". CoreNLP is a Java implementation of an open IE system as described in the paper:

More information can be found here : http://nlp.stanford.edu/software/openie.html

The OpenIE library is only available in english: https://stanfordnlp.github.io/CoreNLP/human-languages.html

Installation

You need python3 and Java installed. Java is used by the CoreNLP library.

pip install stanford_openie

Example

from openie import StanfordOpenIE

# https://stanfordnlp.github.io/CoreNLP/openie.html#api
# Default value of openie.affinity_probability_cap was 1/3.
properties = {
    'openie.affinity_probability_cap': 2 / 3,
}

with StanfordOpenIE(properties=properties) as client:
    text = 'Barack Obama was born in Hawaii. Richard Manning wrote this sentence.'
    print('Text: %s.' % text)
    for triple in client.annotate(text):
        print('|-', triple)

    graph_image = 'graph.png'
    client.generate_graphviz_graph(text, graph_image)
    print('Graph generated: %s.' % graph_image)

    with open('corpus/pg6130.txt', encoding='utf8') as r:
        corpus = r.read().replace('\n', ' ').replace('\r', '')

    triples_corpus = client.annotate(corpus[0:5000])
    print('Corpus: %s [...].' % corpus[0:80])
    print('Found %s triples in the corpus.' % len(triples_corpus))
    for triple in triples_corpus[:3]:
        print('|-', triple)
    print('[...]')

Expected output

|- {'subject': 'Barack Obama', 'relation': 'was', 'object': 'born'}
|- {'subject': 'Barack Obama', 'relation': 'was born in', 'object': 'Hawaii'}
|- {'subject': 'Richard Manning', 'relation': 'wrote', 'object': 'sentence'}
Graph generated: graph.png.
Corpus: According to this document, the city of Cumae in Ćolia, was, at an early period [...].
Found 1664 triples in the corpus.
|- {'subject': 'city', 'relation': 'is in', 'object': 'Ćolia'}
|- {'subject': 'Menapolus', 'relation': 'son of', 'object': 'Ithagenes'}
|- {'subject': 'Menapolus', 'relation': 'was Among', 'object': 'immigrants'}

It will generate a GraphViz DOT in graph.png:



Note: Make sure GraphViz is installed beforehand. Try to run the dot command to see if this is the case. If not, run sudo apt-get install graphviz if you're running on Ubuntu.

V1

Still available here v1.

References

Cite

@misc{StanfordOpenIEWrapper,
  author = {Philippe Remy},
  title = {Python wrapper for Stanford OpenIE},
  year = {2020},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/philipperemy/Stanford-OpenIE-Python}},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stanford-openie-1.3.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

stanford_openie-1.3.0-py3.8.egg (7.4 kB view details)

Uploaded Egg

stanford_openie-1.3.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file stanford-openie-1.3.0.tar.gz.

File metadata

  • Download URL: stanford-openie-1.3.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.0

File hashes

Hashes for stanford-openie-1.3.0.tar.gz
Algorithm Hash digest
SHA256 dafa9efd0a8fa5669ba8bb82da8f677c29ecda88a3090169e14a4b04fb01b7aa
MD5 013d8dfd7f2f9c0e0be97714e9b5943b
BLAKE2b-256 ef7bbf7d89b4c41b733eb08c8d14fe8e9a0b8929269780519646a4a7d874ab81

See more details on using hashes here.

File details

Details for the file stanford_openie-1.3.0-py3.8.egg.

File metadata

  • Download URL: stanford_openie-1.3.0-py3.8.egg
  • Upload date:
  • Size: 7.4 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.0

File hashes

Hashes for stanford_openie-1.3.0-py3.8.egg
Algorithm Hash digest
SHA256 f2875f04d3d3c8faebfa6170fdf85cc01eec33e1265695249a6d0f1226cd22eb
MD5 87a3e812caac6fdf3387be2bc1ebe9bc
BLAKE2b-256 a054c06bd89efb445e9be34da06c8b6d7c34513c1c60dd44c662ba7852b2c3e2

See more details on using hashes here.

File details

Details for the file stanford_openie-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: stanford_openie-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.0

File hashes

Hashes for stanford_openie-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06377a4d212ed9d1a6865fe153a713663e95ad5bd7b4ad42a2b83ed3ff66aec6
MD5 1ec2f0e0281187ea232707b208a3eee4
BLAKE2b-256 098e207fd905459c7e476cd67e3d5c86b3cd9939d8daf96cefc27e684db837ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page