有几个关于在 spacy 中获取名词块 的现有问题,这相对简单。
我感兴趣的是将依赖解析复制到句子中预先指定的 ngram 之上。就像在下面的示例中一样,从这个spacy talk 中,whereAlex Smith和East London被视为依赖项解析中的单个标记。
这可能是通过options您指定的参数
完成的"collapse_phrases" : True
https://spacy.io/api/top-level#options-dep的详细信息
创建可以在浏览器中打开的 svg 文件的示例
import spacy
from spacy import displacy
from pathlib import Path
nlp = spacy.load('en_core_web_sm', parse=True, tag=True, entity=True)
doc = nlp("Alex Smith was fatally stabbed in East London")
print(doc.ents)
options = {"color": "white", "collapse_phrases" : True, "bg": "#000000"}
svg = displacy.render(doc, style="dep", options=options)
output_path = Path("dependency_plot.svg")
output_path.open("w", encoding="utf-8").write(svg)