我正在尝试使用多个组件构建一个SpaCy管道。目前我的管道只有两个组件,一个是实体标尺,另一个是自定义组件。
我建造它的方式是这样的:
class EntityLookupComponent:
def __call__(self, doc: Doc) -> Doc:
print("Just testing")
return doc
@Language.factory("entity_lookup_component")
def my_component(nlp, name):
return EntityLookupComponent(nlp)
def main(patterns_path: Path, output_path: Path):
"""Build the spaCy model and output it to disk"""
# Ensure output_path directory exists
if not Path(os.path.dirname(output_path)).is_dir():
os.makedirs(os.path.dirname(output_path))
nlp = English()
nlp.add_pipe("entity_ruler").from_disk(patterns_path)
nlp.add_pipe("entity_lookup_component", name="entity_lookup", last=True)
print(nlp.pipe_names)
nlp.to_disk('./test')
with open(output_path, "wb") as output_file:
pickle.dump(nlp, output_file)输出pipe_names给我的是:['entity_ruler', 'entity_lookup']。
但是,当我尝试加载模型并进行测试时,请执行以下操作:
nlp = spacy.load("en_core_web_lg", disable=["ner"])
nlp.add_pipe("entity_ruler", source=spacy.load("./test"))它立即将我抛出以下错误:
ValueError: [E002] Can't find factory for 'entity_lookup_component' for language English (en). This usually happens when spaCy calls `nlp.create_pipe` with a custom component name that's not registered on the current language class. If you're using a Transformer, make sure to install 'spacy-transformers'. If you're using a custom component, make sure you've added the decorator `@Language.component` (for function components) or `@Language.factory` (for class components).只有在我添加了entity_lookup_component之后才会发生这种情况。这个组件应该使用一个查找表来向现有实体添加一些元数据。
发布于 2021-12-21 10:15:52
在加载模型的位置,需要访问定义自定义组件的代码。因此,如果定义自定义组件的文件是custom.py,则可以将import custom放在加载管道的文件的顶部,并且应该工作。
还请参阅关于保存和加载自定义组件的医生们。
https://stackoverflow.com/questions/70432899
复制相似问题