2018-07-09 13:56:23 852浏览
今天扣丁学堂给大家介绍一下关于如何使用Python核心编程之AIML搭建聊天机器人的方法,首先你可以在AIMLWikipediapage了解更多AIML的内容以及它能够做什么。借助Python的AIML包,我们很容易实现人工智能聊天机器人。AIML全名为ArtificialIntelligenceMarkupLanguage(人工智能标记语言),是一种创建自然语言软件代理的XML语言,是由RichardS.Wallace博士和Alicebot开源软件组织于1995-2000年间发明创造的。AIML是一种为了匹配模式和确定响应而进行规则定义的XML格式。
pipinstallaiml
#-*-coding:utf-8-*- importaiml importsys importos defget_module_dir(name): path=getattr(sys.modules[name],'__file__',None) ifnotpath: raiseAttributeError('module%shasnotattribute__file__'%name) returnos.path.dirname(os.path.abspath(path)) alice_path=get_module_dir('aiml')+'/alice' #切换到语料库所在工作目录 os.chdir(alice_path) alice=aiml.Kernel() alice.learn("startup.xml") alice.respond('LOADALICE') whileTrue: printalice.respond(raw_input("Enteryourmessage>>"))
<aimlversion="1.0.1"encoding="UTF-8"> <!--std-startup.xml--> <!--Category是一个自动的AIML单元--> <category> <!--Pattern用来匹配用户输入--> <!--如果用户输入"LOADAIMLB"--> <pattern>LOADAIMLB</pattern> <!--Template是模式的响应--> <!--这里学习一个aiml文件--> <template> <learn>basic_chat.aiml</learn> <!--你可以在这里添加更多的aiml文件--> <!--<learn>more_aiml.aiml</learn>--> </template> </category> </aiml>
<aimlversion="1.0.1"encoding="UTF-8"> <!--basic_chat.aiml--> <aiml> <category> <pattern>HELLO</pattern> <template> Well,hello! </template> </category> <category> <pattern>WHATAREYOU</pattern> <template> I'mabot,silly! </template> </category> </aiml>
<category> <pattern>ONETIMEI*</pattern> <template> <random> <li>Goon.</li> <li>Howoldareyou?</li> <li>Bemorespecific.</li> <li>Ididnotknowthat.</li> <li>Areyoutellingthetruth?</li> <li>Idon'tknowwhatthatmeans.</li> <li>Trytotellmethatanotherway.</li> <li>Areyoutalkingaboutananimal,vegetableormineral?</li> <li>Whatisit?</li> </random> </template> </category>
#-*-coding:utf-8-*- importaiml importos mybot_path='./mybot' #切换到语料库所在工作目录 os.chdir(mybot_path) mybot=aiml.Kernel() mybot.learn("std-startup.xml") mybot.respond('loadaimlb') whileTrue: printmybot.respond(raw_input("Enteryourmessage>>"))
#-*-coding:utf-8-*- importaiml importos mybot_path='./mybot' #切换到语料库所在工作目录 os.chdir(mybot_path) mybot=aiml.Kernel() ifos.path.isfile("mybot_brain.brn"): mybot.bootstrap(brainFile="mybot_brain.brn") else: mybot.bootstrap(learnFiles="std-startup.xml",commands="loadaimlb") mybot.saveBrain("mybot_brain.brn") whileTrue: printmybot.respond(raw_input("Enteryourmessage>>"))
whileTrue: message=raw_input("Enteryourmessage>>") ifmessage=="quit": exit() elifmessage=="save": mybot.saveBrain("bot_brain.brn") else: bot_response=mybot.respond(message) #Dosomethingwithbot_response
sessionId=12345 mybot.respond(raw_input(">>>"),sessionId)
sessionId=12345 #会话信息作为字典获取.包含输入输出历史, #以及任何已知断言 sessionData=mybot.getSessionData(sessionId) #每一个会话ID需要时一个唯一值。 #断言名是机器人在与你的会话中了解到的某些/某个名字 #机器人可能知道,你是"Billy",而你的狗的名字是"Brandy" mybot.setPredicate("dog","Brandy",sessionId) clients_dogs_name=mybot.getPredicate("dog",sessionId) mybot.setBotPredicate("hometown","127.0.0.1") bot_hometown=mybot.getBotPredicate("hometown")
<aimlversion="1.0.1"encoding="UTF-8"> <category> <pattern>MYDOGSNAMEIS*</pattern> <template> Thatisinterestingthatyouhaveadognamed<setname="dog"><star/></set> </template> </category> <category> <pattern>WHATISMYDOGSNAME</pattern> <template> Yourdog'snameis<getname="dog"/>. </template> </category> </aiml>
MydogsnameisMax
ThatisinterestingthatyouhaveadognamedMax
Whatismydogsname?
Yourdog'snameisMax.
以上就是关于扣丁学堂解析如何使用PythonAIML搭建聊天机器人方法示例的详细介绍,希望对小伙伴们有所帮助,想要了解更多内容的小伙伴可以登录扣丁学堂官网咨询。扣丁学堂是专业Python培训机构,不仅有专业的老师和与时俱进的课程体系,还有大量的Python视频教程供学员观看学习哦。扣丁学堂Python技术交流群:279521237。
【关注微信公众号获取更多学习资料】