Nov-28-2019, 06:52 AM
hello all...
now the baby chatbot can save the new word that he learns in a text file as a database :)
it creates at the first time a text file called "chatbot_list.txt" witch stores all the new words the initial 4 words are stored in a list called vocab then it is copied to the text file each time you run the program...
have fun :)
now the baby chatbot can save the new word that he learns in a text file as a database :)
it creates at the first time a text file called "chatbot_list.txt" witch stores all the new words the initial 4 words are stored in a list called vocab then it is copied to the text file each time you run the program...
have fun :)
import os import random vocab = ['mom', 'dad', 'food', 'hi'] txt_file = 'chatbot_list.txt' def check_input(txt, vocab): txt_file = 'chatbot_list.txt' with open(txt_file, "r") as f: content = f.readlines() content = [x.strip() for x in content] vocab.append(content) f.close() if txt == "list": print("let see baby's word list grow:") if os.path.isfile(txt_file): with open(txt_file) as f: content = f.readlines() content = [x.strip() for x in content] for i, word in enumerate(content, start=1): print(f"{i}\t{word}") # vocab.append(word) f.close() elif any(s in txt for s in content): print(random.choice(content)) else: print("i've learned a new thing to say! thank you!") def add(txt): txt_file = 'chatbot_list.txt' with open(txt_file, "a") as f: f.write(f"{txt}\n") f.close() def main(): txt_file = 'chatbot_list.txt' if os.path.isfile(txt_file): access = "a" else: access = "w" with open(txt_file, access) as f: for item in vocab: f.write("%s\n" % item) f.close() print("hi! i'm a baby chatbot. please teach me how to talk and what to say") print("or type 'list' to view my words list:") while True: txt = input("> ") if txt == "end": break check_input(txt, vocab) if not txt == "list": add(txt) main()