【发布时间】:2021-02-01 19:50:30
【问题描述】:
我使用 NER 实质上擦洗文本,以便将每个命名实体替换为其标签(PERSON、ORG 等)。所以“John 在 Apple 工作”会变成“PERSON 在 ORG 工作”。
clause_text 是我的句子列表。我使用 ner-d 包来构建我的 NER 模型并按如下方式清理文本:
for text in clause_text:
input_text = text
doc = ner.name(input_text, language='en_core_web_sm')
text_label = [(X.text, X.label_) for X in doc]
# replace all named entities with their label (PERSON, ORG, etc)
for text, label in text_label:
input_text = input_text.replace(text, label)
scrubbed_text.append(input_text)
现在,我正在尝试添加自定义训练数据。基本上,我希望能够添加带有标签的句子并更新 NER 模型,以使其更准确/更具体地满足我的需要。现在我有这个:
nlp = spacy.load('en_core_web_sm')
if 'ner' not in nlp.pipe_names:
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner)
else:
ner = nlp.get_pipe('ner')
from spacy.gold import GoldParse
from spacy.pipeline import EntityRecognizer
doc_list = []
doc = nlp('This EULA stipulates a contract for Hamilton Enterprises.')
doc_list.append(doc)
gold_list = []
gold_list.append(GoldParse(doc, [u'O', u'O', u'O', u'O', u'O', u'O', u'ORG']))
ner = EntityRecognizer(nlp.vocab, entity_types = ['ORG'])
ner.update(doc_list, gold_list)
但是当我运行它时,我得到了这个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-92c53f5c90b1> in <module>
9
10 ner = EntityRecognizer(nlp.vocab, entity_types = ['ORG'])
---> 11 ner.update(doc_list, gold_list)
nn_parser.pyx in spacy.syntax.nn_parser.Parser.update()
nn_parser.pyx in spacy.syntax.nn_parser.Parser.require_model()
ValueError: [E109] Model for component 'ner' not initialized. Did you forget to load a model, or forget to call begin_training()?
是否有人对如何最好地修复此代码有任何见解,或者是否有更好的方法来添加自定义条目以更新 NER 模型?非常感谢!
【问题讨论】:
标签: python nlp spacy named-entity-recognition