【发布时间】:2018-04-07 02:02:46
【问题描述】:
我正在尝试使用基于规则的匹配器(即添加 on_match 规则)创建一个名为 FRUIT 的自定义实体标签,遵循 spaCy guide。我使用的是 spaCy 2.0.11,所以我相信这样做的步骤与 spaCy 1.X 相比有所改变
示例:doc = nlp('汤姆想在联合国吃些苹果')
预期的文本和实体输出:
Tom PERSON
apples FRUIT
the United Nations ORG
但是,我似乎收到以下错误:[E084] 将标签 ID 7429577500961755728 分配给跨度时出错:不在 StringStore 中。我在下面包含了我的代码。当我将 nlp.vocab.strings['FRUIT'] 更改为 nlp.vocab.strings['EVENT'] 时,奇怪的是它可以工作,但苹果会被分配实体标签 EVENT。还有其他人遇到这个问题吗?
doc = nlp('Tom wants to eat some apples at the United Nations')
FRUIT = nlp.vocab.strings['FRUIT']
def add_ent(matcher, doc, i, matches):
# Get the current match and create tuple of entity label, start and end.
# Append entity to the doc's entity. (Don't overwrite doc.ents!)
match_id, start, end = matches[i]
doc.ents += ((FRUIT, start, end),)
matcher = Matcher(nlp.vocab)
pattern = [{'LOWER': 'apples'}]
matcher.add('AddApple', add_ent, pattern)
matches = matcher(doc)
for ent in doc.ents:
print(ent.text, ent.label_)
【问题讨论】: