制作一个Python聊天机器人

我们学习一下如何使用 ChatterBot 库在 Python 中创建聊天机器人,该库实现了各种机器学习算法来生成响应对话,还是挺不错的

什么是聊天机器人

聊天机器人也称为聊天机器人、机器人、人工代理等,基本上是由人工智能驱动的软件程序,其目的是通过文本或语音与用户进行对话。 我们日常接触的比较著名的例子包括 Siri、Alexa 等

这些聊天机器人倾向于为用户执行特定任务,聊天机器人经常执行诸如进行交易、预订酒店、提交表格等任务。随着人工智能领域的技术进步,聊天机器人的可能性也是无穷无尽的

当然了,在当前技术下,聊天机器人还是有很多局限性的

  • 领域知识 —— 由于真正的人工智能仍然遥不可及,任何聊天机器人在与人类对话时都很难完全理解对话含义

  • 个性 —— 无法正确响应和相当差的理解能力比任何聊天机器人的常见错误更重要,为聊天机器人添加个性仍然是很遥远和困难的事情

我们可以将聊天机器人定义为两类

  • 基于特定规则 —— 在这种方法中,机器人是根据规则进行训练的。 基于此,机器人可以回答简单的查询,但有时无法回答复杂的对话

  • 自学 —— 这些机器人遵循机器学习方法,效率更高,并进一步分为另外两类

    • 基于检索模型 —— 在这种方法中,机器人根据用户输入从响应列表中检索最佳响应

    • 生成模型 —— 这些模型通常会给出答案,而不是从一组答案中进行搜索,这也使它们成为智能机器人

好了,高大上的聊天机器人知识就先介绍到这里,下面我们就通过 chatterbot 来构建一个简单的在线聊天机器人

ChatterBot 库简介

ChatterBot 是 Python 中的一个库,它生成对用户输入的响应,使用多种机器学习算法来产生各种响应。 用户可以更轻松地使用 ChatterBot 库制作具有更准确响应的聊天机器人

ChatterBot 的设计允许机器人接受多种语言的训练,最重要的是,机器学习算法使机器人更容易使用用户的输入自行改进

ChatterBot 可以轻松创建参与对话的软件,每次聊天机器人从用户那里获得输入时,它都会保存输入和响应,这有助于没有初始知识的聊天机器人使用收集到的响应进行自我进化

随着响应的增加,聊天机器人的准确性也会提高。 程序从与输入匹配的最接近匹配语句中选择最接近匹配的响应,然后从该响应的已知语句选择中选择响应

安装 ChatterBot 也非常简单

pip install chatterbot

下面我们就正式进入 Chatterbot 的世界吧

构建聊天机器人

机器人训练

Chatterbot 带有一个数据实用程序模块,可用于训练聊天机器人。 目前该模块中有十多种语言的训练数据,我们可以拿来直接使用

https://github.com/gunthercox/chatterbot-corpus

下面是在 python 中开始使用 ChatterBot 的简单示例

from chatterbot import chatbot
from chatterbot.trainers import ListTrainer

chatbot = Chatbot('Edureka')
trainer = ListTrainer(chatbot)
trainer.train([ 'hi, can I help you find a course', 'sure I'd love to find you a course', 'your course have been selected'])

response = chatbot.get_response("I want a course")
print(response)

在例子中,我们根据提供的输入从聊天机器人获得响应

构建 flask app

对于基本的 flask 结构,我们直接使用 GitHub 上的一个脚手架,这个是专门用来开发 ChatterBot 应用的

https://github.com/chamkank/flask-chatterbot

我们直接克隆项目就好

把项目下载到本地之后,我们进行一些修改

我们需要为 HTML 和 CSS 文件添加另外两个目录 static 和模板

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

<
  • 20
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
by Sumit Raj Apress 2019-02-04 192 pages Build your own chatbot using Python and open source tools. This book begins with an introduction to chatbots where you will gain vital information on their architecture. You will then dive straight into natural language processing with the natural language toolkit (NLTK) for building a custom language processing platform for your chatbot. With this foundation, you will take a look at different natural language processing techniques so that you can choose the right one for you. The next stage is to learn to build a chatbot using the API.ai platform and define its intents and entities. During this example, you will learn to enable communication with your bot and also take a look at key points of its integration and deployment. The final chapter of Building Chatbots with Python teaches you how to build, train, and deploy your very own chatbot. Using open source libraries and machine learning techniques you will learn to predict conditions for your bot and develop a conversational agent as a web application. Finally you will deploy your chatbot on your own server with AWS. What You Will Learn Gain the basics of natural language processing using Python Collect data and train your data for the chatbot Build your chatbot from scratch as a web app Integrate your chatbots with Facebook, Slack, and Telegram Deploy chatbots on your own server Who This Book Is For Intermediate Python developers who have no idea about chatbots. Developers with basic Python programming knowledge can also take advantage of the book.
好的,你想制作一个微信聊天机器人,需要以下步骤: 1. 注册一个微信公众号,并通过审核。 2. 在公众号设置中开启开发者模式,并获取AppID和AppSecret。 3. 在微信公众平台中申请开通“开发者模式”和“消息管理权限”。 4. 安装Flask框架和WeChat SDK。 5. 编写Python代码实现微信公众号的自动回复功能。 以下是一个简单的微信聊天机器人的代码示例: ```python from flask import Flask, request from wechatpy import parse_message from wechatpy.replies import TextReply app = Flask(__name__) @app.route('/wechat', methods=['GET', 'POST']) def wechat(): if request.method == 'GET': token = 'your_token' echostr = request.args.get('echostr', '') if check_signature(token, request): return echostr else: return 'Wrong validation token' else: xml = request.data msg = parse_message(xml) if msg.type == 'text': reply = TextReply(content=auto_reply(msg.content), message=msg) return reply.render() else: return '' def check_signature(token, request): signature = request.args.get('signature', '') timestamp = request.args.get('timestamp', '') nonce = request.args.get('nonce', '') tmp_list = [token, timestamp, nonce] tmp_list.sort() tmp_str = ''.join(tmp_list) tmp_str = hashlib.sha1(tmp_str.encode('utf-8')).hexdigest() return tmp_str == signature def auto_reply(msg): # 在这里编写自动回复的逻辑 return '你好,我是机器人' if __name__ == '__main__': app.run(host='0.0.0.0', port=80) ``` 这个代码示例使用了Flask框架和WeChat SDK,实现了微信公众号的自动回复功能。你需要将代码中的your_token替换为你在微信公众平台中设置的Token。 当有用户给公众号发送文本消息时,程序会调用auto_reply()函数进行自动回复。你可以在这个函数中编写自己的自动回复逻辑。 如果你想了解更多关于Python实现微信聊天机器人的知识,可以参考以下链接: 1. [使用Python实现微信公众号](https://zhuanlan.zhihu.com/p/146530965) 2. [WeChat SDK官方文档](https://wechatpy.readthedocs.io/en/stable/index.html)
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhouluobo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值