如何根据与 SpaCy 的依赖关系获取部分子树?

问题描述 投票:0回答:0

我用SpaCy解析了一些文本的依赖关系。在提取给定标记/跨度的子树时,如何施加与这些依赖关系相关的条件?

例如,我想获取给定标记的子树,但排除子树的所有部分,其中我的原始标记的直接子代与该标记具有连词(“conj”)依赖关系。

举一个更具体的例子:我想从下面的句子中提取名称和相应的属性:

"The entrepreneur and philanthropist Bill Gates and the Apple's Steve Jobs ate hamburgers."

属性
比尔·盖茨 企业家和慈善家
史蒂夫·乔布斯 苹果的

依赖关系如下所示:

以下代码成功提取了人物实体,但比尔盖茨的子树与史蒂夫乔布斯的子树重叠:

import spacy
nlp = spacy.load("en_core_web_trf")

s = "The entrepreneur and philanthropist Bill Gates and the Apple's Steve Jobs ate hamburgers."
doc = nlp(s)

persons = [ent for ent in doc.ents if ent.label_ == "PERSON"]
# [Bill Gates, Steve Jobs]

[[token for token in p.subtree] for p in persons]
# [[The, entrepreneur, and, philanthropist, Bill, Gates, and, the, Apple, 's, Steve, Jobs], [the, Apple, 's, Steve, Jobs]]

所以我想要么只获取比尔盖茨子树中第一个孩子具有

nmod
依赖关系的部分,要么删除那些与具有
conj
依赖关系的第一个孩子相连的部分。在 R 中,包 rsyntax 可以完成工作,所以我假设 SpaCy 中已经内置了类似的东西。

(任何关于更聪明的方法来获得上表的技巧也很受欢迎——我一般来说对 SpaCy 和 Python 都不太精通)

python graph-theory spacy
© www.soinside.com 2019 - 2024. All rights reserved.