我正在试着识别句子中的主语。我尝试在这里使用一些代码:
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 nsubjpass
- 可以辅助
- 是auxpass 使用根对辅助
- 找到xcomp
- 对象dobj
我认为在这种情况下,蟒蛇将是主题,在大多数情况下,这将是_dep
将是nsubj
,但它的nsubjpass
。因此,如果nsubj
不存在,我可以检查nsubjpass
,但是否有任何其他_dep
可能是?
是否有更可靠的方法来确定主题?
你的句子是被动语态的例子。当使用被动语态
时,nsubjpass
是主语。可以通过调用
找到dep_
的列表。for label in nlp.get_pipe("parser").labels:
print(label, " -- ", spacy.explain(label))
我可以看到还有两个主题类型:
csubj -- clausal subject
csubjpass -- clausal subject (passive)
确定主题的一种可能方法:
if "subj" in word.dep_:
# continue