AttributeError:'spacy.pipeline.ner.EntityRecognizer'对象没有属性'add_pipe'

问题描述 投票:0回答:1

为什么下面的代码会抛出

add_pipe
属性未定义的错误?

if 'ner' not in nlp.pipe_names:
    ner = nlp.create_pipe('ner')
    ner.add_pipe(ner , last = True)
    for _, annotation in train_data:
      for ent in annotation['entities']:
        ner.add_label(ent[2])
nlp
1个回答
1
投票

要将管道组件添加到 spacy,您必须将它们添加到加载的模型 (

nlp
),而不是单个组件(如
ner
)。

if 'ner' not in nlp.pipe_names: 
    ner = nlp.create_pipe('ner') 
    nlp.add_pipe(ner, last = True)  # replaced ner by nlp here
    for _, annotation in train_data: 
        for ent in annotation['entities']: 
            ner.add_label(ent[2])
© www.soinside.com 2019 - 2024. All rights reserved.