原文地址:http://www.bugingcode.com/blog/ChatterBot_Dialogue_process.html

创建机器人

部署机器人的各种属性,根据前面的章节里聊天机器人的各种属性,对聊天机器人进行相应的配置,创建一个符合自己的机器人。

bot = ChatBot(
'Default Response Example Bot',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch'
},
{
'import_path': 'chatterbot.logic.LowConfidenceAdapter',
'threshold': 0.65,
'default_response': 'I am sorry, but I do not understand.'
}
],
trainer='chatterbot.trainers.ListTrainer'
)

logic_adapters

logic_adapters:用来设置所选择的算法,这里选择的是chatterbot.logic.BestMatch,也就是最匹配方式,从训练的对话中找到最相识的语句,根据对话,提供回答。

trainer

trainer:选择的是chatterbot.trainers.ListTrainer

在trainer中,决定选择哪种构造方式来创建上下文的关系。

def train(self, conversation):
"""
Train the chat bot based on the provided list of
statements that represents a single conversation.
"""
previous_statement_text = None for conversation_count, text in enumerate(conversation):
print_progress_bar("List Trainer", conversation_count + 1, len(conversation)) statement = self.get_or_create(text) if previous_statement_text:
statement.add_response(
Response(previous_statement_text)
) previous_statement_text = statement.text
self.storage.update(statement)

在ListTrainer中,用上下句来构建一个statement ,statement相当于存储了一个上下对话的关系,在查找的时候,先找到最合适的上文,下文就是答案了。这就是一个训练的过程,训练的这一过程,主要是在构建statement,并把statement放到storage中。

storage_adapter

storage_adapter有几种可选的方案chatterbot.storage.SQLStorageAdapter,MongoDatabaseAdapter,存储之前训练的statement,把statement存储在数据库中,默认的数据库选择的是本地的sqlite3。

训练机器人

把语料准备好,就聊天机器人进行训练,语料的来源比较重要,像之前的小黄鸭语料的来源,主要是来源于众包,用户会交小黄鸭怎么去回答问题,语料是重要的一种选择,一个语料的质量决定了聊天机器人的可玩性。

训练的过程,就是一个建立statement并存储的过程,代码在ListTrainer中都有详细的体现。

bot.train([
'How can I help you?',
'I want to create a chat bot',
'Have you read the documentation?',
'No, I have not',
'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'
])

产生答案

聊天机器人主要的过程是产生答案的过程,而答案的选择最关键的就是算法的实现,之前有介绍过,可玩性比较高的聊天机器人必须拥有不同的算法,对不同的聊天内容给出不一样的答案,根据输入选择最合适的算法,产生最好的答案。在机器人对话中,最常见的问题是一些生活的问题,比如,天气,时间,笑话等,根据问题,选择最匹配的算法,给出精彩的答案。

response = bot.get_response('How do I make an omelette?')

get_response的过程

采用的是ChatBot的方法,一开始先得到输入,并对数据进行过滤,在根据输入数据选择算法,得出答案。

def get_response(self, input_item, session_id=None):
"""
Return the bot's response based on the input. :param input_item: An input value.
:returns: A response to the input.
:rtype: Statement
"""
if not session_id:
session_id = str(self.default_session.uuid) input_statement = self.input.process_input_statement(input_item) # Preprocess the input statement
for preprocessor in self.preprocessors:
input_statement = preprocessor(self, input_statement) statement, response = self.generate_response(input_statement, session_id) # Learn that the user's input was a valid response to the chat bot's previous output
previous_statement = self.conversation_sessions.get(
session_id
).conversation.get_last_response_statement()
self.learn_response(statement, previous_statement) self.conversation_sessions.update(session_id, (statement, response, )) # Process the response output with the output adapter
return self.output.process_response(response, session_id)

算法是如何进行选择的呢?

在multi_adapter.py 算法选择中,遍历了所有我们已经选择的算法,算法通过 can_process 进行选择,对输入生成的statement 进行匹配,并通过confidence来进行评分,而应该还可以进行扩展,通过不同的得分,来选择算法,最佳匹配。

def process(self, statement):
"""
Returns the output of a selection of logic adapters
for a given input statement. :param statement: The input statement to be processed.
"""
results = []
result = None
max_confidence = -1 for adapter in self.get_adapters():
if adapter.can_process(statement): output = adapter.process(statement) if type(output) == tuple:
warnings.warn(
'{} returned two values when just a Statement object was expected. '
'You should update your logic adapter to return just the Statement object. '
'Make sure that statement.confidence is being set.'.format(adapter.class_name),
DeprecationWarning
)
output = output[1] results.append((output.confidence, output, )) self.logger.info(
'{} selected "{}" as a response with a confidence of {}'.format(
adapter.class_name, output.text, output.confidence
)
) if output.confidence > max_confidence:
result = output
max_confidence = output.confidence
else:
self.logger.info(
'Not processing the statement using {}'.format(adapter.class_name)
) # If multiple adapters agree on the same statement,
# then that statement is more likely to be the correct response
if len(results) >= 3:
statements = [s[1] for s in results]
count = Counter(statements)
most_common = count.most_common()
if most_common[0][1] > 1:
result = most_common[0][0]
max_confidence = self.get_greatest_confidence(result, results) result.confidence = max_confidence
return result

ChatterBot的架构和流程基本清楚以后,就是对ChatterBot的扩展,一个好的ChatterBot聊天机器人,还有很多需要完成的地方,比如多轮对话,

我:天气如何?

机器人:你在位置在那里?

我:厦门

机器人:多云转晴,32摄氏度

转载请标明来之:http://www.bugingcode.com/

更多教程:阿猫学编程

