Build your first chatbot using NLTK and Keras

Chatbot using NLTK and Keras

In this article, we will learn about chatbots using Python and how to make chatbots in python using NLTK and Keras.

What is Chatbot?

A chatbot is a software application used to conduct an online chat conversation via text or text-to-speech instead of providing direct contact with a live human agent. A chatbot is a type of software that can automate conversations and interact with people through messaging platforms.

Chatbots are now responsible for almost 30% of all activities. Chatbots are used by companies to provide services such as customer care, knowledge generation, and more.

Types of chatbots?

Chatbots can be broadly categorized into three types.

Simple chatbots

  • Simple chatbots have limited capabilities, and are usually called rule-based bots.
  • They are task-specific. This means the bot poses questions based on predetermined options and the customer can choose from the options until they get answers to their query.
  • These chatbots are best suited for straightforward dialogues.
  • They are very simple to build and train.
  • Example: Ordering Pizza

Smart chatbots

  • AI-enabled smart chatbots are designed to simulate near-human interactions with customers.
  • They can have free-flowing conversations and understand intent, language, and sentiment.
  • These chatbots require programming to help them understand the context of interactions.
  • They are much harder to implement and execute and need a lot of data to learn.
  • Example: Virtual Assistants

Hybrid chatbots

  • They are a combination of simple and smart chatbots.
  • Hybrid chatbots meet that middle ground.
  • Hybrid chatbots have some rule-based tasks, and they can understand intent and context.
  • This makes them a balanced tool for businesses to interact with customers.
  • Example: Medical Diagnosis

How to Make Chatbot in Python?

To create a chatbot in python you should have good knowledge of Python, Keras, and Natural language processing (NLTK).

Below are the 6 steps to create a chatbot in Python:

  1. Install required modules you can install the required modules with the help of the python-pip command

2. Import and load the data file

  • Create intents.json. This is how our intents.json file looks like.
  • First, make a file name as We import the necessary packages for our chatbot and initialize the variables we will use in our Python project.
  • The data file is in JSON format so we used the JSON package to parse the JSON file into Python.

3. Preprocess data

  • When working with text data, we need to perform various preprocessing on the data before we make a machine learning or a deep learning model. Based on the requirements we need to apply various operations to preprocess the data.
  • Tokenizing is the most basic and first thing you can do on text data. Tokenizing is the process of breaking the whole text into small parts like words.
  • Here we iterate through the patterns and tokenize the sentence using nltk.word_tokenize() function and append each word in the words list. We also create a list of classes for our tags.
  • Now we will lemmatize each word and remove duplicate words from the list. Lemmatizing is converting a word into its lemma form and then creating a pickle file to store the Python objects that we will use while predicting.

4. Create training and testing data

  • Now, we will create the training data in which we will provide the input and the output.
  • Our input will be the pattern and output will be the class our input pattern belongs to. But the computer doesn’t understand the text so we will convert text into numbers.

5. Build the model

  • We have our training data ready, now we will build a deep neural network that has 3 layers. We use the Keras sequential API for this.
  • After training the model for 200 epochs, we achieved 100% accuracy on our model. Let us save the model as ‘chatbot_model.h5’.

6. Predict the response (Graphical User Interface)

  • To predict the sentences and get a response from the user, create a new file ‘’.
  • We will load the trained model and then use a graphical user interface that will predict the response from the bot. The model will only tell us the class it belongs to, so we will implement some functions which will identify the class and then retrieve a random response from the list of responses.
  • Again we import the necessary packages and load the ‘words.pkl’ and ‘classes.pkl’ pickle files which we have created when we trained our model:
  • To predict the class, we will need to provide input in the same way as we did while training. So we will create some functions that will perform text preprocessing and then predict the class.
  • After predicting the class, we will get a random response from the list of intents.
  • Now we will develop a graphical user interface. Let’s use the Tkinter library which is shipped with tons of useful libraries for GUI.
  • We will take the input message from the user and then use the helper functions we have created to get the response from the bot and display it on the GUI. Here is the full source code for the GUI.

7. Run the chatbot

  • To run the chatbot, we have two main files; and
  • First, we train the model using the command in the terminal:
  • If we don’t see any error during training, we have successfully created the model. Then to run the app, we run the second file.
  • The program will open up a GUI window within a few seconds. With the GUI you can easily chat with the bot.


Looking for the Spanish version of this article, click here

Check out the code on my GitHub.

Thanks for reading. If you found the article useful don’t forget to clap and do share it with your friends and colleagues. :) If you have any questions, feel free to reach out to me.
Connect with me on 👉 LinkedIn, Github :)

Full Stack Developer | Python | ReactJS | NodeJS | Tech Writer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store