人机交互程序 python实现人机对话

yipeiwu_com5年前Python基础

自己随便写了一个人机交互的程序。不存在任何智能,只是可以识别姓名,可以记录对话内容,并保存等到下一次交互时加载。 (推荐面向对象版本)

# 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() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python调用摄像头拍摄数据集

之前需要做一些目标检测的训练,需要自己采集一些数据集,写了一个小demo来实现图片的采集 使用方法: 指定name的名称,name为分类的标签 按n键拍摄图片 程序会在当前...

使用python编写android截屏脚本双击运行即可

测试的过程中经常需要截取屏幕,通常的做法是使用手机自带的截屏功能,然后将截屏文件复制出来,这种方法的优点是不需要连接数据线就可截屏,缺点则是生成的截屏文件命名是随机命名的,复制出来也比较...

Python搜索引擎实现原理和方法

Python搜索引擎实现原理和方法

如何在庞大的数据中高效的检索自己需要的东西?本篇内容介绍了Python做出一个大数据搜索引擎的原理和方法,以及中间进行数据分析的原理也给大家做了详细介绍。 布隆过滤器 (Bloom Fi...

用python解压分析jar包实例

写这个玩意的背景:在u8多渠道打包里,需要分析jar包,并把里面的文件按目录和类型分别放在root和assets文件夹里,之前师兄都是手动解压,一个一个文件夹找文件,效率比较低,刚好最近...

淘宝秒杀python脚本 扫码登录版

本文实例为大家分享了python淘宝秒杀的具体代码,供大家参考,具体内容如下 # 淘宝秒杀脚本,扫码登录版 import os from selenium import webdri...