Import Pickledata = {"Do you have a girlfriend": "No", "can we have a relationship": "Yes", "About Today": "About", "to which restaurant": "McDonald"} with open ("Db.pkl", ' WB ') as F:f.write ( Pickle.dumps (data)) with open ("Db.pkl", "RB") as F:data = Pickle.loads (F.read ()) class Robotui:def __init__ (Self,name, Admin): Self.name = Name self.admin = Admin def talk (self): Worlds = input (' You say: ') if worlds = = ' End ': Return worlds Else: Reply = Backrobot.answer (Worlds) print (f ' {self.name} answer: {reply} ') if reply = = ' None ': ch = input (' Need to add data y/n: ') print (CH) if ch = = ' Y ': updata = input (' Please enter the sample (Eat: ATE): ') new = Updata.split (': ') robotadmin.write_data ({new[0]:new[1]}) class Robo Tadmin:def __init__ (self): Self.data = Self.load_data () def load_data (self): with open (' db.pkl ', ' RB ') as F:data = Pickle . Loads (F.read ()) # Print (data) return Data def answer (Self,worlds): If worlds in Self.data:return Self.data[worlds] Else: Return ' None ' @staticmethod def write_data (DIC): Data.update (DIC) print (data) with open ("Db.pkl", ' WB ') as F:f.write (pick Le.dumps (data) # Robot.talk () whileTrue:backrobot = Robotadmin () robot = Robotui (' Robot ', ' back ') if robot.talk () = = ' End ': Print (' Finish ') Break Robot.talk ()
你说:你有女朋友吗robot回答:没有你说:我们可以交往吗robot回答:可以你说:今晚约不约robot回答:约你说:去哪家餐厅robot回答:麦当劳你说:今晚去K歌吗robot回答:none是否需要增加数据 Y/N:YY请输入 示例(吃饭了吗:吃了):今晚去K歌吗:去{‘你有女朋友吗‘: ‘没有‘, ‘我们可以交往吗‘: ‘可以‘, ‘今晚约不约‘: ‘约‘, ‘去哪家餐厅‘: ‘麦当劳‘, ‘今晚去K歌吗‘: ‘去‘}你说:end你说:end结束
Case: Python implements a chat robot