News:

Welcome to RetroCoders Community

Main Menu

the baby chatbot in python

Started by ron77_db, May 12, 2022, 11:17 AM

Previous topic - Next topic

ron77_db

hello all

here is the 3rd version of my chatbot... in this version, the bot uses 2 lists one to store the input (questions) and a second one to store the desired output (answers). Then, when you exit by typing "end", it stores the questions and answers in two separate text files ("chatbot_Q.txt" and "chatbot_A.txt"). It also has a weak AI algorithm if the new questions is 75% like other questions that the bot has answers to in its database the bot gives you to choose which answer id most appropriate or you can then type "none" and enter a new answer (input) of your own costume made...

this could be a good start to make a database of Q&A for further developing even more advanced chatbots (with say tensorflow and machine learning) that use a private database based upon your own conversations with this bot...

here is the code... enjoy :)


import os
 
 
def check_question_in_vocabQ(txt, vocabQ, vocabA):
    optional_answers = []
    clean_string = txt.strip("?.,!':;$#").replace("'", '')
    clean_string_list = clean_string.split()
    word_num = len(clean_string_list)
    for inx, question in enumerate(vocabQ):
        if question == clean_string:
            print(vocabA[inx])
            return True
 
    for inx, question in enumerate(vocabQ):
        word_counter = 0
        for word in clean_string_list:
            if word in question:
                word_counter += 1
        if word_counter > word_num * 0.75:
            optional_answers.append(vocabA[inx])
 
    if len(optional_answers) > 0:
        print("select answer from list, if non of them match enter none:")
        for inx, answer in enumerate(optional_answers):
            print(f"{inx+1}. {answer}")
        choice = input("> ")
        if choice == "none":
            ans = input("please enter correct answer ")
        else:
            ans = optional_answers[int(choice)-1]
        vocabQ.append(clean_string)
        vocabA.append(ans)
        return True
    return False
 
 
def check_input(txt, vocabQ, vocabA):
    if txt == "list":
        print("let see baby's word list grow:")
        for i in range(len(vocabQ)):
            print(f"Q: {vocabQ[i]}")
            print(f"A: {vocabA[i]}")
            print()
    elif check_question_in_vocabQ(txt, vocabQ, vocabA):
        print()
    else:
        answer = input("new input - what should i replay?: ")
        vocabQ.append(txt.strip("?.,!:;$#\'").replace("'", ''))
        vocabA.append(answer)
 
 
def main():
    txt_fileQ = 'chatbot_Q.txt'
    txt_fileA = 'chatbot_A.txt'
    if os.path.isfile(txt_fileQ) and os.path.isfile(txt_fileA):
        vocabQ = [line.strip() for line in open(txt_fileQ)]
        vocabA = [line.strip() for line in open(txt_fileA)]
    else:
        vocabQ = []
        vocabA = []
    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":
            with open(txt_fileQ, 'w') as f:
                for item in vocabQ:
                    f.write("%s\n" % item)
            with open(txt_fileA, 'w') as f:
                for item in vocabA:
                    f.write("%s\n" % item)
            break
        check_input(txt, vocabQ, vocabA)
 
 
main()