Build simple chatbot using Python and NLTK | Beginner Python NLP Project

How amazing it is to tell someone everything and anything and not be judged at all. It is a top-class feeling and that's what the beauty of a chatbot is. A chatbot is an AI-based software used to deal with users with sort of particular queries or tasks and try to imitate human actions as much as possible.  

In simple words, a chatbot is something that reduces human work and helps humans to solve basic queries. Today most companies, businesses use chatbots on their website to get their customer loyalty and respond to them as soon as possible by dealing with them in a short period.

Build Simple Chatbot using Python and NLTK

Many companies integrate chatbots with their social media handles like Samsung has a smart Samsung assistant on WhatsApp as Samsung support which deals with you in sort of queries related to Samsung products. There are many examples like this. Some organizations provide the service to develop chatbots with more advanced techniques to these businesses like Engati, FloatBot, ManyChat, etc.

In this tutorial, we are going to understand how chatbots work, what is the logic behind them and we will build a simple chatbot with python to understand the concepts after that, we will also build another chatbot using NLTK library with datasets and built some more advance chatbot.

Why we Need Chatbots (Requirement of Chatbot in current technology evolving society)

1) Cost and Time Effective: It reduces the human or labor cost which requires being active on-site to provide the data of each second. Now we have the chatbots integrated with a website that crawls all the website activity as well as deals with the user visiting the site.

2) Cheap Development cost: with each passing day technology is revolutionizing, and it's very simple to build a chatbot and upgrade it as per our requirements and service.

3) Human Resource: The chatbot looks like the human is talking on the other side, so today the development of chatbots is done in a pretty good way. 

4) Business Branding:- integrating chatbots to social media sites gives a wow factor to your customers who can deal and respond.

There are 2 types of chatbots

  1. Rule-based Chatbots:- As the name suggests there are certain rules on which chatbots depend. It is provided with certain rules that if an input is this then, the output will be this. In this, you cannot ask complex queries. training of these bots is very simple.
  2. Self-Learning Bots:- The type of bots are highly efficient and are capable to grab the things that user want to know because it is built on the concept of Machine learning, deep learning techniques. The self-learning chatbots are further divided into 2 categories as.
    • Retrieval-based chatbots:- The chatbots include predefined input patterns and responses.
    • Generative-based chatbots:- These are based on a seq 2 seq neural network. It is based on the same idea as Machine Translation.

We have seen the theory of chatbot, Now let's build a simple chatbot which we call a retrieval-based chatbot. We will understand the process of working with a chatbot through this by training it on simple inputs using the if-else approach. we will build a simple GUI of this using Tkinter and python.

If you do not have the Tkinter library installed then you can install it using the pip command direct in your command prompt.

        pip install tkinter

First, we have created a plain Tkinter window. After that, we have created a text field using the entry() method where the user will enter the text. and then we placed a button widget which on click call the function as send. in the function, we have used a basic if-else statement to train a chatbot on basic syntax. 

simple chatbot using python

Implement Chatbot using Python NLTK library

Natural language processing plays a great part in text analytics or yet mining. Chatbots is one of the crucial application of NLP, where a chatbot is capable to respond to human in human language. we will use NLTK(Natural language toolkit) library now for creating a simple yet amazing chatbot.

You can install the NLTK library using the below code.

        pip install nltk

The first step is to import the libraries. 

Explanation ~ 

Chat:- Chat is a class that contains all the logic for building a chatbot like it preprocesses the text data, and finds the useful information from it to feed to a chatbot.

reflections: Another import was reflections which is a dictionary containing a set of input values and corresponding output values. you can also create your own dictionary in the same format as shown. if you print reflections you can see the dictionary as below.

Building a chatbot

After importing the libraries, we have to create rules which will be fed to a chatbot. The lines of code as given below create a simple set of rules. The first element is user input and corresponding to it, we have the second element as a list which is the response from a bot. we create pairs of an object of it, and you can modify them as per the question and answer you want.

After creating pairs of rules we define a function to chat. the function is very simple that first displays a greeting message to you. The next step is to initiate the conversation using the main function and you can start a conversation with a chatbot.

Let's start chatting to a chatbot and have fun.

Chat with chatbot made using Python

let's interact more with chatty.

Interaction with Chatbot created using Python

The NLTK chat chatbots work on regex patterns of keywords present in your questions. so you can add any number of questions in a proper format as per your choice.

Conclusion

Hurray! We have learned to implement a simple chatbot using plain python. I hope you guys have enjoyed this blog. If you have any queries then please post them in the query section below. Now after this post we will learn about some chatbot implementing tools like DialogFlow, Rasa, etc. If you want to develop more advanced chatbots then just use the above search button to search for more articles and projects related to chatbot creation.

Be safe, Happy Learning!..
Thank You!

6 Comments

If you have any doubt or suggestions then, please let me know.

  1. Simple and easy explanation.
    Liked it.

    ReplyDelete
  2. Great Work buddy, Keep it Up✌️

    ReplyDelete
  3. Great work... Coding looks simple after visiting this.. 🤟

    ReplyDelete
  4. business benefits A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post.

    ReplyDelete
  5. chatbot website, I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.

    ReplyDelete
  6. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. professional organizing

    ReplyDelete
Previous Post Next Post