首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何导出多个组件的SpaCy模型

如何导出多个组件的SpaCy模型
EN

Stack Overflow用户
提问于 2021-12-21 09:05:43
回答 1查看 299关注 0票数 0

我正在尝试使用多个组件构建一个SpaCy管道。目前我的管道只有两个组件,一个是实体标尺,另一个是自定义组件。

我建造它的方式是这样的:

代码语言:javascript
复制
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']

但是,当我尝试加载模型并进行测试时,请执行以下操作:

代码语言:javascript
复制
nlp = spacy.load("en_core_web_lg", disable=["ner"])
nlp.add_pipe("entity_ruler", source=spacy.load("./test"))

它立即将我抛出以下错误:

代码语言:javascript
复制
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之后才会发生这种情况。这个组件应该使用一个查找表来向现有实体添加一些元数据。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-21 10:15:52

在加载模型的位置,需要访问定义自定义组件的代码。因此,如果定义自定义组件的文件是custom.py,则可以将import custom放在加载管道的文件的顶部,并且应该工作。

还请参阅关于保存和加载自定义组件的医生们

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70432899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档