问题描述
我从 kaggel 中找到了以下代码,每次运行代码时都会出现 ValueError。 这是因为 SpaCy 的新版本。请帮助 提前致谢
import scispacy
import spacy
import en_core_sci_lg
from spacy_langdetect import LanguageDetector
nlp = en_core_sci_lg.load(disable=["tagger","ner"])
nlp.max_length = 2000000
nlp.add_pipe(LanguageDetector(),name='language_detector',last=True)
ValueError: [E966] 如果您使用 如果你传入了一个像 如果您使用自定义组件:将装饰器 我已经安装: scispacy.version : '0.4.0' en_core_sci_lg.version : '0.4.0' python_version : 3.8.5 spacy.version : '3.0.3'nlp.add_pipe
现在采用已注册组件工厂的字符串名称,而不是可调用组件。预期的字符串,但在 0x00000216BB4C8D30> 处得到
nlp.create_pipe('name')
创建组件:删除 nlp.create_pipe 并改为调用 nlp.add_pipe('name')
。TextCategorizer()
这样的组件:用字符串名称调用 nlp.add_pipe
,例如nlp.add_pipe('textcat')
。@Language.component
(用于函数组件)或 @Language.factory
(用于类组件/工厂)添加到您的自定义组件并为其分配名称,例如@Language.component('your_name')
。然后您可以运行 nlp.add_pipe('your_name')
将其添加到管道中。
解决方法
add_pipe
的工作方式在 v3 中发生了变化;必须注册组件,然后可以仅使用它们的名称将其添加到管道中。在这种情况下,您必须像这样包装 LanguageDetector:
import scispacy
import spacy
import en_core_sci_lg
from spacy_langdetect import LanguageDetector
from spacy.language import Language
def create_lang_detector(nlp,name):
return LanguageDetector()
Language.factory("language_detector",func=create_lang_detector)
nlp = en_core_sci_lg.load(disable=["tagger","ner"])
nlp.max_length = 2000000
nlp.add_pipe('language_detector',last=True)
您可以在 the spaCy docs 中阅读有关其工作原理的更多信息。
,您还可以使用 @Language.factory
装饰器以更少的代码实现相同的结果:
import scispacy
import spacy
import en_core_sci_lg
from spacy_langdetect import LanguageDetector
from spacy.language import Language
@Language.factory('language_detector')
def language_detector(nlp,name):
return LanguageDetector()
nlp = en_core_sci_lg.load(disable=["tagger",last=True)