人机交互程序 python实现人机对话
2017-11-14 10:01
1116 查看
自己随便写了一个人机交互的程序。不存在任何智能,只是可以识别姓名,可以记录对话内容,并保存等到下一次交互时加载。 (推荐面向对象版本)
# 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的面向对象编程版本(推荐)
# 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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Bezier曲线重要性质及程序实现(可人机交互)
- 利用树莓派3 实现简单的人机交互程序
- python 调用图灵机器人api实现简单的人机交互
- python 调用图灵机器人api实现简单的人机交互
- python——socket实现简单C/S交互开发
- Python设计足球联赛赛程表程序的思路与简单实现示例
- python编写小程序,模拟实现自动按下键盘
- python中的 \r以及基于此实现简单的倒计时小程序
- python实现的某网页模拟点赞程序
- AI人工智能-Python实现人机对话
- python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过autopy实现右键菜单和另存为操作
- 较高人工智能的人机博弈程序实现(多个算法结合)含C++源码
- python实现图片筛选程序
- 一个基于python3+PyQt5实现的简单计算器程序
- python+soket实现UDP协议的局域网广播程序
- 浏览Python主页的发现和收获 & 想要实现的Python程序
- Python 小程序,实现比较两个列表范围
- 手动实现把python项目发布为exe可执行程序过程分享
- python实现微信小程序自动回复
- 用IBM Bluemix-Watson API 实现语音人机交互