如何编写简单的聊天机器人AI? [英] How can I program a simple chat bot AI?

查看:655
本文介绍了如何编写简单的聊天机器人AI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个机器人,该机器人会问一些简单的问题,并根据答案进行分支.我意识到从人的回应中解析含义将具有挑战性,但是您如何设置该程序来处理对话的状态"?

I want to build a bot that asks someone a few simple questions and branches based on the answer. I realize parsing meaning from the human responses will be challenging, but how do you setup the program to deal with the "state" of the conversation?

这将是人与机器人之间的一对一对话.

It will be a one-to-one conversation between a human and the bot.

推荐答案

您可能想研究马尔可夫链作为bot AI的基础知识.我很久以前写了一些东西(我一点都不引以为豪的代码,并且需要一些mods才能在Python> 1.5上运行),这可能对您来说是一个有用的起点: http://sourceforge.net/projects/苯并/

You probably want to look into Markov Chains as the basics for the bot AI. I wrote something a long time ago (the code to which I'm not proud of at all, and needs some mods to run on Python > 1.5) that may be a useful starting place for you: http://sourceforge.net/projects/benzo/

这是Markov Chain的Python中的一个最小示例,该示例接受来自stdin的输入并根据单词在输入中彼此连续的概率输出文本.它针对IRC风格的聊天记录进行了优化,但是通过它运行任何体面大小的文本都应演示这些概念:

Here's a minimal example in Python of a Markov Chain that accepts input from stdin and outputs text based on the probabilities of words succeeding one another in the input. It's optimized for IRC-style chat logs, but running any decent-sized text through it should demonstrate the concepts:

import random, sys

NONWORD = "\n"
STARTKEY = NONWORD, NONWORD
MAXGEN=1000

class MarkovChainer(object):
    def __init__(self):
        self.state = dict()

    def input(self, input):
        word1, word2 = STARTKEY
        for word3 in input.split():
            self.state.setdefault((word1, word2), list()).append(word3)
            word1, word2 = word2, word3 
        self.state.setdefault((word1, word2), list()).append(NONWORD)

    def output(self):
        output = list()
        word1, word2 = STARTKEY
        for i in range(MAXGEN):
            word3 = random.choice(self.state[(word1,word2)])
            if word3 == NONWORD: break
            output.append(word3)
            word1, word2 = word2, word3
        return " ".join(output)

if __name__ == "__main__":
    c = MarkovChainer()
    c.input(sys.stdin.read())
    print c.output()

从这里插入持久性和IRC库非常容易,并且具有您所讨论的bot类型的基础.

It's pretty easy from here to plug in persistence and an IRC library and have the basis of the type of bot you're talking about.

这篇关于如何编写简单的聊天机器人AI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