zoukankan      html  css  js  c++  java
  • Python微信公众号后台开发<005>:集成智能聊天机器人​

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

    一、前述

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

    二、具体

    1、安装

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

    pip install chatterbot

    2、流程

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

      

    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、代码

    计算模式

    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("
    ")
    
    # 回答和时间相关的问题
    question = "What time is it?"
    print(question)
    response = bot.get_response(question)
    print(response)
    

     

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

    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("你是谁"))
    
     

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

    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(">")))
    

    小黄鸡语料数据库:

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

  • 相关阅读:
    android 如何在手机上 安装两个同样的app
    Ubuntu 16.04 安装Gitlab
    VScode开发C++常用插件
    过滤text字段为空的数据
    ES标签搜索并解决评分排序问题
    3
    2
    1
    9
    Maven3种打包方式
  • 原文地址:https://www.cnblogs.com/blogs/p/12174766.html
Copyright © 2011-2022 走看看