【问题标题】:Unable to create a custom entity type/label using Matcher in Spacy 2无法在 Spacy 2 中使用 Matcher 创建自定义实体类型/标签
【发布时间】: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_)

【问题讨论】:

    标签: python spacy


    【解决方案1】:

    哦,好吧,我想我找到了解决方案。如果标签不存在,则必须将标签添加到 nlp.vocab.strings:

    nlp.vocab.strings.add('FRUIT') 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多