model_aiml--aiml对话执行主文件
# -*- 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') + '/botdata/alice'
alice_path = '/Users/admin/Desktop/Study_aiml'
# 切换到.aiml所在工作目录
os.chdir(alice_path)
alice = aiml.Kernel()
# 通过std-startup.xml启动aiml
alice.learn("std-startup.xml")
# aiml文件有修改时可以通过load aiml b(在xml中pattern配置)进行修改
alice.respond('LOAD AIML B')
while True:
# 对话接入位置
print(alice.respond(input("Enter your message >> ")))
std-startup.xml--固定写法,可配置多个aiml
<aiml version="1.0.1" encoding="UTF-8">
<!-- std-startup.xml -->
<!-- Category是一个自动的AIML单元 -->
<category>
<!-- Pattern用来匹配用户输入 -->
<!-- 如果用户输入 "LOAD AIML B" -->
<pattern>LOAD AIML B</pattern>
<!-- Template是模式的响应 -->
<!-- 这里学习一个aiml文件 -->
<template>
<learn>basic_chat.aiml</learn>
<learn>stude.aiml</learn>
<!-- 你可以在这里添加更多的aiml文件 -->
<!--<learn>more_aiml.aiml</learn>-->
</template>
</category>
</aiml>
basic_chat.aiml--机器人对话内容储存文件
<aiml version="1.0.1" encoding="UTF-8">
<!-- basic_chat.aiml -->
<category>
<pattern>HELLO</pattern>
<template>
Well, hello!
</template>
</category>
<!--that使用方法,在于承上启下,当前回复内容查找aiml中所有that,有则可以根据输入内容进行pattern匹配,无则进行新一轮问答-->
<category>
<pattern>你是男生吗?</pattern>
<template>
先告诉我你是么?
</template>
</category>
<category>
<pattern>是</pattern>
<that>先告诉我你是么?</that>
<template>
我也是
</template>
</category>
<category>
<pattern>不是</pattern>
<that>先告诉我你是么?</that>
<template>
我也不是
</template>
</category>
<!--srai使用方法:srai会根据整句去查找整个aiml中的pattern,匹配到后template返回-->
<category>
<pattern>张的性别是</pattern>
<template>
男
</template>
</category>
<category>
<pattern>李的性别是</pattern>
<template>
女
</template>
</category>
<category>
<pattern>你知道*的性别是*</pattern>
<template>
<srai><star index="1"/>的性别是</srai>
</template>
</category>
<!--random+li标签随机选取一个进行回复-->
<category>
<pattern>你好</pattern>
<template>
<random>
<li>你好1</li>
<li>你好2</li>
<li>你好3</li>
<li>你好4</li>
<li>你好5</li>
<li>你好6</li>
</random>
</template>
</category>
<!--set,get标签get根据name获取set的值-->
<category>
<pattern>我是*</pattern>
<template>
<set name = "name1"> <star/></set>你好
</template>
</category>
<category>
<pattern>我的姓名是*</pattern>
<template>
你的姓名是<get name = "name1"/>
</template>
</category>
<!--think标签,记录变量值不返回给用户-->
<category>
<pattern>think我是*</pattern>
<template>
<think><set name = "name"> <star/></set></think>你好
</template>
</category>
<category>
<pattern>我的姓名是*</pattern>
<template>
你的姓名是<get name = "name"/>
</template>
</category>
<!--condition使用方法,根据输入信息返回不同的结果-->
<category>
<pattern>我*</pattern>
<template>
<think>
<set name = "state"><star index = "1"/></set>
</think>
<condition name = "state" value = "开心">
我也开心
</condition>
<condition name = "state" value = "不开心">
我也不开心
</condition>
</template>
</category>
<category>
<pattern>WHAT ARE YOU</pattern>
<template>
I'm a bot, silly!
</template>
</category>
</aiml>
stude.aiml --机器人自我学习
<aiml version="1.0.1" encoding="UTF-8">
<!-- std-startup.xml -->
<!-- Category是一个自动的AIML单元 -->
<category>
<!-- Pattern用来匹配用户输入 -->
<!-- 如果用户输入 "LOAD AIML B" -->
<pattern>* 答案是 *</pattern>
<!-- Template是模式的响应 -->
<!-- 这里学习一个aiml文件 -->
<template>
<!--system可以调用python命令,从而往aiml中写入对话-->
<system>python3 learn.py '<star index="1"/>' '<star index="2"/>'</system>
<learn>basic_chat.aiml</learn>
好的我学会了
<!-- 你可以在这里添加更多的aiml文件 -->
<!--<learn>more_aiml.aiml</learn>-->
</template>
</category>
</aiml>
learn.py--写入aiml方法
# -*- coding: utf-8 -*-
import sys
# print(sys.argv)
template = """
<category>
<pattern>{}</pattern>
<template>
{}
</template>
</category>"""
file = open("basic_chat.aiml","rb+")
content = file.read()
# seek定位至aiml中最后一行之前,删除</aiml>
file.seek(-7,1)
file.truncate()
file.close()
file1 = open("basic_chat.aiml","a")
file1.write(template.format(sys.argv[1],sys.argv[2])+"\n"+"</aiml>")
file1.close()