stanford openie python
1.0.0
支持最新的Corenlp庫4.5.3(截至2023-03-10)。
開放信息提取(打開IE)是指從純文本中提取結構化關係三元,因此這些關係的模式無需提前指定。例如,巴拉克·奧巴馬(Barack Obama)出生於夏威夷,將創建一個三重(Barack Obama; was born in; Hawaii) ,對應於“出生於”的開放式領域關係。 Corenlp是本文所述的開放IE系統的Java實現:
更多信息可以在此處找到。 Openie庫僅提供英文。
您需要安裝Python3和Java(JRE)。 Java由Corenlp庫使用。
確保首先安裝Java。
java -version然後安裝自由。
pip install stanford_openie 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 ( '[...]' )預期輸出
|- {'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'}
它將在graph.png中生成一個graphViz點:

注意:確保事先安裝了GraphViz。嘗試運行dot命令以查看是否是這種情況。如果沒有,如果您在Ubuntu上運行,請運行sudo apt-get install graphviz 。在MacOS上,它是brew install graphviz 。
@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}},
}