基于chatterbot制作聊天机器人

编程入门 行业动态 更新时间:2024-10-24 02:00:46

基于chatterbot制作聊天<a href=https://www.elefans.com/category/jswz/34/1771107.html style=机器人"/>

基于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制作聊天机器人

本文发布于:2024-03-09 07:43:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1724342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:机器人   chatterbot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!