spaCy 共指解析 - 命名实体识别 (NER) 返回唯一实体 ID?

Ben*_*enP 5 python nlp information-extraction spacy ner

也许我跳过了部分文档,但我要确定的是标准 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')
Run Code Online (Sandbox Code Playgroud)

返回:

ID:381    ORG "Apple Inc" 
ID:382    GPE "San Fransisco" 
ID:381    ORG "Samsung Corp." 
ID:381    ORG "Apple" 
ID:382    GPE "Norway"
Run Code Online (Sandbox Code Playgroud)

我一直在寻找ent.ent_ident.ent_id_,但这些都是不活动根据文档。我也找不到任何东西ent.root

例如,在GCP NLP 中,每个实体都返回一个实体编号,使您能够识别文本中同一实体的多个实例。

这是一篇关于“Apple Inc”的“文字”2?1 位于“旧金山”?4。这是一些关于?三星公司?6 的“文本”3。现在,这里有更多关于“Apple”1 及其“产品”5 的更多“文本”8,面向“客户”7 的“挪威”9”

spaCy 是否支持类似的东西?或者有没有办法使用 NLTK 或斯坦福?

小智 4

您可以使用neuralcoref库来获得与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
Run Code Online (Sandbox Code Playgroud)

在这里找到安装和使用说明: https: //github.com/huggingface/neuralcoref