实际上,这个问题有一个问题报告。
这个库的作者建议使用以下解决方案之一:
- 使用全局目标的波束搜索算法。
这是标准解决方案:使用全局目标,以便训练解析器模型更倾向于整体上更好的分析结果。保留N个不同的候选项,并输出最佳候选项。可以通过查看波束中的备选分析来支持置信度。如果一个实体在每个分析中都出现,则命名实体识别更有把握判断其正确性。
代码:
import spacy
import sys
from collections import defaultdict
nlp = spacy.load('en')
text = u'Will Japan join the European Union? If yes, we should \
move to United States. Fasten your belts, America we are coming'
with nlp.disable_pipes('ner'):
doc = nlp(text)
threshold = 0.2
(beams, somethingelse) = nlp.entity.beam_parse([ doc ], beam_width = 16, beam_density = 0.0001)
entity_scores = defaultdict(float)
for beam in beams:
for score, ents in nlp.entity.moves.get_beam_parses(beam):
for start, end, label in ents:
entity_scores[(start, end, label)] += score
print ('Entities and scores (detected with beam search)')
for key in entity_scores:
start, end, label = key
score = entity_scores[key]
if ( score > threshold):
print ('Label: {}, Text: {}, Score: {}'.format(label, doc[start:end], score))
样例输出:
实体及其得分(使用beam search检测)
标签:GPE,文本:日本,得分:0.9999999999999997
标签:GPE,文本:美国,得分:0.9991664575947963
重要提示:在这里得到的输出可能与使用标准NER而不是beam search替代方法得到的输出不同。然而,从您的问题中我理解到,beam search替代方案提供了对置信度的度量,这对您的情况很有用。
此示例的标准NER输出:
标签:GPE,文本:日本
标签:ORG,文本:欧盟
标签:GPE,文本:美国
标签:GPE,文本:美国