国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Python如何實(shí)現(xiàn)機(jī)器人聊天

瀏覽:73日期:2022-07-11 16:09:25

今天午休的時(shí)候,無意之中看了一篇博客,名字叫Python實(shí)現(xiàn)機(jī)器人,感覺挺有的意思的。于是用其寫了一個(gè)簡(jiǎn)單的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’) + ’botdataalice’ os.chdir(alice_path) # 切換到語料庫所在工作目錄 alice = aiml.Kernel() # 創(chuàng)建機(jī)器人alice對(duì)象alice.learn('startup.xml') # 加載...botdataalicestartup.xmlalice.respond(’LOAD ALICE’) # 加載...botdataalice目錄下的語料庫 while True: message = input('Enter your message >> ') if('exit' == message): exit() response = alice.respond(message) # 機(jī)器人應(yīng)答 print(response)

注意:如果出現(xiàn)某某模塊找不到的時(shí)候,記得使用pip安裝對(duì)應(yīng)的模塊。

效果圖如下所示:

Python如何實(shí)現(xiàn)機(jī)器人聊天

唯一美中不足的是英文,不過沒關(guān)系,國內(nèi)有圖靈機(jī)器人。

代碼如下所示:

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://www.tuling123.com/openapi/api?’ def get_turing_text(self,text): ’’’ 請(qǐng)求方式: HTTP POST 請(qǐng)求參數(shù): 參數(shù) 是否必須 長度 說明 key 必須 32 APIkey info 必須 1-32 請(qǐng)求內(nèi)容,編碼方式為'utf-8' userid 必須 32 MAC地址或ID ’’’ turing_url_data = dict( key = ’fcbf9efe277e493993e889eabca5b331’, info = text, userid = ’60-14-B3-BA-E1-4D’, ) # print('The things to Request is:',self.turing_url + urlencode(turing_url_data)) self.request = Request(self.turing_url + urlencode(turing_url_data)) # print('The result of Request is:',self.request) try: w_data = urlopen(self.request) # print('Type of the data from urlopen:',type(w_data)) # print('The data from urlopen is:',w_data) except URLError: raise IndexError('No internet connection available to transfer txt data') # 如果發(fā)生網(wǎng)絡(luò)錯(cuò)誤,斷言提示沒有可用的網(wǎng)絡(luò)連接來傳輸文本信息 except: raise KeyError('Server wouldn’t respond (invalid key or quota has been maxed out)') # 其他情況斷言提示服務(wù)相應(yīng)次數(shù)已經(jīng)達(dá)到上限 response_text = w_data.read().decode(’utf-8’) # print('Type of the response_text :',type(response_text)) # print('response_text :',response_text) json_result = json.loads(response_text) # print('Type of the json_result :',type(json_result)) return json_result[’text’]if __name__ == ’__main__’: print('Now u can type in something & input q to quit') turing = TuringChatMode() while True: msg = input('nMaster:') if msg == ’q’: exit('u r quit the chat !') # 設(shè)定輸入q,退出聊天。 else: turing_data = turing.get_turing_text(msg) print('Robot:',turing_data)

效果圖如下:

Python如何實(shí)現(xiàn)機(jī)器人聊天

可能由于機(jī)器人智能太低了,有點(diǎn)答非所問。

更多精彩可以去圖靈機(jī)器人官網(wǎng)了解:http://www.tuling123.com

編程的世界是有趣的,你去探索,你會(huì)發(fā)現(xiàn)很多有意思的事情。

以上就是Python如何實(shí)現(xiàn)機(jī)器人聊天的詳細(xì)內(nèi)容,更多關(guān)于python 實(shí)現(xiàn)機(jī)器人聊天的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久草福利资源网站免费 | 国产成人综合亚洲一区 | 欧美一级毛片黄 | 韩国一大片a毛片 | 中文字幕一级毛片视频 | 她也啪在线视频 | 精品国产乱码久久久久久一区二区 | 国产一区二区三区免费看 | 国产日韩在线播放 | 欧美日韩成人在线视频 | 夜色亚洲 | 免费在线黄色网址 | 成人影院久久久久久影院 | 性感美女一级片 | 欧美a级在线 | 久久久久无码国产精品一区 | 91精品国产手机 | 国产午夜亚洲精品一区网站 | 毛片韩国 | 香蕉超级碰碰碰97视频在线观看 | 国产国语高清在线视频二区 | 亚洲精品成人一区二区 | 久久久久久日本一区99 | 香蕉网站狼人久久五月亭亭 | 亚洲视频自拍 | 亚洲欧美午夜 | 成人黄色三级 | 免费视频 久久久 | 偷偷久久| 国产精品久久久久毛片 | 欧美性色xo影院在线观看 | 一级做a爱片久久蜜桃 | 99久久99久久久精品久久 | 久久国产精品高清一区二区三区 | 欧美午夜不卡在线观看最新 | 最新在线精品国自拍视频 | 久久福利青草精品资源 | 国产一级一级一级国产片 | 一级一级特黄女人精品毛片 | 在线观看自拍视频 | 国产亚洲欧美在线人成aaaa |