gpt4 book ai didi

python-3.x - 将标签 ID 分配到跨度 : not in StringStore 时出错

转载 作者:行者123 更新时间:2023-12-04 01:55:43 24 4
gpt4 key购买 nike

由于我对 spacy 和 python 非常陌生,因此我在这里尝试创建一个用户定义的标签来识别短语匹配,但在解析时我无法将哈希值分配给“标签”,遇到“分配标签 ID 时出错” XXXX 到一个跨度对象"

enter code here
import spacy
from spacy.matcher import PhraseMatcher
from spacy.tokens import Span

class EntityMatcher(object):
name = 'entity_matcher'

def __init__(self, nlp, terms, label):
patterns = [nlp(text) for text in terms]
self.matcher = PhraseMatcher(nlp.vocab)
self.matcher.add(label, None, *patterns)

def __call__(self, doc):
matches = self.matcher(doc)
for match_id, start, end in matches:
span = Span(doc, start, end, label=match_id)
doc.ents = list(doc.ents) + [span]
return doc

nlp = spacy.load('en_core_web_sm')
terms = (u'cat', u'dog', u'tree kangaroo', u'giant sea spider')
entity_matcher = EntityMatcher(nlp, terms, 'ANIMAL')

nlp.add_pipe(entity_matcher, after='ner')
print(nlp.pipe_names) # the components in the pipeline

doc = nlp(u"This is a text about Barack Obama and a tree kangaroo")
print([(ent.text, ent.label_) for ent in doc.ents])

****[Error]****
File "new.py", line 17, in __call__
span = Span(doc, start, end, label=match_id)
File "span.pyx", line 62, in spacy.tokens.span.Span.__cinit__
ValueError: [E084] Error assigning label ID 893087899 to span: not in
StringStore.

最佳答案

尝试将标签添加到 nlp 词汇表中。

from spacy.strings import StringStore

animal_hash = StringStore([u'ANIMAL']) # <-- match id
nlp.vocab.strings.add('ANIMAL')

关于python-3.x - 将标签 ID 分配到跨度 : not in StringStore 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51152665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com