Python如何實(shí)現(xiàn)機(jī)器人聊天
今天午休的時(shí)候,無(wú)意之中看了一篇博客,名字叫Python實(shí)現(xiàn)機(jī)器人,感覺(jué)挺有的意思的。
于是用其寫(xiě)了一個(gè)簡(jiǎn)單的Python聊天,源碼如下所示:
# -*- coding: utf-8 -*- import aiml import sys import 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) # 切換到語(yǔ)料庫(kù)所在工作目錄 alice = aiml.Kernel() # 創(chuàng)建機(jī)器人alice對(duì)象 alice.learn("startup.xml") # 加載...\\botdata\\alice\\startup.xml alice.respond('LOAD ALICE') # 加載...\\botdata\\alice目錄下的語(yǔ)料庫(kù) 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)的模塊。
效果圖如下所示:
唯一美中不足的是英文,不過(guò)沒(méi)關(guān)系,國(guó)內(nèi)有圖靈機(jī)器人。
代碼如下所示:
from urllib.request import urlopen,Request from urllib.error import URLError from urllib.parse import urlencode import json class 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ù) 是否必須 長(zhǎng)度 說(shuō)明 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ò)誤,斷言提示沒(méi)有可用的網(wǎng)絡(luò)連接來(lái)傳輸文本信息 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)
效果圖如下:
可能由于機(jī)器人智能太低了,有點(diǎn)答非所問(wè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)注腳本之家其它相關(guān)文章!
相關(guān)文章
狀態(tài)機(jī)的概念和在Python下使用狀態(tài)機(jī)的教程
這篇文章主要介紹了狀態(tài)機(jī)的概念和在Python下使用狀態(tài)機(jī)的教程,本文來(lái)自于IBM官方開(kāi)發(fā)者技術(shù)文檔,需要的朋友可以參考下2015-04-04Django的開(kāi)發(fā)步驟原來(lái)是這樣的
這篇文章主要為大家詳細(xì)介紹了Django的開(kāi)發(fā)步驟,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02Python?matplotlib實(shí)戰(zhàn)之散點(diǎn)圖繪制
散點(diǎn)圖,又名點(diǎn)圖、散布圖、X-Y圖,是將所有的數(shù)據(jù)以點(diǎn)的形式展現(xiàn)在平面直角坐標(biāo)系上的統(tǒng)計(jì)圖表,本文主要為大家介紹了如何使用Matplotlib繪制散點(diǎn)圖,需要的可以參考下2023-08-08python使用os.listdir和os.walk獲得文件的路徑的方法
本篇文章主要介紹了python使用os.listdir和os.walk獲得文件的路徑的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12python基于itchat模塊實(shí)現(xiàn)微信防撤回
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信防撤回,基于itchat模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Python成功解決讀文件出現(xiàn):IOError:?[Errno?0]?Error的錯(cuò)誤
在Python編程中,處理文件是常見(jiàn)的任務(wù)之一,但偶爾也會(huì)遇到各種錯(cuò)誤,包括IOError,盡管Python?3.x中IOError已被OSError和FileNotFoundError等更具體的異常所取代,由于[Errno?0]不直接指向具體的錯(cuò)誤類型,我們將討論一系列可能導(dǎo)致IOError的常見(jiàn)情況,需要的朋友可以參考下2024-07-07python中h5py開(kāi)源庫(kù)的使用樣例詳解
這篇文章主要介紹了python中的h5py開(kāi)源庫(kù)的使用,本文只是簡(jiǎn)單的對(duì)h5py庫(kù)的基本創(chuàng)建文件,數(shù)據(jù)集和讀取數(shù)據(jù)的方式進(jìn)行介紹,需要的朋友可以參考下2022-05-05pytorch模型保存與加載中的一些問(wèn)題實(shí)戰(zhàn)記錄
一般來(lái)說(shuō),保存模型是把參數(shù)全部用model.cpu().state_dict(),然后加載模型時(shí)一般用model.load_state_dict(torch.load(model_path)),下面這篇文章主要給大家介紹了關(guān)于pytorch模型保存與加載中的一些問(wèn)題實(shí)戰(zhàn)記錄,需要的朋友可以參考下2022-10-10PYQT5 vscode聯(lián)合操作qtdesigner的方法
這篇文章主要介紹了PYQT5 vscode聯(lián)合操作qtdesigner的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03