请我尝试做一个从一个解析句子的pdf文本串的关系提取。我使用了斯坦福大学的coreNLP和python pycorenlp来解析句子,现在我想从这个解析树中提取主语、动词和宾语。
下面是我的数据样本:“马克·罗伯特是3trucks的创始人。3trucks成立于2010年。”
以下是我想要的输出:('Mark Robert',创始人,'3trucks') ('3truck',成立于'2010')
以下是文本和代码的示例
import nltk
import re
from pycorenlp import *
nlp = StanfordCoreNLP("http://localhost:9000/")
text = 'Mark Robert is the founder of 3trucks. 3trucks was founded in 2010'
output = nlp.annotate(text, properties={
'annotators': 'tokenize,ssplit,pos,depparse,parse',
"timeout": "50000",
'outputFormat': 'json'
})
print(output['sentences'][0]['parse'])
print('------------------------------')
print(output['sentences'][1]['parse'])`我的代码输出:
(ROOT
(S
(NP (NNP Mark) (NNP Robert))
(VP (VBZ is)
(NP
(NP (DT the) (NN founder))
(PP (IN of)
(NP (NNS 3trucks)))))
(. .)))
------------------------------
(ROOT
(S
(NP (NNS 3trucks))
(VP (VBD was)
(VP (VBN founded)
(PP (IN in)
(NP (CD 2010)))))))发布于 2019-02-12 17:31:24
您可以在注释器列表中包含'openie‘。Openie还将形成三元组,这是列表所必需的。还要记住将输出限制为3。
output = nlp.annotate(s, properties={"annotators":"tokenize,ssplit,pos,depparse,natlog,openie",
"outputFormat": "json",
"openie.triple.strict":"true",
"openie.max_entailments_per_clause":"1"})Post,您可以根据需要添加输出。
result = [output["sentences"][0]["openie"] for item in output]
for i in result:
for rel in i:
relationSent=rel['subject'],rel['relation'],rel['object']
print(relationset)希望这能有所帮助。
https://stackoverflow.com/questions/52561555
复制相似问题