我使用NER来清理文本,这样每个命名实体都会被替换为它的标签(PERSON、ORG等)。所以"John在苹果工作“就变成了”个人在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模型?非常感谢!
发布于 2020-07-22 01:29:50
你绝对是在正确的轨道上。spaCy文档以令人难以置信的清晰指南解决了您的问题。在https://spacy.io/usage/training上查看它。
我建议阅读整篇文章以真正理解该应用编程接口,但您最感兴趣的部分是Training the named entity recognizer。它详细说明了如何添加新的训练数据来微调现有(或空白) spaCy NER模型。也有代码示例!
请注意,他们的训练数据是硬编码的,但如果我是你,我会把它拉出到它自己的管道中。他们还推荐了几百个观察值,以在微调中获得最大的效果。
https://stackoverflow.com/questions/62924791
复制相似问题