ChatterBot聊天机器人呢结构(五):ChatterBot对话流程

原文地址: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.go2coding.com/

更多教程:go2coding

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
# 说明 该库是对目前市面上已有的开源中文聊天语料的搜集和系统化整理工作 该库搜集了包含 - chatterbot - 豆瓣多轮 - PTT八卦语料 - 青云语料 - 电视剧对白语料 - 贴吧论坛回帖语料 - 微博语料 - 小黄鸡语料 共8个公开闲聊常用语料和短信,白鹭时代问答等语料。 并对8个常见语料的数据进行了统一化规整和处理,达到直接可以粗略使用的目的。 **使用该项目,即可对所有的聊天语料进行一次性的处理和统一下载,不需要到处自己去搜集下载和分别处理各种不同的格式。* # 环境 python3 # 处理过程 将各个来源的语料按照其原格式进行提取,提取后进行繁体字转换,然后统一变成一轮一轮的对话。 # 使用方法 将解压后的raw_chat_corpus文件夹放到当前目录下 目录结构为 ``` raw_chat_corpus -- language -- process_pipelines -- raw_chat_corpus ---- chatterbot-1k ---- douban-multiturn-100w ---- .... -- main.py -- ... ``` 执行命令即可 ```bash python main.py ``` 或者 ```bash python3 main.py ``` # 生成结果 每个来源的语料分别生成一个独立的*.tsv文件,都放在新生成的clean_chat_corpus文件夹下。 生成结果格式为 tsv格式,每行是一个样本,先是query,再是answer ``` query \t answer ``` # 结果的使用 这个就根据每个人不同的情况自主使用即可 个人对于聊天机器人方向实践也不是很多,以下一篇之前写的知乎专栏供参考 **《从产品完整性的角度浅谈chatbot》** 文章粗略讲解了如下一些方面,介绍了聊天机器人在实际产品化过程中可能遇到的问题和解决办法。 1. chatbot自身人格的设置 1. 产品上线需要考虑的敏感词处理 1. 文本检索模型的使用 1. 文本生成模型的使用 1. 回答打分机制 1. 万能回答的使用策略 1. 多媒体消息的处理 1. 产品模型部署的问题 # 版权说明 本项目为非商业项目,为纯搜集和汇总资料,如有侵权,请在issue下留言。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

go2coding

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

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

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

打赏作者

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

抵扣说明:

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

余额充值