今天午休的时候,无意之中看了一篇博客,名字叫Python实现机器人,感觉挺有的意思的。
于是用其写了一个简单的Python聊天,源码如下所示:
# -*- coding: utf-8 -*-import aimlimport sysimport os def get_module_dir(name): print("module", sys.modules[name]) path = getattr(sys.modules[name], '__file__', None) print(path) 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' os.chdir(alice_path) # 切换到语料库所在工作目录 alice = aiml.Kernel() # 创建机器人alice对象alice.learn("startup.xml") # 加载...\\botdata\\alice\\startup.xmlalice.respond('LOAD ALICE') # 加载...\\botdata\\alice目录下的语料库 while True: message = input("Enter your message >> ") if("exit" == message): exit() response = alice.respond(message) # 机器人应答 print(response)
注意:如果出现某某模块找不到的时候,记得使用pip安装对应的模块。
效果图如下所示:
唯一美中不足的是英文,不过没关系,国内有图灵机器人。
代码如下所示:
from urllib.request import urlopen,Requestfrom urllib.error import URLErrorfrom urllib.parse import urlencodeimport jsonclass TuringChatMode(object): """this mode base on turing robot""" def __init__(self): # API接口地址 self.turing_url = 'http://
编程的世界是有趣的,你去探索,你会发现很多有意思的事情。
以上就是Python如何实现机器人聊天的详细内容,更多关于python 实现机器人聊天的资料请关注其它相关文章!