重新启用 spacy 的解析器组件给出错误

我目前正在尝试通过在不需要时删除额外的 spaCy 组件并在以后启用它们来加速我的应用程序。我想出了这段代码。


import spacy

nlp = spacy.load("en_core_web_lg", disable=('ner', 'textcat'))

nlp.pipe_names

它给出了以下输出


['tagger', 'parser']

我必须执行一项任务,下面是代码片段


text = """Extracts the selected  layers in the specified area of interest.... """


doc = nlp(text)


def get_pos(remove_parser=True):

    if remove_parser: 

        nlp.remove_pipe("parser")


    for kw in keywords:

        doc = nlp(kw[0])

        tag_list = [(token.text, token.tag_) for token in doc]


    if remove_parser:

        nlp.add_pipe(nlp.create_pipe('parser'))


    return tag_list


result = get_pos(remove_parser=True)

nlp.pipe_names

所以我get_pos用remove_parser=True. 它删除解析器组件,为列表nlp(kw[0])中的每个项目运行。keywords循环结束后,我添加回parser组件,这可以通过nlp.pipe_names命令的输出进行验证。我得到以下输出


['tagger', 'parser']   

但是,如果我在函数调用nlp("Hello World")之后get_pos调用。它给出了这个错误 -


ValueError                                Traceback (most recent call last)

<ipython-input-29-320b76b1fe36> in <module>

----> 1 nlp("Hello World")


~\.conda\envs\keyword-extraction\lib\site-packages\spacy\language.py in __call__(self, text, disable, component_cfg)

    433             if not hasattr(proc, "__call__"):

    434                 raise ValueError(Errors.E003.format(component=type(proc), name=name))

--> 435             doc = proc(doc, **component_cfg.get(name, {}))

    436             if doc is None:

    437                 raise ValueError(Errors.E005.format(name=name))


nn_parser.pyx in spacy.syntax.nn_parser.Parser.__call__()


nn_parser.pyx in spacy.syntax.nn_parser.Parser.predict()


nn_parser.pyx in spacy.syntax.nn_parser.Parser.require_model()


ValueError: [E109] Model for component 'parser' not initialized. Did you forget to load a model, or forget to call begin_training()?


明月笑刀无情
浏览 86回答 1
1回答

MM们

您正在尝试将空白/未经训练的解析器添加回管道,而不是随附的解析器。取而代之的是 try disable_pipes(),这样可以更轻松地保存组件并稍后将其添加回来:disabled = nlp.disable_pipes(["parser"])# do stuffdisabled.restore()请参阅:https ://spacy.io/api/language#disable_pipes
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python