机器人"/>
基于chatterbot制作聊天机器人
一、环境搭建
python 3.6
安装chatterbot
安装方式:
1.在项目目录下pip install chatterbot进行安装
2.下载源码,运行setup.py进行安装
本人尝试使用第一种安装方式失败后,下载源码进行安装成功。
报错信息:
根据刚才第一次安装的报错信息,查看requirement.txt中的版本要求,可以看到:
使用pip list查看所有库的版本:
对比要求的版本号,进行版本更新:
使用pip install --upgrade pyyaml
仍报错,提示不能卸载pyyaml,我们可以执行以下命令:
pip install --ignore-installed pyyaml 会略之前的安装版本
检查是否已安装:python -m chatterbot –version 查看版本号
二、使用chatbot
1.首先尝试基于规则的聊天问答,即设置固定格式的问答。
代码如下:
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainerChinese_bot = ChatBot("Training demo") #创建一个新的实例
trainer = ListTrainer(Chinese_bot)
trainer.train(['在吗?','亲,在呢','鞋按正常尺码买吗?','是呢,请放心下单吧。','有43码的吗?','暂时没有哦。',
])
报错: OSError: Can't find model 'en'
解决方案:
python -m spacy download en
测试结果:
question="在吗?"
print(question)
response=Chinese_bot.get_response(question)
print(response)
print("\n")
question="有43码的吗?"
print(question)
response=Chinese_bot.get_response(question)
print(response)
2.使用自己的语料库进行训练:
比如将聊天记录进行处理,进行自动问答训练。
使用聊天记录进行实验。
3.也可以使用官网提供的中文语料库进行训练,除此外,还有英文、西班牙语等其他语言的语料库可供训练。
可以查看下语料库内容:
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
import logging'''
This is an example showing how to train a chat bot using the
ChatterBot Corpus of conversation dialog.
'''# Enable info level logging
#logging.basicConfig(level=logging.INFO)chatbot = ChatBot('Example Bot')# Start by training our bot with the ChatterBot corpus data
trainer = ChatterBotCorpusTrainer(chatbot)trainer.train('chatterbot.corpus.chinese'
)
进行测试:
response = chatbot.get_response('我告诉你一个秘密,你不要和别人说')
print(response)
测试结果:
更多推荐
基于chatterbot制作聊天机器人
发布评论