今天嘗試運作一個chatbot的demo,建立實體時報錯:
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")
其實個人感覺是包和包之間的沖突,spacyV3.0以後不再支援“en”這樣簡單的引用,期待全名稱“en_core_web_sm”引用。而chatbot則未能跟進,源碼中保持嘗試“en”這樣的deprecated的shortcut。
解決方法:
進入py環境下chatbot包中的tagging.py子產品,将報錯資訊中提到的這句:
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())
重新加載後問題解決。