【问题标题】:ChatterBot error- OSError: [E941] Can't find model 'en'ChatterBot 错误-OSError:[E941] 找不到模型“en”
【发布时间】:2021-05-11 05:22:10
【问题描述】:

我尝试运行我的第一个 Chatterbot 程序(它来自 Chatterbot 的 PyPi 页面),但当我运行它时,我得到了一个错误。该错误与 Spacy 有关,但我无法找到解决方案。

代码如下:

from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('Ron Obvious')

trainer = ChatterBotCorpusTrainer(chatbot)

trainer.train("chatterbot.corpus.english")

chatbot.get_response("Hello, how are you today?")

这是错误:

Traceback (most recent call last):
  File "c:/users/USER/desktop/bot.py", line 77, in <module>
    chatbot = ChatBot('Ron Obvious')
  File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\chatterbot.py", line 28, in __init__
    self.storage = utils.initialize_class(storage_adapter, **kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\utils.py", line 33, in initialize_class
    return Class(*args, **kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\storage\sql_storage.py", line 20, in __init__
    super().__init__(**kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\storage\storage_adapter.py", line 21, in __init__
    'tagger_language', languages.ENG
  File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\tagging.py", line 13, in __init__
    self.nlp = spacy.load(self.language.ISO_639_1.lower())
  File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\spacy\__init__.py", line 47, in load
    return util.load_model(name, disable=disable, exclude=exclude, config=config)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\spacy\util.py", line 328, in load_model
    raise IOError(Errors.E941.format(name=name, full=OLD_MODEL_SHORTCUTS[name]))
OSError: [E941] Can't find model 'en'. It looks like you're trying to load a model from a shortcut, which is deprecated as of spaCy v3.0. To load the model, use its full name instead:

nlp = spacy.load("en_core_web_sm")

For more details on the available models, see the models directory: https://spacy.io/models. If you want to create a blank model, use spacy.blank: nlp = spacy.blank("en")

如果有人找到解决方案会很有帮助。谢谢。

【问题讨论】:

  • 一种可能的解决方案是将spacy 降级到2.x 版本,例如pip install -U spacy==2.1.3
  • 使用前可能需要python -m spacy download en_core_web_sm

标签: python python-3.x windows spacy chatterbot


【解决方案1】:

确保您确实安装了正确的 spacy 模型。例如,在终端中使用python -m spacy download en_core_web_sm 命令安装en_core_web_sm

接下来,修复这个错误:

File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\tagging.py", line 13, in __init__
    self.nlp = spacy.load(self.language.ISO_639_1.lower())

也就是说,

  1. 打开C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\tagging.py文件
  2. 去13号线
  3. self.nlp = spacy.load(self.language.ISO_639_1.lower()) 替换为
if self.language.ISO_639_1.lower() == 'en':
    self.nlp = spacy.load('en_core_web_sm')
else:
    self.nlp = spacy.load(self.language.ISO_639_1.lower())

您需要为需要支持的其他语言添加更多条件。

【讨论】:

  • 恭喜你!
  • @EzequielMiceli De nada!
【解决方案2】:

检查你正在使用的 spacy 版本。

安装 spacy 并下载语言模型,en_core_web_sm,在本例中使用

 python -m spacy download en_core_web_sm

如果是 v3.0,则需要使用加载它

nlp = spacy.load("en_core_web_sm")

如果是

python -m spacy link en_core_web_sm en

然后使用nlp = spacy.load("en")加载它

【讨论】:

    【解决方案3】:

    首先,您需要通过运行以下命令下载 en_core_web_sm:python -m spacy download en_core_web_sm

    您需要修改以下代码。

    enter image description here

    【讨论】:

      【解决方案4】:

      除了其他 cmets,请注意 SpaCy 3.0.3 和 Python 3.8 的问题 - 如果您正在使用这些版本,您可能需要通过 Python shell 下载语言模型,例如:

      import spacy
      from spacy.cli.download import download
      download(model="en_core_web_sm")
      

      对于这些版本,通过 python -m spacy download en_core_web_sm 下载可能会导致异常 - 如前所述。 here.

      【讨论】:

        【解决方案5】:

        尝试使用 >>pip install -U spacy 安装 spacy

        并更改代码

        self.nlp = spacy.load(self.language.ISO_639_1.lower()) 
        

        在“C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\tagging.py”到

          if self.language.ISO_639_1.lower() == 'en':
             self.nlp = spacy.load('en_core_web_sm')
          else:
            self.nlp = spacy.load(self.language.ISO_639_1.lower()) 
        

        在这里为我工作,我遇到了同样的问题

        【讨论】:

          【解决方案6】:

          LinuxMac 用户:

          对于上面投票最多的答案,我会补充说tagging.py 的位置是:

          /usr/local/lib/python3.7/site-packages/chatterbot

          更准确地说:

          &lt;Install_path_of_Python&gt;/site-packages/chatterbot

          (安装路径也可以是你的虚拟环境路径)

          【讨论】:

          • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
          猜你喜欢
          • 1970-01-01
          • 2020-01-23
          • 2023-03-08
          • 1970-01-01
          • 2021-11-23
          • 2020-08-19
          • 1970-01-01
          • 2019-10-11
          • 1970-01-01
          相关资源
          最近更新 更多