本文主要介紹什麼是aiml, 如何python環境安裝aiml,如何使用與製作一個屬於自己的聊天機器人
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 了.