【翻译】用AIML实现的Python人工智能聊天机器人

前言

用python的AIML包很容易就能写一个人工智能聊天机器人。 AIML是Artificial Intelligence Markup Language的简写, 但它只是一个简单的XML。 下面的代码例子会带你写一个你自己的Python人工智能聊天机器人。

什么是AIML?

AIML是Richard Wallace开发的。 他开发了一个叫A.L.I.C.E(Artificial Linguistics Internet Computer Entity)的机器人并且赢了几个人工智能的奖项。 有趣的是, 其中一个图灵测试是让一个人在文本界面跟一个机器人聊几分钟,看看人们是否认为它是个人类。 AIML是一种定义了匹配模式和决定响应的规则的一种XML。

要看完整的AIML入门,可以看一下 Alice Bot's AIML Primer.你可以在AIML wiki页学更多关于AIML的知识并知道它能做什么。 我们先写一些AIML文件并用Python给它一点生命。

写标准启动文件

标准是写一个叫std-startup.xml的启动文件作为载入AIML文件的主入口点。 这个例子里我们会写一个可以匹配一个模式并做一个动作的基本文件。 我们想要匹配模式load aiml b, 然后让它载入我们的aiml大脑。我们会花一分钟写一个basic_chat aiml文件。

<aiml version="1.0.1" encoding="UTF-8">
    <!-- std-startup.xml -->

    <!-- Category is an atomic AIML unit -->
    <category>

        <!-- Pattern to match in user input -->
        <!-- If user enters "LOAD AIML B" -->
        <pattern>LOAD AIML B</pattern>

        <!-- Template is the response to the pattern -->
        <!-- This learn an aiml file -->
        <template>
            <learn>basic_chat.aiml</learn>
            <!-- You can add more aiml files here -->
            <!--<learn>more_aiml.aiml</learn>-->
        </template>
        
    </category>

</aiml>

写AIML文件

上面我们写了一个只处理一种模式的AIML文件,load aiml b。当我们输入那条命令给机器人,它会加载basic_chat.aiml。当我们没写这个文件的时候是不会工作的。这里是你可以放到basic_chat.aiml的内容。我们会匹配两个基本模式和响应。

<aiml version="1.0.1" encoding="UTF-8">
<!-- basic_chat.aiml -->

    <category>
        <pattern>HELLO</pattern>
        <template>
            Well, hello!
        </template>
    </category>
    
    <category>
        <pattern>WHAT ARE YOU</pattern>
        <template>
            I'm a bot, silly!
        </template>
    </category>
    
</aiml>

随机回复

我们也可以加一些随机回复。这条在它收到一个以“One time I"开始的消息时会随机回复。 *是一个占位符代表能匹配任何字符。

<category>
    <pattern>ONE TIME I *</pattern>
    <template>
        <random>
            <li>Go on.</li>
            <li>How old are you?</li>
            <li>Be more specific.</li>
            <li>I did not know that.</li>
            <li>Are you telling the truth?</li>
            <li>I don't know what that means.</li>
            <li>Try to tell me that another way.</li>
            <li>Are you talking about an animal, vegetable or mineral?</li>
            <li>What is it?</li>
        </random>
    </template>
</category>

用现成的AIML文件

写自己的AIML文件很有意思,但会是个很大的工作量。我想它需要大约一万的模式才能看起来真实一点。幸好,ALICE基金提供很多免费的AIML文件。可以在Alice Bot网站看看AIML文件。那有个叫std-65-percent.xml文件包括了常规65%的场景。那也有一个可以让你玩21点(BlackJack)的机器人。

写Python

到目前为止,所有的东西都是AIML XML文件。这些是可以构造机器人大脑的重要部分,但这些只是一些信息。机器人需要变活。你需要用一些语言来实现AIML规范,但一些好心人已经用Python做了。

先用pip装aiml包。

pip install aiml

记住aiml包只能在Python 2下用。 Python 3可以用GitHub上的Py3kAiml替代。

简单Python程序

这是我们能启动的最简单的程序。它建一个aiml对象,学习启动文件,并加载剩下的aiml文件。这之后,就可以聊天了,然后我们进入一个无限循环并持续让用户输入消息。你需要输入一个机器人能识别的模式。模式识别依赖于你之前加载的AIML文件。

