python - spaCy 共指解析 - 命名实体识别 (NER) 返回唯一实体 ID?
问题描述
也许我跳过了部分文档,但我试图确定的是标准 NER 工具集中每个实体的唯一 ID。例如:
import spacy
from spacy import displacy
import en_core_web_sm
nlp = en_core_web_sm.load()
text = "This is a text about Apple Inc based in San Fransisco. "\
"And here is some text about Samsung Corp. "\
"Now, here is some more text about Apple and its products for customers in Norway"
doc = nlp(text)
for ent in doc.ents:
print('ID:{}\t{}\t"{}"\t'.format(ent.label,ent.label_,ent.text,))
displacy.render(doc, jupyter=True, style='ent')
返回:
ID:381 ORG "Apple Inc" ID:382 GPE "San Fransisco" ID:381 ORG "Samsung Corp." ID:381 ORG "Apple" ID:382 GPE "Norway"
我一直在看,ent.ent_id
但ent.ent_id_
根据文档这些是不活动的。我也找不到任何东西ent.root
。
例如,在GCP NLP中,每个实体都返回一个 ⟨entity⟩number,使您能够识别文本中同一实体的多个实例。
这是一个关于⟨Apple Inc⟩1 的⟨text⟩2,位于⟨San Fransisco⟩4。这里有一些关于⟨Samsung Corp⟩6的⟨text⟩3。现在,这里有更多关于 ⟨Apple⟩1 及其 ⟨products⟩5 的 ⟨text⟩8 供⟨挪威⟩9的⟨customers⟩7使用
spaCy 是否支持类似的东西?或者有没有办法使用 NLTK 或斯坦福?
解决方案
您可以使用神经核函数库来获得与 SpaCy 模型一起使用的共指解析:
# Load your usual SpaCy model (one of SpaCy English models)
import spacy
nlp = spacy.load('en')
# Add neural coref to SpaCy's pipe
import neuralcoref
neuralcoref.add_to_pipe(nlp)
# You're done. You can now use NeuralCoref as you usually manipulate a SpaCy document annotations.
doc = nlp(u'My sister has a dog. She loves him.')
doc._.has_coref
doc._.coref_clusters
在此处找到安装和使用说明:https ://github.com/huggingface/neuralcoref
推荐阅读
- sql - 由于字段的时间戳超出标准 SQL 时间戳的范围,无法在 Google BigQuery 中删除或更新行
- java - 严重:异常启动过滤器 ShiroFilter 错误
- android - VFY:register1 v4 type 2,通缉 17
- c - 这是 Vernam Cipher 的 C 代码,如何将数字作为输入消息而不是字符串?
- c++ - 如何在类派生层次结构的所有步骤中调用方法?
- javascript - 使用jquery为每列的textarea添加值
- html - css flex 将可滚动的 div 设置为父级的 100%
- python - 如何确定在顺序模型中添加的层数以使用 Tensorflow 解决多元线性回归问题?
- mysql - 如果 select(*) 计数值为 2,如何更改此查询以返回 true?
- typescript - 如何在 TypeScript 中保存枚举值?