前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python微信公众号后台开发<005>:集成智能聊天机器人​

Python微信公众号后台开发<005>:集成智能聊天机器人​

作者头像
Python疯子
发布2020-02-13 13:42:37
9060
发布2020-02-13 13:42:37
举报
文章被收录于专栏:Python疯子Python疯子

​给公众号集成一个智能聊天机器人

一、前述

ChatterBot是一个基于机器学习的聊天机器人引擎,构建在python上,主要特点是可以自可以从已有的对话中进行学(jiyi)习(pipei)。

二、具体

1、安装

是的,安装超级简单,用pip就可以啦

<pre style="margin: 0px; padding: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; white-space: pre-wrap; overflow-wrap: break-word; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-size: 14px; text-align: left; text-size-adjust: auto;">pip install chatterbot</pre>

2、流程

大家已经知道chatterbot的聊天逻辑和输入输出以及存储,是由各种adapter来限定的,我们先看看流程图,一会再一起看点例子,看看怎么用。

image

3、每个部分都设计了不同的“适配器”(Adapter)。

机器人应答逻辑 => Logic Adapters Closest Match Adapter 字符串模糊匹配(编辑距离)

Closest Meaning Adapter  借助nltk的WordNet,近义词评估 Time Logic Adapter 处理涉及时间的提问 Mathematical Evaluation Adapter 涉及数学运算

存储器后端 => Storage Adapters  Read Only Mode 只读模式,当有输入数据到chatterbot的时候,数 据库并不会发生改变  Json Database Adapter 用以存储对话数据的接口,对话数据以Json格式 进行存储。 Mongo Database Adapter  以MongoDB database方式来存储对话数据

输入形式 => Input Adapters

Variable input type adapter 允许chatter bot接收不同类型的输入的,如strings,dictionaries和Statements Terminal adapter 使得ChatterBot可以通过终端进行对话  HipChat Adapter 使得ChatterBot 可以从HipChat聊天室获取输入语句,通过HipChat 和 ChatterBot 进行对话 Speech recognition 语音识别输入,详见chatterbot-voice

输出形式 => Output Adapters Output format adapter支持text,json和object格式的输出 Terminal adapter HipChat Adapter Mailgun adapter允许chat bot基于Mailgun API进行邮件的发送 Speech synthesisTTS(Text to speech)部分,详见chatterbot-voice

4、代码

计算模式

代码语言:javascript
复制
from chatterbot import ChatBot


bot = ChatBot(
    "Math & Time Bot",
    logic_adapters=[
        "chatterbot.logic.MathematicalEvaluation",
        "chatterbot.logic.TimeLogicAdapter"
    ],
    input_adapter="chatterbot.input.VariableInputTypeAdapter",
    output_adapter="chatterbot.output.OutputAdapter"
)

# 进行数学计算
question = "What is 4 + 9?"
print(question)
response = bot.get_response(question)
print(response)

print("\n")

# 回答和时间相关的问题
question = "What time is it?"
print(question)
response = bot.get_response(question)
print(response)

image

利用已经提供好的小中文语料库

代码语言:javascript
复制
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot("ChineseChatBot")
trainer = ChatterBotCorpusTrainer(chatbot)

# 使用中文语料库训练它
trainer.train("chatterbot.corpus.chinese")

def response_text(sentence):
    res_text = chatbot.get_response(sentence)

    print(sentence , "----", res_text)

    return res_text
#
if __name__ == '__main__':

    # 开始对话
    while True:
        print(chatbot.get_response(input(">")))

    # print(response_text("你是谁"))

image

小黄鸡语料更智能(推荐)

代码语言:javascript
复制
from chatterbot import ChatBot

bot = ChatBot('my-chat', database_uri='sqlite:///db.sqlite3')

def response_text(sentence):
    temp = bot.get_response(sentence)
    return temp.text

if __name__ == '__main__':
    # bot_response = response_text("你几岁了")
    # print(bot_response)

    # 开始对话
    while True:
        print(response_text(input(">")))

image

小黄鸡语料数据库:

链接:https://pan.baidu.com/s/1bgGlyH4RwiB1UDud1P1DQw 密码:wzny

Snip20191204_47.png

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 利用已经提供好的小中文语料库
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档