我们写个单独的启动文件,这样我们可以不用动程序源码加载更多的aiml文件。我们可以在启动xml文件加更多的文件来学习。

import aiml

# Create the kernel and learn AIML files
kernel = aiml.Kernel()
kernel.learn("std-startup.xml")
kernel.respond("load aiml b")

# Press CTRL-C to break this loop
while True:
    print kernel.respond(raw_input("Enter your message >> "))

加速大脑启动

当你有很多AIML文件,这需要学很长时间。这就要靠机器人大脑文件了。在机器人学习了所有的AIML文件后并可以直接把大脑存到一个文件里,这样在下次启动时就可以直接加速。

import aiml
import os

kernel = aiml.Kernel()

if os.path.isfile("bot_brain.brn"):
    kernel.bootstrap(brainFile = "bot_brain.brn")
else:
    kernel.bootstrap(learnFiles = "std-startup.xml", commands = "load aiml b")
    kernel.saveBrain("bot_brain.brn")

# kernel now ready for use
while True:
    print kernel.respond(raw_input("Enter your message >> "))

运行中重载AIML

你可以在运行时发重载消息给机器人来重载AIML文件。记住如果你使用了之前写的大脑方法,重载不会节省刚对大脑做的改动。你需要删除大脑文件一遍下次启动时可以重建,或者你需要改代码让机器人在下次重载后的一个时间点来保存大脑。看下节写Python命令给机器人。

load aiml b

加Python命令

如果你要给你的机器人加一些命令来运行Python函数,那你需要捕获机器人的输入信息并在发给kernel.respond()前处理它。上个例子我们从raw_input得到了用户的输入。我们可以从很多地方得到输入。比如一个TCP socket,或一个语音转文本的源。在它发给AIML前处理它。你需要在某些消息时跳过AIML。

while True:
    message = raw_input("Enter your message to the bot: ")
    if message == "quit":
        exit()
    elif message == "save":
        kernel.saveBrain("bot_brain.brn")
    else:
        bot_response = kernel.respond(message)
        # Do something with bot_response

会话和断言

通过一个会话,AIML可以区分不同人的不同的对话。例如,如果一个人告诉机器人她的名字是Alice,另一个人告诉机器人他的名字是Bob,机器人可以区分他们。为了指定你在用哪个会话你可以传给respond()第二个参数。

sessionId = 12345
kernel.respond(raw_input(">>>"), sessionId)

这样每个客户都有个性化的聊天,很好。你可以生成你自己的session id(会话id)并跟踪他们。记住保存大脑文件并不会保存所有会话值。

sessionId = 12345

# Get session info as dictionary. Contains the input
# and output history as well as any predicates known
sessionData = kernel.getSessionData(sessionId)

# Each session ID needs to be a unique value
# The predicate name is the name of something/someone
# that the bot knows about in your session with the bot
# The bot might know you as "Billy" and that your "dog" is named "Brandy"
kernel.setPredicate("dog", "Brandy", sessionId)
clients_dogs_name = kernel.getPredicate("dog", sessionId)

kernel.setBotPredicate("hometown", "127.0.0.1")
bot_hometown = kernel.getBotPredicate("hometown")

在AIML中我们可以在模板中设置response来设置断言。

<aiml version="1.0.1" encoding="UTF-8">
   <category>
      <pattern>MY DOGS NAME IS *</pattern>
      <template>
         That is interesting that you have a dog named <set name="dog"><star/></set>
      </template>  
   </category>  
   <category>
      <pattern>WHAT IS MY DOGS NAME</pattern>
      <template>
         Your dog's name is <get name="dog"/>.
      </template>  
   </category>  
</aiml>

用上面的AIML你可以告诉机器人:

My dogs name is Max

然后机器人会回复:

That is interesting that you have a dog named Max

如果你问:

What is my dogs name?

机器人会回答:

Your dog's name is Max.

更多资料

AIML Tag Reference Table

原文:http://www.devdungeon.com/content/ai-chat-bot-python-aiml


本文来自微信平台「麦芽面包」
微信公众号「darkjune_think」转载请注明。
微信扫一扫关注公众号。

posted @ 2016-12-22 22:37  祝坤荣  阅读(2139)  评论(0编辑  收藏  举报