python中的主客体识别

2024-06-01 03:28:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想确定一组句子的主语和宾语。我的实际工作是从一组回顾数据中找出因果关系。在

我使用Spacy包来分块和解析数据。但实际上没有达到我的目标。有办法吗?在

例如:

 I thought it was the complete set

输出:

^{pr2}$

Tags: the数据目标spacyit分块句子complete
2条回答

以最简单的方式。 依赖项由令牌.dep_ 有输入空间:

import spacy
nlp = spacy.load('en')
parsed_text = nlp(u"I thought it was the complete set")

#get token dependencies
for text in parsed_text:
    #subject would be
    if text.dep_ == "nsubj":
        subject = text.orth_
    #iobj for indirect object
    if text.dep_ == "iobj":
        indirect_object = text.orth_
    #dobj for direct object
    if text.dep_ == "dobj":
        direct_object = text.orth_

print(subject)
print(direct_object)
print(indirect_object)

你可以用名词块。在

代码

doc = nlp("I thought it was the complete set")
for nc in doc.noun_chunks:
    print(nc.text)

结果:

^{pr2}$

要只选择“I”而不是“I”和“it”,可以先编写一个测试,将nsubj放在ROOT的左边。在

相关问题 更多 >