重新启用 spacy 的解析器组件会出错
re enabling parser component of spacy give error
我目前正在尝试通过在不需要时删除额外的 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
所以我用remove_parser=True
调用了get_pos
函数。它为 keywords
列表中的每个项目删除了解析器组件 运行 nlp(kw[0])
。循环结束后,我添加回 parser
组件,这可以通过 nlp.pipe_names
命令的输出来验证。我得到以下输出
['tagger', 'parser']
但是如果我在 get_pos
函数调用之后调用 nlp("Hello World")
。它给出了这个错误 -
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()?
您正在尝试将 blank/untrained 解析器添加回管道,而不是与其一起提供的解析器。相反,请尝试 disable_pipes()
,这样可以更轻松地保存组件并稍后将其添加回来:
disabled = nlp.disable_pipes(["parser"])
# do stuff
disabled.restore()
我目前正在尝试通过在不需要时删除额外的 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
所以我用remove_parser=True
调用了get_pos
函数。它为 keywords
列表中的每个项目删除了解析器组件 运行 nlp(kw[0])
。循环结束后,我添加回 parser
组件,这可以通过 nlp.pipe_names
命令的输出来验证。我得到以下输出
['tagger', 'parser']
但是如果我在 get_pos
函数调用之后调用 nlp("Hello World")
。它给出了这个错误 -
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()?
您正在尝试将 blank/untrained 解析器添加回管道,而不是与其一起提供的解析器。相反,请尝试 disable_pipes()
,这样可以更轻松地保存组件并稍后将其添加回来:
disabled = nlp.disable_pipes(["parser"])
# do stuff
disabled.restore()