1. 安装更新
$ pip install chatterbot
$ pip install chatterbot --upgrade
2. 创建机器人
from chatterbot import ChatBot
chatbot = ChatBot("Ron Obvious")
3. 训练机器人
from chatterbot.trainers import ListTrainer
conversation = [
"Hello",
"Hi there!",
"How are you doing?",
"I'm doing great.",
"That is good to hear",
"Thank you.",
"You're welcome."
]
chatbot.set_trainer(ListTrainer)
chatbot.train(conversation)
4. 获取机器人回复
response = chatbot.get_response("Good morning!")
print(response)
5. 只读模式
ChatterBot机器人每次接收到输入时都会自主进行学习,如果已经训练好机器人之后,不希望机器人再进行自主学习,可以设置为只读模式。当然可以使用这一特性来区分增量学习与生产应用。
chatbot = ChatBot("Johnny Five", read_only=True)