有没有一种方法可以在spaCy中使用根标记来检索整个名词块?

2024-10-06 12:21:04 发布

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

我对使用spaCy很陌生。我已经看了好几个小时的文档了,我仍然不知道是否有可能做到我的问题。不管怎样。。。在

正如标题所说,有没有一种方法可以使用包含它的标记来实际获取给定的名词块。例如,给定一个句子:

"Autonomous cars shift insurance liability toward manufacturers"

当我只有"cars"标记时,有可能得到"autonomous cars"名词块吗?下面是一个我尝试使用的场景片段示例。在

^{pr2}$

任何帮助将不胜感激。谢谢!在


Tags: 方法文档标记标题shiftspacycars句子
1条回答
网友
1楼 · 发布于 2024-10-06 12:21:04

通过检查标记是否位于某个名词块跨度中,可以轻松找到包含所标识标记的名词块:

doc = nlp("Autonomous cars and magic wands shift insurance liability toward manufacturers")
interesting_token = doc[7] # or however you identify the token you want
for noun_chunk in doc.noun_chunks:
    if interesting_token in noun_chunk:
        print(noun_chunk)

en_core_web_sm和spacy 2.0.18的输出不正确,因为shift没有被标识为动词,因此可以得到:

magic wands shift insurance liability

使用en_core_web_md,它是正确的:

insurance liability

(在文档中包含具有真实歧义的示例是有意义的,因为这是一个现实的场景(https://spacy.io/usage/linguistic-features#noun-chunks),但是如果新用户的分析过于模糊,以致于跨版本/模型的分析不稳定,则会让新用户感到困惑。)

相关问题 更多 >