人機交互程序 python實現(xiàn)人機對話
更新時間:2017年11月14日 10:01:03 作者:nbu2004
這篇文章主要為大家詳細介紹了人機交互程序,初步實現(xiàn)python人機對話,具有一定的參考價值,感興趣的小伙伴們可以參考一下
自己隨便寫了一個人機交互的程序。不存在任何智能,只是可以識別姓名,可以記錄對話內(nèi)容,并保存等到下一次交互時加載。 (推薦面向?qū)ο蟀姹荆?/span>
# hello.py # 這是老早寫的。不過今天加入了Pickle,然后潤色了一下。 # 可能有點無聊(不推薦使用) import pickle import os.path def search(x, data): for k, d in enumerate(data): if x == d['name']: return k, d def save_data(A,pklname): with open(pklname, 'wb') as pkl: pickle.dump(A, pkl) def load_data(pklname): with open(pklname, 'rb') as pkl: return pickle.load(pkl) # communicating with computer data=[] if not os.path.isfile('data') else load_data('data') while True: print('Welcome! [type "quit" if you want to quit.]') name=input('--What is your name?\n--') if name in {'quit','Quit','q','Q'}: print('[You quit]') break if not search(name, data): print('--Welcome, '+name+ '. I will remember you name.') d={'name':name,'age':0,'history':[]} data.append(d) else: print('--Hi, '+name+'. How I miss you.') k, d=search(name, data) while d['age']==0: age=input('--How old are you?[I will repeat until you respond!]') try: if int(age)==0: continue d['age']=int(age); data[k]=d except: pass while True: y=input('--Chan I help you? [yes/no]') while not y: y=input('--Yes or no?') d['history'].append(y); data[k]=d if y in {'no','No','n','N'}: print('--%s.'%y) print('--Bye bye.') break elif y in {'yes','Yes','y','Y'}: print('--%s.'%y) print('I am pleased to serve you.') else: print('I am sorry. I can not understand what you said.') break # save data y=input('--Do you want to save the data? [yes/no]') while not y: y=input('--Yes or no?') if y in {'no','No','n','N'}: print('--%s. [You say no.]'%y) elif y in {'yes','Yes','y','Y'}: print('--%s. [the data is saved in file named "data".]'%y) save_data(data, 'data') else: print('I am sorry. I can not understand what you said. data are not saved.')
下面是hello.py的面向?qū)ο缶幊贪姹荆ㄍ扑])
# hello.py import pickle import os.path def search(x, data): for k, d in enumerate(data): if x == d['name']: return k, d class Hello: def __init__(self, name='', data=None): self.name = name self.data = data def __getstate__(self): return self.data def __setstate__(self, data): self.data = data def mainloop(self): while True: print('Welcome! [type "quit" if you want to quit.]') name=input('--What is your name?\n--') if name in {'quit','Quit','q','Q'}: print('[You quit]') break if not search(name, self.data): print('--Welcome, '+name+ '. I will remember you name.') d={'name':name,'age':0,'history':[]} self.data.append(d) else: print('--Hi, '+name+'. How I miss you.') k, d=search(name, self.data) while d['age']==0: age=input('--How old are you?[I will repeat until you respond!]') try: if int(age)==0: continue d['age']=int(age); self.data[k]=d except: pass while True: y=input('--Chan I help you? [yes/no]') while not y: y=input('--Yes or no?') d['history'].append(y); self.data[k]=d if y in {'no','No','n','N'}: print('--%s.'%y) print('--Bye bye.') break elif y in {'yes','Yes','y','Y'}: print('--%s.'%y) print('I am pleased to serve you.') else: print('I am sorry. I can not understand what you said.') break # save data y=input('--Do you want to save the data? [yes/no]') while not y: y=input('--Yes or no?') if y in {'no','No','n','N'}: print('--%s. [You say no.]'%y) elif y in {'yes','Yes','y','Y'}: print('--%s. [the data is saved in file named "data.pkl".]'%y) with open('data.pkl', 'wb') as fo: pickle.dump(self, fo) else: print('I am sorry. I can not understand what you said. data are not saved.') # communicating with computer try: with open('data.pkl', 'rb') as fo: hello=pickle.load(fo) except: hello=Hello('ai',[]) hello.mainloop()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作
這篇文章主要介紹了從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python中使用tkFileDialog實現(xiàn)文件選擇、保存和路徑選擇
這篇文章主要介紹了Python中使用tkFileDialog實現(xiàn)文件選擇、保存和路徑選擇,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05Python基于分析Ajax請求實現(xiàn)抓取今日頭條街拍圖集功能示例
這篇文章主要介紹了Python基于分析Ajax請求實現(xiàn)抓取今日頭條街拍圖集功能,涉及Python針對今日頭條URL請求與json數(shù)據(jù)處理相關操作技巧,需要的朋友可以參考下2018-07-07使用Python的Treq on Twisted來進行HTTP壓力測試
這篇文章主要介紹了使用Python的Treq on Twisted來進行HTTP壓力測試,基于Python中的Twisted框架,需要的朋友可以參考下2015-04-04淺談python 調(diào)用open()打開文件時路徑出錯的原因
這篇文章主要介紹了淺談python 調(diào)用open()打開文件時路徑出錯的原因,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06