end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

install chatterbot for python to anaconda for win

日本語環境での chatterbot インストールは、 情報が少ないらしく、やや手こずったので、メモ

2023/7/4 追記

chatterbotは「chatbot python」で検索すると上位に表示されますので、 インストールまでは行いましたが 【Python】チャットボットフレームワーク、ChatterBotの実装 を読むと、 期待したほどのものはできそうにないので、 chatterbotは今後、使用しないと思います

参考url

anaconda仮想環境の作成と、activate

CONDA> conda create -n chat1 python=3.8.0
CONDA> activate chat1

pip install

CONDA> pip install chatterbot
CONDA> pip install chatterbot-corpus
CONDA> pip install spacy

CONDA> python -m spacy download en_core_web_sm

CONDA> pip list
Package            Version
------------------ --------
 :
ChatterBot         1.0.8
chatterbot-corpus  1.2.0
 :
PyYAML             3.13
 :
spacy              3.5.4
 :

日本語コーパスのダウンロードとコピー

CONDA> wget https://github.com/gunthercox/chatterbot-corpus/archive/refs/heads/master.zip
CONDA> unzip master.zip
CONDA> cp chatterbot-corpus-master/chatterbot_corpus/data/japanese \
       ~/anaconda3/envs/chat1/Lib/site-packages/chatterbot_corpus/data/

tagging.py の修正

CONDA> vi /anaconda3/envs/chat1/Lib/site-packages/chatterbot/tagging.py

【OLD】
self.nlp = spacy.load(self.language.ISO_639_1.lower())
【NEW】
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())

上記の修正を行わない場合、ChatBotをnewする際に なぜか以下のエラーとなります。

OSError: [E941] Can't find model 'en'.
It looks like you're trying to load a model from a shortcut,
which is obsolete 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")

動作テスト

単に動いていることを確認するもので、テストと呼べる程ではありませんが

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('test')
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train("chatterbot.corpus.japanese")
response = chatbot.get_response("こんにちは")
print(response)

↑こう書くと、↓こう表示されます

Training ai.yml: [####################] 100%
Training botprofile.yml: [####################] 100%
Training computers.yml: [####################] 100%
Training conversations.yml: [####################] 100%
Training emotion.yml: [####################] 100%
Training food.yml: [####################] 100%
Training gossip.yml: [####################] 100%
Training greetings.yml: [####################] 100%
Training health.yml: [####################] 100%
Training history.yml: [####################] 100%
Training humor.yml: [####################] 100%
Training literature.yml: [####################] 100%
Training money.yml: [####################] 100%
Training movies.yml: [####################] 100%
Training politics.yml: [####################] 100%
Training psychology.yml: [####################] 100%
Training science.yml: [####################] 100%
Training sports.yml: [####################] 100%
Training trivia.yml: [####################] 100%
こんにちは