首页 > 解决方案 > 在高级案例中使用 spacy 识别句子中的主题

问题描述

我正在尝试识别句子中的主题。我尝试在这里使用一些代码

import spacy
nlp = nlp = spacy.load("en_core_web_sm")
sent = "the python can be used to find objects."
#sent = "The bears in the forest, which has tall trees, are very scary"
doc=nlp(sent)

sentence = next(doc.sents) 

for word in sentence:
    print(word,word.dep_)

这将返回结果:

我认为在这种情况下,python 将成为主题,在大多数情况下,这_dep将是nsubj,但它的nsubjpass. 因此,如果nsubj不存在,我可以检查,nsubjpass但还有其他_dep可能吗?

有没有更稳健的方法来确定主题?

标签: pythonnlpspacy

解决方案


你的句子是被动语态的例子。nsubjpass是使用被动语态时的主语

dep_您可以通过调用找到列表

for label in nlp.get_pipe("parser").labels:
    print(label, " -- ", spacy.explain(label))

我可以看到还有 2 种主题类型:

csubj  --  clausal subject
csubjpass  --  clausal subject (passive)

确定主题的一种可能方法:

if "subj" in word.dep_:
    # continue

推荐阅读