博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用PyAIML开发简单的对话机器人
阅读量:6574 次
发布时间:2019-06-24

本文共 4478 字,大约阅读时间需要 14 分钟。

  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 . 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. 

...

  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:

  
    
HOW ARE YOU
    
  

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:

$ 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 pythonimport aimlimport sysmybot = 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. 

LOAD AIML B

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 pythonimport aiml# Create a Kernel object.mybot = aiml.Kernel()# Learn startup.xmlmybot.learn('startup.xml')  # Change the current path to your aiml files path# Calling load aiml b for loading all AIML filesmybot.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:

 

以<random>标签为例。<li>标签 is the list can be used within the <random> and <condition> tag sets)

Its purpose is random selection of one of a set of list items.

<random> 

<li>A</li> 
<li>B</li> 
<li>C</li> 
</random>

Say one of A, B or C randomly. 

WHO ARE YOU

 

The <star index="n"/> tag indicates the input text fragment matching the "n" pattern '*'. 

<star index="1"/> yields the first matching text fragment.
<star index="2"/> yields the second matching text fragment.
<star index="3"/> yields the third matching text fragment.
And so on....
The index="n" is optional, and if left off, the index value of "1" is assumed. The tag <star/> is equivilant to <star index="1"/>.

MY NAME IS *
MEET OUR GUEST * AND *

 

参考:

转载地址:http://ugrjo.baihongyu.com/

你可能感兴趣的文章
angularJS表达式详解!
查看>>
jquery的一点点认识
查看>>
localhost或本机ip无法连接数据库问题解决与原因
查看>>
git的基本使用
查看>>
Latent Semantic Analysis (LSA) Tutorial第一部分(转载)
查看>>
【CF311E】biologist
查看>>
将vim打造成python开发工具
查看>>
sql中去掉字段的所有空格
查看>>
添加头像
查看>>
Django 模板层
查看>>
Html5学习进阶一 视频和音频
查看>>
ap.net core 教程(三)
查看>>
【转】虚拟机下安装小红帽Linux9.0图解
查看>>
经验的总结,需要记录。
查看>>
我的家庭私有云计划-21
查看>>
运维人员如何最大限度避免误删除文件(20160627更新)
查看>>
《构建高可用Linux服务器(第二版)》正式发售
查看>>
Nginx upstream的几种分配方式
查看>>
高薪源于专注和极致!
查看>>
Lync Server 2010的部署系列_第三章 证书、架构、DNS规划
查看>>