spaCy官方classy classification示例报错:组件工厂未找到的解决咨询
问题
我正在学习使用spaCy,现在想要学习classy classification,但spaCy官方网页上展示的classy示例无法正常运行。以下是我使用的代码以及出现的错误信息:
import spacy data = { "furniture": ["This text is about chairs.", "Couches, benches and televisions.", "I really need to get a new sofa."], "kitchen": ["There also exist things like fridges.", "I hope to be getting a new stove today.", "Do you also have some ovens."] } # see github repo for examples on sentence-transformers and Huggingface nlp = spacy.load('en_core_web_md') nlp.add_pipe("classy_classification", config={ "data": data, "model": "spacy" } ) print(nlp("I am looking for kitchen appliances.")._.cats)
错误信息:
File "C:\Users\vidrr\AppData\Local\Programs\Python\Python312\Lib\site-packages\spacy\language.py", line 821, in add_pipe pipe_component = self.create_pipe( ^^^^^^^^^^^^^^^^^ File "C:\Users\vidrr\AppData\Local\Programs\Python\Python312\Lib\site-packages\spacy\language.py", line 690, in create_pipe raise ValueError(err) ValueError: [E002] Can't find factory for 'classy_classification' 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 custom component, make sure you've added the decorator `@Language.component` (for function components) or `@Language.factory` (for class components). Available factories: attribute_ruler, tok2vec, merge_noun_chunks, merge_entities, merge_subtokens, token_splitter, doc_cleaner, parser, beam_parser, lemmatizer, trainable_lemmatizer, entity_linker, entity_ruler, tagger, morphologizer, ner, beam_ner, senter, sentencizer, spancat, spancat_singlelabel, span_finder, future_entity_ruler, span_ruler, textcat, textcat_multilabel, en.lemmatizer
解决方法
- 安装对应第三方库
classy_classification不是spaCy内置组件,需要单独安装配套库。在终端执行命令:
pip install spacy-classy-classification
- 修改代码导入组件
安装完成后,在代码中导入该库,它会自动完成组件注册。修改后的代码如下:
import spacy import spacy_classy_classification # 新增该行,注册classy_classification组件 data = { "furniture": ["This text is about chairs.", "Couches, benches and televisions.", "I really need to get a new sofa."], "kitchen": ["There also exist things like fridges.", "I hope to be getting a new stove today.", "Do you also have some ovens."] } nlp = spacy.load('en_core_web_md') nlp.add_pipe("classy_classification", config={ "data": data, "model": "spacy" } ) print(nlp("I am looking for kitchen appliances.")._.cats)
- 运行验证
执行修改后的代码,即可正常加载组件并输出分类结果。
内容的提问来源于stack exchange,提问作者David Ramirez
相关产品推荐
相关产品推荐

