【Python】chatterbotのインストール注意点と日本語対応【メモ】

環境

※ python 3.8.0

注意点としてはpython 3.6.0ではうまくインストールできなかったので、
pythonのバージョンに関しては注意が必要。

chatterbotをインストールして動かす

必要なモジュールなどをインストールする。

pip install pytz
pip install spacy==2.3.5
python -m spacy download ja_core_news_sm
pip install ChatterBot

日本語コーパスを取得する

ここに日本語コーパスを格納するディレクトリの用意

mkdir -p ~/chatterbot_corpus/data

テキトウなディレクトリにchatterbotのコーパスを落として、
日本語のcorpusを先程作成したディレクトリに格納する。

git clone https://github.com/gunthercox/chatterbot-corpus
cp chatterbot-corpus/data/japanese ~/chatterbot_corpus/data/

残りの必要なモジュールなどをinstallする。

python -m spacy download en
pip install pyyaml

サンプルコードを書いて動かす

サンプルコードを書く。
(ここは公式のを日本語にしただけ。)
このサンプルコードはchat_sample.pyとする。

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('Ron Obvious')

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)

# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.japanese")

# Get a response to an input statement
print(chatbot.get_response("こんにちわ"))

動かす。

python chat_sample.py

何か帰ってくるはず。

参考文献

  • https://github.com/gunthercox/ChatterBot
タイトルとURLをコピーしました