AttributeError: type object 'Language' has no attribute 'factory'

AttributeError: type object 'Language' has no attribute 'factory'

我在使用“spacy_sentence_bert”时遇到错误

代码:

import spacy_sentence_bert
# load one of the models listed at https://github.com/MartinoMensio/spacy-sentence-bert/
nlp = spacy_sentence_bert.load_model('en_roberta_large_nli_stsb_mean_tokens')
# get two documents
doc_1 = nlp('Hi there, how are you?')
doc_2 = nlp('Hello there, how are you doing today?')
# use the similarity method that is based on the vectors, on Doc, Span or Token
print(doc_1.similarity(doc_2[0:7]))

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-42-d86d8866c1ca> in <module>()
      1 ##### using BERT
----> 2 import spacy_sentence_bert
      3 # load one of the models listed at https://github.com/MartinoMensio/spacy-sentence-bert/
      4 nlp = spacy_sentence_bert.load_model('en_roberta_large_nli_stsb_mean_tokens')
      5 # get two documents

1 frames
/usr/local/lib/python3.7/dist-packages/spacy_sentence_bert/language.py in <module>()
     24 
     25 # the pipeline stage factory
---> 26 @Language.factory('sentence_bert', default_config={
     27     'model_name': None,
     28     'debug': True

AttributeError: type object 'Language' has no attribute 'factory'

问题似乎与您的环境有关。

AttributeError: type object 'Language' has no attribute 'factory' message

是一个空洞的 2.x 错误。

请尝试运行 spacy 3.x。如果您已经安装了 3.x,那么请验证您的虚拟环境是否指向正确的 python。

我遇到了同样的问题(试图加载 2020 年创建的旧 BERT 模型),我通过 降级 spaCyspacymoji 解决了这个问题].我正在研究 Google Colab,它与下面相关。

首先,我安装了spacymoji版本2.0.0。其次,我删除了预装在 Google Colab 上的更新的 spaCy 版本。第三,我安装了旧的 spaCy 版本 2.2.2:

pip3 install spacymoji==2.0.0
pip3 uninstall spacy -y
pip3 install spacy==2.2.2

我确信公认的解决方案在某些情况下也适用,但我会把它留在这里以防其他人遇到我的情况!