python3.8使用aiml总结
生活随笔
收集整理的這篇文章主要介紹了
python3.8使用aiml总结
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
model_aiml--aiml對(duì)話執(zhí)行主文件
# -*- 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啟動(dòng)aiml alice.learn("std-startup.xml") # aiml文件有修改時(shí)可以通過load aiml b(在xml中pattern配置)進(jìn)行修改 alice.respond('LOAD AIML B')while True:# 對(duì)話接入位置print(alice.respond(input("Enter your message >> "))) std-startup.xml--固定寫法,可配置多個(gè)aiml <aiml version="1.0.1" encoding="UTF-8"><!-- std-startup.xml --><!-- Category是一個(gè)自動(dòng)的AIML單元 --><category><!-- Pattern用來(lái)匹配用戶輸入 --><!-- 如果用戶輸入 "LOAD AIML B" --><pattern>LOAD AIML B</pattern><!-- Template是模式的響應(yīng) --><!-- 這里學(xué)習(xí)一個(gè)aiml文件 --><template><learn>basic_chat.aiml</learn><learn>stude.aiml</learn><!-- 你可以在這里添加更多的aiml文件 --><!--<learn>more_aiml.aiml</learn>--></template></category></aiml>basic_chat.aiml--機(jī)器人對(duì)話內(nèi)容儲(chǔ)存文件
<aiml version="1.0.1" encoding="UTF-8"> <!-- basic_chat.aiml --><category><pattern>HELLO</pattern><template>Well, hello!</template></category><!--that使用方法,在于承上啟下,當(dāng)前回復(fù)內(nèi)容查找aiml中所有that,有則可以根據(jù)輸入內(nèi)容進(jìn)行pattern匹配,無(wú)則進(jìn)行新一輪問答--><category><pattern>你是男生嗎?</pattern><template>先告訴我你是么?</template></category><category><pattern>是</pattern><that>先告訴我你是么?</that><template>我也是</template></category><category><pattern>不是</pattern><that>先告訴我你是么?</that><template>我也不是</template></category><!--srai使用方法:srai會(huì)根據(jù)整句去查找整個(gè)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標(biāo)簽隨機(jī)選取一個(gè)進(jìn)行回復(fù)--><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標(biāo)簽get根據(jù)name獲取set的值--><category><pattern>我是*</pattern><template><set name = "name1"> <star/></set>你好</template></category><category><pattern>我的姓名是*</pattern><template>你的姓名是<get name = "name1"/></template></category><!--think標(biāo)簽,記錄變量值不返回給用戶--><category><pattern>think我是*</pattern><template><think><set name = "name"> <star/></set></think>你好</template></category><category><pattern>我的姓名是*</pattern><template>你的姓名是<get name = "name"/></template></category><!--condition使用方法,根據(jù)輸入信息返回不同的結(jié)果--><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 --機(jī)器人自我學(xué)習(xí)
<aiml version="1.0.1" encoding="UTF-8"><!-- std-startup.xml --><!-- Category是一個(gè)自動(dòng)的AIML單元 --><category><!-- Pattern用來(lái)匹配用戶輸入 --><!-- 如果用戶輸入 "LOAD AIML B" --><pattern>* 答案是 *</pattern><!-- Template是模式的響應(yīng) --><!-- 這里學(xué)習(xí)一個(gè)aiml文件 --><template><!--system可以調(diào)用python命令,從而往aiml中寫入對(duì)話--><system>python3 learn.py '<star index="1"/>' '<star index="2"/>'</system><learn>basic_chat.aiml</learn>好的我學(xué)會(huì)了<!-- 你可以在這里添加更多的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()總結(jié)
以上是生活随笔為你收集整理的python3.8使用aiml总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GitHub 优秀的 Android 开
- 下一篇: CSS | 使用 PostCSS 插件让