色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Python如何實現機器人聊天

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

今天午休的時候,無意之中看了一篇博客,名字叫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’) + ’botdataalice’ os.chdir(alice_path) # 切換到語料庫所在工作目錄 alice = aiml.Kernel() # 創建機器人alice對象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) # 機器人應答 print(response)

注意:如果出現某某模塊找不到的時候,記得使用pip安裝對應的模塊。

效果圖如下所示:

Python如何實現機器人聊天

唯一美中不足的是英文,不過沒關系,國內有圖靈機器人。

代碼如下所示:

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): ’’’ 請求方式: HTTP POST 請求參數: 參數 是否必須 長度 說明 key 必須 32 APIkey info 必須 1-32 請求內容,編碼方式為'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') # 如果發生網絡錯誤,斷言提示沒有可用的網絡連接來傳輸文本信息 except: raise KeyError('Server wouldn’t respond (invalid key or quota has been maxed out)') # 其他情況斷言提示服務相應次數已經達到上限 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 !') # 設定輸入q,退出聊天。 else: turing_data = turing.get_turing_text(msg) print('Robot:',turing_data)

效果圖如下:

Python如何實現機器人聊天

可能由于機器人智能太低了,有點答非所問。

更多精彩可以去圖靈機器人官網了解:http://www.tuling123.com

編程的世界是有趣的,你去探索,你會發現很多有意思的事情。

以上就是Python如何實現機器人聊天的詳細內容,更多關于python 實現機器人聊天的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 亚洲自拍中文 | 欧美性色生活片免费播放 | 免费看美女毛片 | 欧美精品专区55页 | 久久精品国产精品亚洲综合 | 国产精品久久久久久久久久久威 | 欧美性猛交xxxxx按摩国内 | 99爱在线精品视频网站 | 一本本久综合久久爱 | 亚洲国产精品一区二区久久 | 亚洲欧美18v中文字幕高清 | 日本在线视频不卡 | 国产精品视频九九九 | 狠狠色丁香九九婷婷综合五月 | 亚洲精品www久久久久久久软件 | 日本高清一本二本三本如色坊 | 成年人网站在线观看视频 | 欧美日本俄罗斯一级毛片 | 久草最新在线 | 一区二区三区四区国产精品 | 精品国产三级a∨在线 | 性欧美17一18sex性高清播放 | 日韩欧美中文字幕在线观看 | 亚洲午夜精品 | 男人的天堂久久 | 99精品久久秒播无毒不卡 | 国产成人免费视频精品一区二区 | 成人欧美日韩高清不卡 | 国产精品高清久久久久久久 | 思思久热re6这里有精品 | 99久久精品毛片免费播放 | 国产精品一区二区在线观看 | 欧美一级情欲片在线 | 欧美一级特黄真人毛片 | 欧美一区二区三区视频在线 | 看中国一级毛片 | 久久国产精品二国产精品 | 欧美白人和黑人xxxx猛交视频 | 美女黄色在线网站大全 | 国产成人aa在线视频 | 一区二区三区四区在线视频 |