ChatterBot聊天机器人呢结构(五):ChatterBot对话流程的更多相关文章

  1. 【自然语言处理篇】--Chatterbot聊天机器人

    一.前述 ChatterBot是一个基于机器学习的聊天机器人引擎,构建在python上,主要特点是可以自可以从已有的对话中进行学(jiyi)习(pipei). 二.具体 1.安装 是的,安装超级简单, ...

  2. vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)

    使用Vue2进行的仿手机QQ的webapp的制作,作品由个人独立开发,源码中进行了详细的注释. 由于自己也是初学Vue2,所以注释写的不够精简,请见谅. 项目地址 https://github.com ...

  3. 深度学习项目——基于循环神经网络(RNN)的智能聊天机器人系统

    基于循环神经网络(RNN)的智能聊天机器人系统 本设计研究智能聊天机器人技术,基于循环神经网络构建了一套智能聊天机器人系统,系统将由以下几个部分构成:制作问答聊天数据集.RNN神经网络搭建.seq2s ...

  4. NLP(十五) 聊天机器人

    对话引擎 1.了解目标用户 2.理解用于沟通得语言 3.了解用户的意图 4.应答用户,并给出进一步线索 NLTK中的引擎 eliza,iesha,rude,suntsu,zen import nltk ...

  5. 计算机网络课设之基于UDP协议的简易聊天机器人

    前言:2017年6月份计算机网络的课设任务,在同学的帮助和自学下基本搞懂了,基于UDP协议的基本聊天的实现方法.实现起来很简单,原理也很简单,主要是由于老师必须要求使用C语言来写,所以特别麻烦,而且C ...

  6. AI中台——智能聊天机器人平台的架构与应用(分享实录)

    内容来源:宜信技术学院第3期技术沙龙-线上直播|AI中台——智能聊天机器人平台 主讲人:宜信科技中心AI中台团队负责人王东 导读:随着“中台”战略的提出,目前宜信中台建设在思想理念及架构设计上都已经取 ...

  7. 【Python撩妹合集】微信聊天机器人,推送天气早报、睡前故事、精美图片分享

    福利时间,福利时间,福利时间 如果你还在为不知道怎么撩妹而烦恼,不知道怎么勾搭小仙女而困惑,又或者不知道怎么讨女朋友欢心而长吁短叹. 那么不要犹豫徘徊,往下看.接下来我会分享怎么使用 Python 实 ...

  8. 智能聊天机器人——基于RASA搭建

    前言: 最近了解了一下Rasa,阅读了一下官方文档,初步搭建了一个聊天机器人. 官方文档:https://rasa.com/docs/ 搭建的chatbot项目地址: https://github.c ...

  9. 基于PaddlePaddle的语义匹配模型DAM,让聊天机器人实现完美回复 |

    来源商业新知网,原标题:让聊天机器人完美回复 | 基于PaddlePaddle的语义匹配模型DAM 语义匹配 语义匹配是NLP的一项重要应用.无论是问答系统.对话系统还是智能客服,都可以认为是问题和回 ...

随机推荐

  1. Disruptor的简单介绍与应用

    前言 最近工作比较忙,在工作项目中,看了很多人都自己实现了一套数据任务处理机制,个人感觉有点乱,且也方便他人的后续维护,所以想到了一种数据处理模式,即生产者.缓冲队列.消费者的模式来统一大家的实现逻辑 ...

  2. Insulator|enhancer|LCR|EKLF|CTCF|调控基因印记| A-USF|HATs|ChIP|Chip-seq|PAGE|

    表观遗传学 转录因子 基本转录因子:TFIID.A.B.F.E.H. Pol II… 基转录因子具有稳定作用 组织特异性转录因子:GATA.EKLF.Bcl11A… 特异性是在特定组织中的细胞中时与细 ...

  3. ZJNU 2136 - 会长的正方形

    对于n*m网格 取min(n,m)作为最大的正方形边长 则答案可以表示成 s=1~min(n,m) 对于一个s*s的正方形 用oblq数组储存有多少四个角都在这个正方形边上的正方形 以4*4为例 除了 ...

  4. [Scoi2016]背单词(trie+贪心)

    题意:重新解释一下题意吧(题意晦涩难懂) 给定n个单词,你可以按照顺序学习,当学习这一单词时,这个单词是第x个要学习的单词,需要的代价分三类: 1.若存在其他单词是其后缀没被学习,则代价为n2 2.若 ...

  5. httpsqs 源码修改(内部自动复制多队列)

    /* HTTP Simple Queue Service - httpsqs v1.7 Author: Zhang Yan (http://blog.s135.com), E-mail: net@s1 ...

  6. C++ malloc函数

    malloc的全称是memory allocation,中文叫动态内存分配,用于申请一块连续的指定大小的内存块区域以void*类型返回分配的内存区域地址,当无法知道内存具体位置的时候,想要绑定真正的内 ...

  7. 一个搜索框的小demo

    一.实时按照输入的搜索值显示与其匹配的内容,隐藏其它内容 <%@ Page Language="C#" AutoEventWireup="true" Co ...

  8. Serverless 基本概念入门

    从行业趋势看,Serverless 是云计算必经的一场革命 2019 年,Serverless 被 Gartner 称为最有潜力的云计算技术发展方向,并被赋予是必然性的发展趋势.Serverless ...

  9. PAT甲级——1002 A+B for Polynomials

    PATA1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomia ...

  10. RAID和LVM

    EXT家族支持度最广,但创建文件系统慢修复慢存储容量有限 XFS同样是日志文件系统:容量大,支持大存储高性能,创建/修复文件系统快inode与block都是系统需要用到时,才动态配置产生 基本分区(静 ...