python3.8使用aiml总结
生活随笔
收集整理的這篇文章主要介紹了
python3.8使用aiml总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
model_aiml--aiml對話執行主文件
# -*- coding: utf-8 -*- import aiml import sys import osdef 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()總結
以上是生活随笔為你收集整理的python3.8使用aiml总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GitHub 优秀的 Android 开
- 下一篇: 【实践1】Python调用搜狗语音,自制