为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

明月笑刀无情 2022-10-06 16:50:17
我目前正在尝试通过在不需要时删除额外的 spaCy 组件并在以后启用它们来加速我的应用程序。我想出了这段代码。import spacynlp = 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_listresult = 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()?
查看完整描述

1 回答

?
MM们

TA贡献1886条经验 获得超2个赞


您正在尝试将空白/未经训练的解析器添加回管道,而不是随附的解析器。取而代之的是 try disable_pipes(),这样可以更轻松地保存组件并稍后将其添加回来:


disabled = nlp.disable_pipes(["parser"])

# do stuff

disabled.restore()

请参阅:https ://spacy.io/api/language#disable_pipes


查看完整回答
反对 回复 2022-10-06
  • 1 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信