基于Python制作AI聊天軟件的示例代碼
效果圖
先看一下效果圖
就當(dāng)是女友無聊的時(shí)候自己抽不出時(shí)間的小分身吧!
需要用到的庫
tkinter、time、urllib、requests
tkinter負(fù)責(zé)窗體、time顯示時(shí)間、urllib和requests負(fù)責(zé)請(qǐng)求
窗體設(shè)計(jì)
from tkinter import * win1 = Tk() win1.geometry('400x644+100+100') win1.title('xxx男神的AI分身') Label11 = Label(win1, text='男神白', font=('黑體', 12), anchor='center').place(y=13, x=15, width=380, height=20) Entry11 = Entry(win1, font=('等線', 11), width = 70) Entry11.place(y=600, x=15, width=310, height=26) Button11 = Button(win1, text='發(fā)送', font=('等線', 11), command = mecha).place(y=598, x=328, width=65, height=30) console = Text(win1, font=('等線', 11)) console.place(y=35, x=15, width=368, height=550) console.insert(1.0,' 歡迎來到你與男神的小天地!\n你可以把你想說的內(nèi)容輸入到下面的輸入框哦\n') console.mark_set('markOne', 1.0) console.mark_set('markTwo', 3.0) console.tag_add('tag1', 'markOne', 'markTwo') console.tag_config('tag1', foreground='red') win1.bind("<Return>", test_fun) win1.mainloop()
函數(shù)
在txet部件上顯示發(fā)送時(shí)間及顏色處理,使用requests和urllib庫來調(diào)用接口處理回復(fù)你女朋友的信息。
def liaotian(): global b import time b = 3 def mecha(): global b b+=2 console.insert('end',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n') console.insert('end',str(Entry11.get())+'\n') console.mark_set(str(b-1), str(b-1)+'.0') console.mark_set(str(b), str(b)+'.0') console.tag_add(str(b), str(b-1), str(b)) console.tag_config(str(b), foreground='blue') console.see(END) console.update() console.insert('end',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n') console.insert('end',aicha()+'\n') console.mark_set(str(b-1), str(b-1)+'.0') console.mark_set(str(b), str(b)+'.0') console.tag_add(str(b), str(b-1), str(b)) console.tag_config(str(b), foreground='red') console.see(END) Entry11.delete(0,END) console.update() def test_fun(self): mecha() def aicha(): global b b+=2 msg = str(Entry11.get()) else: import urllib import requests def qingyunke(msg): url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg)) html = requests.get(url) return html.json()["content"] print("原話>>", msg) res = qingyunke(msg) res = res.replace('菲菲', '你男神') res = res.replace('我', '你男神') print("智能回復(fù)>>", res) return res
為粉絲們額外添加的功能
根據(jù)女友輸入的內(nèi)容自定義回復(fù)
我自己添加的內(nèi)容太肉麻的,不太適合分享給你們哈。于是我就給你們做多了一個(gè)自定義回復(fù)的功能。嘿嘿!那就是獨(dú)一無二的代碼了
堅(jiān)持著你們直接能使用代碼不做任何修改的原則,我就不讓你們?cè)诖a里面添加了,當(dāng)你第一次運(yùn)行此代碼的時(shí)候會(huì)自動(dòng)創(chuàng)建一個(gè)txt文件(甚至還不用讓你創(chuàng)建文件),你就可以在txt文件中自定義回復(fù)內(nèi)容了。
使用異常處理模塊try來嘗試open讀取名為“自定義回復(fù).txt”的文件,若不存在except就創(chuàng)建,若已存在直接讀取即可。操作讀取的字符串逐個(gè)添加到zidingyi字典中,判斷輸入的語句是否在zidingyi.keys()中即可做出相應(yīng)回復(fù)。
添加后的效果圖:
zidingyi = {} try: with open("自定義回復(fù).txt", "r", encoding='utf8') as f: asd = f.readlines() print(asd) for line in asd: line = line.strip('\n') wen, da = line.split(':', 1) zidingyi[wen] = da except: with open("自定義回復(fù).txt", "w+", encoding='utf8') as f: f.write('提示——>采用“輸入:回復(fù)”格式 如——>你吃飯了嗎?:我吃飯啦 回車以繼續(xù)下一自定義回復(fù)(注意使用英文的冒號(hào))') with open("自定義回復(fù).txt", "r", encoding='utf8') as f: asd = f.readlines() print(asd) for line in asd[1:]: line = line.strip('\n') wen, da = line.split(':', 1) zidingyi[wen] = da print(line) print(zidingyi)
完整代碼
from tkinter import * def liaotian(): global b import time b = 3 def mecha(): global b b += 2 console.insert('end', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n') console.insert('end', str(Entry11.get()) + '\n') console.mark_set(str(b - 1), str(b - 1) + '.0') console.mark_set(str(b), str(b) + '.0') console.tag_add(str(b), str(b - 1), str(b)) console.tag_config(str(b), foreground='blue') console.see(END) console.update() console.insert('end', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n') console.insert('end', aicha() + '\n') console.mark_set(str(b - 1), str(b - 1) + '.0') console.mark_set(str(b), str(b) + '.0') console.tag_add(str(b), str(b - 1), str(b)) console.tag_config(str(b), foreground='red') console.see(END) Entry11.delete(0, END) console.update() def test_fun(self): mecha() def aicha(): global b b += 2 msg = str(Entry11.get()) if msg in zidingyi.keys(): res = zidingyi[msg] return res else: import urllib import requests def qingyunke(msg): url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg)) html = requests.get(url) return html.json()["content"] print("原話>>", msg) res = qingyunke(msg) res = res.replace('菲菲', '你男神') res = res.replace('我', '你男神') print("智能回復(fù)>>", res) return res zidingyi = {} try: with open("自定義回復(fù).txt", "r", encoding='utf8') as f: asd = f.readlines() print(asd) for line in asd: line = line.strip('\n') wen, da = line.split(':', 1) zidingyi[wen] = da except: with open("自定義回復(fù).txt", "w+", encoding='utf8') as f: f.write('提示——>采用“輸入:回復(fù)”格式 如——>你吃飯了嗎?:我吃飯啦 回車以繼續(xù)下一自定義回復(fù)(注意使用英文的冒號(hào))') with open("自定義回復(fù).txt", "r", encoding='utf8') as f: asd = f.readlines() print(asd) for line in asd[1:]: line = line.strip('\n') wen, da = line.split(':', 1) zidingyi[wen] = da print(line) print(zidingyi) win1 = Tk() win1.geometry('400x644+100+100') win1.title('男神的AI分身') Label11 = Label(win1, text='你男神', font=('黑體', 12), anchor='center').place(y=13, x=15, width=380, height=20) Entry11 = Entry(win1, font=('等線', 11), width=70) Entry11.place(y=600, x=15, width=310, height=26) Button11 = Button(win1, text='發(fā)送', font=('等線', 11), command=mecha).place(y=598, x=328, width=65, height=30) console = Text(win1, font=('等線', 11)) console.place(y=35, x=15, width=368, height=550) console.insert(1.0, ' 歡迎來到你與男神的小天地!\n 你可以把你想說的內(nèi)容輸入到下面的輸入框哦\n') console.mark_set('markOne', 1.0) console.mark_set('markTwo', 3.0) console.tag_add('tag1', 'markOne', 'markTwo') console.tag_config('tag1', foreground='red') win1.bind("<Return>", test_fun) win1.mainloop() liaotian()
怎么樣,是不是特別簡(jiǎn)單~快復(fù)制去送給你心中的那個(gè)女神吧~
以上就是基于Python制作AI聊天軟件的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python聊天軟件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python pydoc生成API文檔的實(shí)現(xiàn)
pydoc?模塊會(huì)根據(jù) Python 模塊來自動(dòng)生成文檔,本文主要介紹了python pydoc生成API文檔的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12Python程序打包exe報(bào)錯(cuò)的幾種解決方法
本文主要介紹了Python程序打包exe報(bào)錯(cuò)的幾種解決方法,文中通過幾種解決方法的介紹非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2023-08-08Python數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)代碼示例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)代碼示例,簡(jiǎn)單介紹了順序表的相關(guān)內(nèi)容,然后分享了其代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11python合并已經(jīng)存在的sheet數(shù)據(jù)到新sheet的方法
今天小編就為大家分享一篇python合并已經(jīng)存在的sheet數(shù)據(jù)到新sheet的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12python 字符串轉(zhuǎn)列表 list 出現(xiàn)\ufeff的解決方法
下面小編就為大家?guī)硪黄猵ython 字符串轉(zhuǎn)列表 list 出現(xiàn)\ufeff的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06詳解pandas刪除缺失數(shù)據(jù)(pd.dropna()方法)
這篇文章主要介紹了pandas刪除缺失數(shù)據(jù)(pd.dropna()方法),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06PyTorch實(shí)現(xiàn)手寫數(shù)字識(shí)別的示例代碼
本文主要介紹了PyTorch實(shí)現(xiàn)手寫數(shù)字識(shí)別的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下<BR>2022-05-05Python爬取股票信息,并可視化數(shù)據(jù)的示例
這篇文章主要介紹了Python爬取股票信息,并可視化數(shù)據(jù)的示例,幫助大家更好的理解和使用python爬蟲,感興趣的朋友可以了解下2020-09-09Python數(shù)字圖像處理代數(shù)之加減乘運(yùn)算
這篇文章主要介紹了Python數(shù)字圖像處理代數(shù)運(yùn)算,對(duì)其中的加、減、乘運(yùn)算分別作了詳細(xì)的講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09