รองรับห้องสมุด Corenlp ล่าสุด 4.5.3 (ณ ปี 2023-03-10)
การสกัดข้อมูลแบบเปิด (Open IE) หมายถึงการสกัดของความสัมพันธ์ที่มีโครงสร้างสามเท่าจากข้อความธรรมดาเช่นสคีมาสำหรับความสัมพันธ์เหล่านี้ไม่จำเป็นต้องระบุล่วงหน้า ตัวอย่างเช่นบารัคโอบามาเกิดที่ฮาวายจะสร้างสาม (Barack Obama; was born in; Hawaii) Corenlp เป็นการใช้งาน Java ของระบบ IE แบบเปิดตามที่อธิบายไว้ในกระดาษ:
ข้อมูลเพิ่มเติมสามารถพบได้ที่นี่ ห้องสมุด Openie มีให้บริการเป็นภาษาอังกฤษเท่านั้น
คุณต้องติดตั้ง Python3 และ Java (JRE) Java ใช้โดย Corenlp Library
ตรวจสอบให้แน่ใจว่ามีการติดตั้ง Java ก่อน
java -versionจากนั้นติดตั้ง lib
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'}
มันจะสร้าง graphviz dot ใน graph.png :

หมายเหตุ : ตรวจสอบให้แน่ใจว่ามีการติดตั้ง graphviz ล่วงหน้า ลองเรียกใช้คำสั่ง dot เพื่อดูว่าเป็นกรณีนี้หรือไม่ ถ้าไม่เรียกใช้ sudo apt-get install graphviz หากคุณกำลังทำงานบน Ubuntu บน 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}},
}