python - 在高级案例中使用 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 nsubjpass
- 可以辅助
- 辅助通行证
- 用过ROOT
- 辅助
- 找到 xcomp
- 对象 dobj
我认为在这种情况下,python 将成为主题,在大多数情况下,这_dep
将是nsubj
,但它的nsubjpass
. 因此,如果nsubj
不存在,我可以检查,nsubjpass
但还有其他_dep
可能吗?
有没有更稳健的方法来确定主题?
解决方案
你的句子是被动语态的例子。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
推荐阅读
- c - 将 char 数组从消息队列转换为 int 数组
- mysql - 带外键的 SQL 查询(缺失数据,返回默认值)
- inheritance - 与默认方法和字段的接口
- laravel - dockerizing Laravel 应用程序的问题 - 数据库主机数组为空
- python - 如何在python中从目标数据跟踪到源数据?
- eclipse - 在 Tomcat 上运行 Vaadin (maven) 示例项目的问题
- apache-kafka - 通过在 ksqlDB 中翻转窗口生成零计数的可能方法
- r - R,从图中推断平均分数
- javascript - 在 WEBGL 片段着色器中确定屏幕的中心
- python - NGINX 单元,Fastapi:在可用的应用程序模块中找不到运行“python 3.8”的模块