码迷,mamicode.com
首页 > 其他好文 > 详细

用PyAIML开发简单的对话机器人

时间:2016-10-24 14:02:29      阅读:438      评论:0      收藏:0      [点我收藏+]

标签:sys   question   intro   tail   name   href   osi   current   response   

  AIML files are a subset of Extensible Mark-up Language (XML) that can store different text patterns in the form of tags. AIML was developed by the Alicebot free software community. AIML is mainly used to implement Chatbots, a natural language software agent in which a user can ask questions to the robot and it can give an intelligent reply.

The <aiml> tag: Each AIML code begins with this tag and is closed using the </aiml> tag. This tag also consists of attributes such as the version and encoding scheme of the file. 

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

  The <category> tag: The basic knowledge blocks of AIML are called categories. Each category block consists of two sections. One is the user input in the form of a sentence and the other is a corresponding response to user input which comes from robot. The category tag is represented using the opening <category> tag and the closing tag is represented using the </category> tag. These categories must be inside the <aiml> and </aiml> tags. The category tags consist of two tags, namely, the <pattern> tag and the <template> tag. The input given by users is inside the <pattern> tag and the answers are in the <template> tag. For example, look at this following conversation:

  User: How are you?

  Robot: I am fine.

  In this conversation, the user dialog will be in the <pattern> tag and the robot‘s response will be in the <template> tag. The following code shows the representation of the preceding dialogs in the AIML format:

<aiml version="1.0.1" encoding="UTF-8">
  <category>
    <pattern> HOW ARE YOU </pattern>
    <template> I AM FINE </template>
  </category>
</aiml>

We need to save this file in the .aiml or .xml format. For example, save the code with the name "sample.aiml".

Introduction to PyAIML
PyAIML is an open source Python AIML interpreter written completely in pure Python without using any third-party dependencies.

Installing PyAIML from source code:

https://github.com/DYFeng/pyaiml

$ sudo  python  setup.py  install 

Install the aiml package with pip:

pip install aiml

 

Loading a single AIML file from the command-line argument(载入单个aiml文件
We can load a single AIML file using the following code:

#!/usr/bin/env python
import aiml
import sys
mybot = aiml.Kernel()
mybot.learn(sys.argv[1])

while True:
    print mybot.respond(raw_input("Enter input >")) 

Execute the code using the following command:
python test.py sample.aiml
It will give you the following result:

技术分享

 

Loading AIML files into memory(载入多个aiml文件)
If you want to learn more than one AIML, it‘s better to use an XML file, for example, the startup.xml file can load all other AIML files. 

<aiml version="1.0">
    <category>
        <pattern>LOAD AIML B</pattern>
        <template>
            <!-- Load standard AIML set -->
            <learn>*.aiml</learn>
        </template>
    </category>
</aiml>

The preceding XML file will learn all the AIML files when we call the LOAD AIML B pattern. The following code will load all the AIML files into memory:

#!/usr/bin/env python
import aiml

# Create a Kernel object.
mybot = aiml.Kernel()

# Learn startup.xml
mybot.learn(startup.xml)  # Change the current path to your aiml files path

# Calling load aiml b for loading all AIML files
mybot.respond(load aiml b)

# Enter the main input/output loop.
print "\nINTERACTIVE MODE (ctrl-c to exit)"
while True:
    print mybot.respond(raw_input("Enter input >"))

You will get the following output:

技术分享

 

参考:

http://python.jobbole.com/82007/

http://www.jb51.net/article/78789.htm

http://www.alicebot.org/aiml.html

http://blog.csdn.net/aofengdaxia/article/details/18364365

用PyAIML开发简单的对话机器人

标签:sys   question   intro   tail   name   href   osi   current   response   

原文地址:http://www.cnblogs.com/21207-iHome/p/5991934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!