Creating a chatbot program that can learn as you interact

 



 ¡Hola! 
As we delve deeper into AI and machine learning saga, here is a very cool Python code that could help you create a small Chatbot program, that can 
a. Learn from you as you chat with it
b. Save information, so that when you reopen the program it can remember what it has learned for so long.
c. And it should be pretty fast to respond.
The below code is very basic and could be exercised to include more and more features as you go through it. 
And I used Python as my code; alternatively, you can also write the same using C#, if that's what you are comfortable with.
The algorithm:
I am creating a file called: questionBank.json and saving it as a physical file, and which starts with a couple of very basic questions:



I am chatting with my chatbot like this:


And when I ask it something it doesn't know:

So that when you ask it next time:


Pretty cool, eh? Let me show you, how this has been done.
Step 1: I am using Spyder3.11, as my IDE, but alternatively you can use anything that you're comfortable with: PyCharm, JuPyter, etc. 
Step 2: Open up your working folder location, and create a JSON file, with the name: "questionBank.json" and do close it. You can fill it up with certain dummy questions, but otherwise, you can leave it just blank.
Step 3: Start your code by importing from json:

Step 4: Create the following function that reads from our file, mentioned above and then loads it into a variable called 'data' and returns it (the keyword 'r' implies read):

Step 5: Create the following function that saves the content of the file in the working folder location (the keyword 'w' implies write).

Step 6: Create the following function that gets the input from the user, converting it into lowercase:

Step 7: Finally in the main method we can manipulate/call the above functions like this:
def main():
    data_file = 'qa_data.json'
    try:
        data = get_data(data_file)
    except FileNotFoundError:
        data = {}

    print("Hello and Welcome to the Q&A ChatBot!")

    while True:
        user_input = input("User: ").strip().lower()

        if user_input == 'exit':
            print("Ciao!")
            break

        answer = get_reply(user_input, data)

        if answer:
            print("Bot:", answer)
        else:
            print("Bot: Hey, I'm sorry, I don't know the answer to that question.")
            teach = input("Would you like to teach me?: ").strip().lower()
            if teach == 'yes' or teach== 'sure' or teach == 'why not':
                new_reply = input("What's the answer to that question?: ").strip()
                data[user_input] = new_reply
                save_file(data_file, data)
                print("Bot: Wow! Noted. Will remeber that!")
            else:
                print("Bot: OK, Please ask something else!")

if __name__ == "__main__":
    main()

Look at the code above. It straightforwardly keeps on reading the prompt from the user and looks up for answers from the file, and it then if it doesn't find them, it will save the answer. The user asks a question, it doesn't find any answer, it searches for any answer from the user that is like 'yes' or 'sure', it will ask what's the response to the question and will save it in the file.
So much for now, see you soon with more insights on Machine learning with Python.

Comments

Popular posts from this blog

X++ : mistakes which developers commit the most

Make your menu items visible on main menu, conditionally,, using this cool feature of D365FO

Speed up your execution performance by using SysGlobalCaches