spaCy nlp.pipe错误,使用spacylangdetect进行多处理(n_进程>1)

2024-09-22 22:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的环境

  • MacOS 10.15/Debian 11
  • Python 3.8.2/3.8.12
  • Spacy 3.1.2
  • 间距0.1.2

我正在尝试使用spacy-langdetect在spaCy NLP管道中添加语言检测功能。当我使用单个进程执行检测时,一切看起来都很好,如下面的示例所示

import spacy
from spacy_langdetect import LanguageDetector
from spacy.language import Language


# Load Language detection
@Language.factory('language_detector')
def language_detector(nlp, name):
    return LanguageDetector()


# Load Spacy
nlp = spacy.load("en_core_web_lg")
nlp.add_pipe('language_detector', last=True)

print([doc._.language for doc in nlp.pipe(['I bless the rains down in Africa'], n_process=-1)])

返回

[{'language': 'en', 'score': 0.9999985260933938}]

但是我在设置n_进程时出现了以下错误>;1:

AttributeError: [E046] Can't retrieve unregistered extension attribute 'language'. Did you forget to call the `set_extension` method?

我认为这是因为在macOS上使用了spawn方法(在多处理中),因为Python3.8和一些上下文没有发送到子进程,但在Linux上使用fork方法时,我得到了相同的错误。有人对此错误有何解释和解决方法吗


Tags: 方法fromimportnlpspacy进程错误load