Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
the baby chatbot
#7
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 :)
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()
Reply


Messages In This Thread
the baby chatbot - by ronblue77 - Nov-27-2019, 12:33 PM
RE: the baby chatbot - by buran - Nov-27-2019, 12:36 PM
RE: the baby chatbot - by ronblue77 - Nov-27-2019, 12:54 PM
RE: the baby chatbot - by buran - Nov-27-2019, 01:01 PM
RE: the baby chatbot - by ronblue77 - Nov-27-2019, 03:10 PM
RE: the baby chatbot - by buran - Nov-27-2019, 03:28 PM
RE: the baby chatbot - by ronblue77 - Nov-28-2019, 06:52 AM
RE: the baby chatbot - by buran - Nov-28-2019, 08:34 AM
RE: the baby chatbot - by ronblue77 - Nov-29-2019, 08:34 PM
the baby chat bot version 3 - by ronblue77 - Nov-30-2019, 07:17 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020