Case: Python implements a chat robot

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.