10分钟构建人人都能学会的个性化聊天机器人-使用AIML(王小草博客)
2018-02-01 20:20
447 查看
本文主要介绍什么是aiml, 如何python环境安装aiml,如何使用与制作一个属于自己的聊天机器人
同样可以在 AIML Wikipedia page 了解更多 AIML 的内容
aiml tutorial: https://www.tutorialspoint.com/aiml/aiml_star_tag.htm
pandorabots官网: https://www.pandorabots.com
博客: http://python.jobbole.com/82007/
但是使用以上方式安装对于处理中文有点点难搞,因此也可以下载github上这位大哥的工程:https://github.com/WangXiaoCao/PyAIML
(我在他基础之上做了小改动,主要是去掉了处理输出的中文空格)
标签解释:
因此表现在聊天界面的效果就是:
但是光是以上如此简单的模式,要适应大量的人类语言与句式,显得异常笨重,aiml提供了许多其他tag来帮助人们构建更有效更灵活的问答模式。在介绍这些标签之前,先介绍一下其他文件
解释:我们想要匹配模式load aiml b,然后让它加载我们的aiml大脑作为响应
2.4 启动程序
•
•
•
•
3.2
可以用*表示1个或多个任意字符
*的元素可以取出
有大于一个*时,可根据索引提取
3.3
用法1:
用法2:
3.4
3.5
3.5
不返回给用户的情况下存储下变量
3.6
3.7
3.8
3.9
1 aiml初识
1.1 demo展示
用aiml写了一个简单的微信聊天机器人的demo,暂且叫小草聊天机器人吧哈哈哈,以下为部分聊天截图:(太懒了截图之后补上,我把测试版的聊天机器人放在我个人微信上了,想亲测的可以直接来我个人微信撩它–咳咳,但不准撩主人)1.2 什么是aiml
AIML由Richard Wallace发明。他设计了一个名为 A.L.I.C.E. (Artificial Linguistics Internet Computer Entity 人工语言网计算机实体) 的机器人,并获得了多项人工智能大奖。AIML是一种为了匹配模式和确定响应而进行规则定义的 XML 格式。(摘自网络)1.3 学习资料
初级读物,可翻阅 Alice Bot’s AIML Primer同样可以在 AIML Wikipedia page 了解更多 AIML 的内容
aiml tutorial: https://www.tutorialspoint.com/aiml/aiml_star_tag.htm
pandorabots官网: https://www.pandorabots.com
博客: http://python.jobbole.com/82007/
2 aiml python安装与使用简介
2.1 安装Python aiml库
在终端输入命令:pip install aiml但是使用以上方式安装对于处理中文有点点难搞,因此也可以下载github上这位大哥的工程:https://github.com/WangXiaoCao/PyAIML
(我在他基础之上做了小改动,主要是去掉了处理输出的中文空格)
2.2 .aiml格式介绍
创建一个后缀为.aiml的文件,内容与格式如下:<aiml version="1.0.1" encoding="UTF-8"> <!-- basic_chat.aiml --> <aiml> <category> <pattern>你好</pattern> <template> 嗨,好久不见 </template> </category> <category> <pattern>你是谁</pattern> <template> 我是灰太狼 </template> </category> </aiml>
标签解释:
<aiml>:定义一个aiml文件的开始与结束
<category>:定义一个知识的单元
<pattern>:定义一个模板,来匹配用户可能的输入
<template>:定义根据用户的输入需要返回的回答
因此表现在聊天界面的效果就是:
user:你好 bot:嗨,好久不见 user:你是谁 bot:我是灰太狼
但是光是以上如此简单的模式,要适应大量的人类语言与句式,显得异常笨重,aiml提供了许多其他tag来帮助人们构建更有效更灵活的问答模式。在介绍这些标签之前,先介绍一下其他文件
2.3 std-startup.xml启动文件介绍
在正式构建聊天机器人之前,需要创建一个名为std-startup.xml的启动文件,用于作为加载AIML文件的主入口点<aimlversion="1.0.1"encoding="UTF-8"> <!--std-startup.xml--> <!--<category>作为AIML的原子级单元--> <category> <!--匹配用户输入的模式--> <!--如果用户输入"LOADAIMLB"--> <pattern>LOAD AIML B</pattern> <!--<Template>用来响应模式--> <!--<learn>是一个aiml文件--> <template> <learn>/Users/wangxiaocao/PycharmProjects/wechat_chatbot/resources/basic_chat.aiml</learn> <learn>/Users/wangxiaocao/PycharmProjects/wechat_chatbot/resources/greeting.aiml</learn> <!--在这下面你能添加更多的aiml文件--> <!--<learn>more_aiml.aiml</learn>--> </template> </category> </aiml>
解释:我们想要匹配模式load aiml b,然后让它加载我们的aiml大脑作为响应
2.4 启动程序
# -*- coding: utf-8 -*- import aiml import sys import os def get_module_dir(name): path = getattr(sys.modules[name], '__file__', None) if not path: raise AttributeError('module %s has not attribute __file__' % name) return os.path.dirname(os.path.abspath(path)) alice_path = get_module_dir('aiml') + '/alice' #切换到语料库所在工作目录 os.chdir(alice_path) alice = aiml.Kernel() alice.learn("std-startup.xml") alice.respond('load aiml b') while True: print alice.respond(raw_input("Enter your message >> "))
3 aiml的标签详解
3.1 basic tag
基本标签:在上文中已经介绍了•
<aiml>− defines the beginning and end of a AIML document.
•
<category>− defines the unit of knowledge in Alicebot’s knowledge base.
•
<pattern>− defines the pattern to match what a user may input to an Alicebot.
•
<template>− defines the response of an Alicebot to user’s input.
3.2 <star>
可以用*表示1个或多个任意字符<category> <pattern>晚安*</pattern> <template> 好的,晚安,做个好梦哈~ </template> </category>
user: 晚安啦/晚安哦,宝贝/晚安,亲/晚安晚安... bot: 好的,晚安,做个好梦哈~
*的元素可以取出
<category> <pattern>我是*</pattern> <template> 哈哈,<star/>最近怎么样呀? </template> </category>
有大于一个*时,可根据索引提取
<category> <pattern>*介绍一下*</pattern> <template> <star index = "2"/>是阿里老板 </template> </category>
user: 能介绍一下马云吗 bot: 马云是阿里老板
3.3 <sria>
用法1:<category> <pattern>再见</pattern> <template> 好,回聊哈! </template> </category> <category> <pattern>*不聊*</pattern> <template> <srai>再见</srai> </template> </category>
user: 再见 bot: 好,回聊哈! user: 太晚了,不聊了哦 bot: 好,回聊哈!
用法2:
<category> <pattern>谁是马云</pattern> <template>马云是阿里巴巴的老板</template> </category> <category> <pattern>谁是马化腾</pattern> <template>马化腾是腾讯的老板</template> </category> <category> <pattern>你知道谁是*吗</pattern> <template> 谁是<star/>! <srai>谁是<star/></srai> </template> </category>
user: 谁是马云 bot: 马云是阿里巴巴的老板 user: 你知道谁是马云吗 bot: 马云是阿里巴巴的老板
3.4 <random>
<li>
<category> <pattern>HELLO</pattern> <template> <random> <li>Hi~Dear~.</li> <li>您好呀.</li> <li>在呢在呢.</li> <li>臣妾在,圣上有何指示.</li> <li>嗨,您好,我是王小草机器人,我的主人不在,有什么悄悄话可以和我说哦~</li> </random> </template> </category>
3.5 <set>
<get>
<category> <pattern>我叫*</pattern> <template> <think> <setname="name"> <formal><star/></formal> </set> </think> 哈哈,<getname="name"/>,别来无恙啊 </template> </category> user: 我叫李莫愁 bot: 哈哈,李莫愁,别来无恙啊 <category> <pattern>*我是谁*</pattern> <template> <random> <li>你是<getname="name"/>呀,把你放在心里面</li> <li><getname="name"/>,<getname="name"/>,<getname="name"/>,重要的名字说三遍</li> </random> </template> </category>
user: 还记得我是谁吗? bot: 李莫愁,李莫愁,李莫愁,重要的名字说三遍
3.5 <think>
不返回给用户的情况下存储下变量3.6 <that>
<category> <pattern>没有</pattern> <that>那你有打火机吗</that> <template> <think><setname="p1"><formal>true</formal></set></think> 那你为什么点燃了我的心? </template> </category>
bot: 你有打火机吗? user: 没有 bot: 那你为什么点燃了我的心?
3.7 <condition>
<category> <pattern>ASKUSERAQUESTION</pattern> <template> <condition> <li name="age" value="">小哥哥你多大了?</li> <li name="p1" value="">那你有打火机吗?</li> <li name="p2" value="">你猜我是什么做的?</li> <li name="p3" value="">有没有计划过什么时候结婚?</li> <li name="p4" value="">你知道为什么我的眼睛特别美吗?</li> <li name="p5" value="">给你一样东西你要不要?</li> <li name="p6" value="">哈哈哈,不套路你了,要不咱们聊点别的吧?</li> </condition> </template> </category>
<category> <pattern> HOW ARE YOU FEELING TODAY </pattern> <template> <think><set name = "state"> happy</set></think> <condition name = "state" value = "happy"> I am happy! </condition> <condition name = "state" value = "sad"> I am sad! </condition> </template> </category>
3.8 <topic>
<category> <pattern>*聊*吧</pattern> <template> <think><setname="p6"><formal>true</formal></set></think> <think><setname="topic"><formal><starindex="2"/></formal></set></think> OK,<getname="topic"/>,我喜欢! </template> </category> <topicname="手机"> <category> <pattern>我喜欢苹果</pattern> <template> 我比较喜欢华为,想问苹果的前置摄像头拍出来的到底是什么鬼 </template> </category> </topic> <topicname="水果"> <category> <pattern>我喜欢苹果</pattern> <template> 我比较喜欢芒果,苹果我吃厌了 </template> </category> </topic>
user: 那我们来聊手机吧 bot: OK,手机,我喜欢! user: 我喜欢苹果 bot: 我比较喜欢华为,想问苹果的前置摄像头拍出来的到底是什么鬼 user:我们还是来聊水果吧 bot: OK, 水果,我喜欢! user: 我喜欢苹果 bot: 我比较喜欢芒果,苹果我吃厌了
3.9 <learn>
<category> <pattern>说错*</pattern> <template> 那我应该怎么说 </template> </category> <category> <pattern>这么说不*</pattern> <template> 那我应该怎么说 </template> </category> <category> <pattern>这样说不*</pattern> <template> 那我应该怎么说 </template> </category> <category> <pattern>你应该说*</pattern> <that>那我应该怎么说</that> <template> <srai><inputindex="3"/>XLEARNREPLY<starindex="1"/></srai> </template> </category> <category> <pattern>应该说*</pattern> <that>那我应该怎么说</that> <template> <srai><inputindex="3"/>XLEARNREPLY<starindex="1"/></srai> </template> </category> <category> <pattern>*XLEARNREPLY*</pattern> <template> <system>pythonlearn.py'<starindex="1"/>''<starindex="2"/>'</system> <learn>auto-gen.aiml</learn> 好的我学会了,你可以再问我试试. </template> </category>
3.10 其他
<date>返回系统时间
<category> <pattern>几点了</pattern> <template> 现在是:<system>date</system> </template> </category>
<formal>格式化
<category> <pattern>FORMAL *</pattern> <template><formal><star /></formal></template> </category>
<gender>性别转换
<category> <pattern>DOES IT BELONG TO *</pattern> <template>No, it belongs to <gender><star/></gender></template> </category>
<input>获取用户输入的语句
<category> <pattern>你能跟我说一样的话吗</pattern> <template><input /></template> </category> <category> <pattern>你还记得我上一句话吗</pattern> <template><input index = "2"/></template> </category>
<size>获取规则的数目
<category> <pattern>你懂多少 *</pattern> <template> 我懂<size />条规则. </template> </category>
<version>
<category> <pattern>你的版本 *</pattern> <template> 我已经<version />了. </template> </category>
user: 几点了 bot: 现在是: Thu Feb 1 20:15:25 CST 2018 user: FORMAL wang xiao cao bot: Wang Xiao Cao user: DOES IT BELONG TO him? bot: No, it belongs to her user: 你能跟我说一样的话吗 bot: 你能跟我说一样的话吗 user: 你还记得我上一句话吗 bot: 你能跟我说一样的话吗 user: 你懂多少问答模式了? bot: 我懂 70 条规则. user: 你的版本是什么 bot: 我已经 PyAIML 0.8.6 了.
相关文章推荐
- 使用GitHub Pages + Hexo 快速搭建个性化博客(三)-更换博客主题
- 使用ASP.NET 构建 Web 应用程序快速入门-8小时的免费培训视频 - Scott Hanselman的中文博客【转载】
- 使用GitHub Pages + Hexo 快速搭建个性化博客(七)-编辑博文
- 使用QC和QTP构建自动测试体系 [ 光影人像 东海陈光剑 的博客 ]
- 10分钟学会如何使用Shiro
- qmake用户手册——10分钟学会使用qmake
- Python使用AIML搭建聊天机器人
- UWP开发入门(十九)——10分钟学会在VS2015中使用Git
- 使用GitHub Pages + Hexo 快速搭建个性化博客(二)-购买并解析域名
- 10分钟学会使用qmake
- 用 Flask 来写个轻博客 (34) — 使用 Flask-RESTful 来构建 RESTful API 之三
- 使用WordPress和暴风云平台构建自己的视频博客
- C# 使用AIML 弄 聊天机器人
- 使用github+Hexo人人都能拥有一个美美的博客
- 用 Flask 来写个轻博客 (36) — 使用 Flask-RESTful 来构建 RESTful API 之五
- 使用Vue.js和Laravel构建的开源博客。
- 10分钟学会使用qmake
- 使用GitHub Pages + Hexo 快速搭建个性化博客
- 使用.net core ABP和Angular模板构建博客管理系统(实现自己的业务逻辑)
- 使用GitHub pages + Hexo 快速搭建个性化博客(一)-部署Hexo默认blog模板至远程GitHub