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

  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

时间: 2024-08-26 05:36:36

用PyAIML开发简单的对话机器人的相关文章

最简单的聊天机器人、人机互动对话代码。(1)

工具:eclipse;JAVA JDK; 语言:java 作者:蛋白 时间:2016.11.23 1 import java.util.Scanner; 2 3 //下面是一个最简单的人机互动对话代码,最简单的聊天机器人. 4 public class a { 5 public static void main(String[] args) { 6 Scanner scan=new Scanner(System.in); 7 String talk = scan.next();//把从控制台输入

人工智能初体验(二):开发简单的图灵智能聊天工具

前言:这里为了有更好的个性化设置,因此我选择了图灵机器人(http://www.tuling123.com/)的接口,使用方法跟上一篇中使用百度的接口是差不多的. 注:文末有打包好的小软件和完整源代码的下载链接 一 API Key申请 申请地址:http://www.tuling123.com/ 中间的注册登录过程不说,最后把API Key值记录下来 二 核心功能开发 这个小项目的目录结构: 核心功能文件TuringRobot.java,代码很简单,一看就明白,代码如下: package act

与一线Linux嵌入式开发工程师的对话

转:与一线Linux嵌入式开发工程师的对话 陈工程师一直做Linux的嵌入式开发,作为在开发一线的工程师,他对很多问题的看法可能更切合实际需求,于是,通过邮件,就嵌入式开发方面的问题,请他谈了一下自己的看法: 问:关于嵌入式开发,我们准备给同学们讲解一些入门知识,从你一线开发经验来说,给我们一些建议: 陈工回答: 对于嵌入式Linux入门,如果有一定基础,可以从驱动开始:如果没有基础,我个人建议还是从应用程序开始.因为从应用程序开始是最容易的,也是 最直观的.而驱动程序运行在内核态,驱动本身的结

(推荐)叮当——中文语音对话机器人

叮当是一款可以工作在 Raspberry Pi 上的开源中文语音对话机器人/智能音箱项目,目的是让中国的Hacker们也能快速打造个性化的智能音箱. github地址:https://github.com/wzpan/dingdang-robot 主要是github上已经提供了打包好的镜像文件,只需少量操作,烧录进入sd卡即可使用体验,推荐给大家! 特性 叮当包括以下诸多特性: 模块化.功能插件.语音识别.语音合成.对话机器人都做到了高度模块化,第三方插件单独维护,方便继承和开发自己的插件. 微

智能对话机器人实战视频教程下载

智能对话机器人实战视频教程网盘地址:https://pan.baidu.com/s/1Om9-VgBSLU7aY75B0d3YIg 提取码: e46v备用地址(腾讯微云):https://share.weiyun.com/5EAhsZ0 密码:xz9swp备用地址(天翼云):https://cloud.189.cn/t/yABvaybEFfEr (访问码:8464) 一.课程特色:1.案例驱动:三大智能对话机器人需求场景,三个完整案例: 2.源码操作:内含完整程序源码和数据集: 3.实战指引:覆

监控开发之如何开发简单高性能扩展性强的监控系统

关于如何快速开发一套属于自己的运维监控系统. 记得刚入行的时候,对于监控方面,用的是nagios和cacti,现在大多数中小公司好多都开始搞zabbix了,熟悉zabbix的人,知道他的性能的瓶颈其实主要还是在数据库上,尤其是zabbx_server 针对数据库一些不高效逻辑的查询和写入引起的. 同事针对zabbix开发也搞了半年了,和他交流了下,有很多的想法. zabbix 有些查询完全可以从缓存里面取值,比如redis.memcached,不用非要从数据库里面来搞个消耗性能的大查询,有些监控

基于TINY4412的Andorid开发-------简单的LED灯控制【转】

本文转载自:http://www.cnblogs.com/pengdonglin137/p/3857724.html 基于TINY4412的Andorid开发-------简单的LED灯控制 阅读目录(Content) 一.编写驱动程序 二.编写代码测试驱动程序 三.编写HAL代码 四.编写Framework代码 五.编写JNI代码 六.编写App 参考资料: <Andriod系统源代码情景分析> <嵌入式Linux系统开发完全手册_基于4412_上册> 作者:彭东林 邮箱:[em

Python开发简单记事本

摘要: 本文是使用Python,结合Tkinter开发简单记事本. 本文的操作环境:ubuntu,Python2.7,采用的是Pycharm进行代码编辑,个人很喜欢它的代码自动补齐功能. 最近很想对python加深学习一下,同时也是想试着做一些东西,今天使用python,结合Tkinter来做一个简单的跨平台记事本.最终实现的记事本如下,也算是麻雀虽小,五脏俱全了,之后也是会继续完善的: 如上图,我们可以看到这个记事本主要分为三个模块:文件,编辑和关于,结合我自身的习惯外加四个toolbar:新

【spark系列3】spark开发简单指南

分布式数据集创建之textFile 文本文件的RDDs能够通过SparkContext的textFile方法创建,该方法接受文件的URI地址(或者机器上的文件本地路径,或者一个hdfs://, sdn://,kfs://,其他URI).这里是一个调用样例:scala> val distFile = sc.textFile("data.txt")distFile: spark.RDD[String] = [email protected] 分布式数据集操作之转换和动作 分布式数据集