Build a Python Chatbot using Keras & NLTK

Through this tutorial, you will build a Chatbot capable of responding some of your messages after learning certain patterns the user can introduce. You require good python programming skills and basic knowledge about neural networks and deep learning.


  • Tensorflow 2.5.0
  • Keras 2.4.3
  • nltk 3.6.2
  • numpy 1.19.5
>>> import nltk
  • Lemmatizing is the process of converting a word into its lemma form, we use this while predicting.
  • Lemma is the canonical form, dictionary form, or citation form of a set of words (headword).
  • A class will be a category of a list and each has a tag.
  • JSON (JavaScript Object Notation) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays.
  • A document is a combination between patterns and intents.
  • The script to build the model and train our chatbot is inside the file.
  • Words.pkl — This is a pickle file in which we store the words Python object that contains a list of our vocabulary.
  • The list of categories (classes) is inside of the pickle file called classes.pkl
  • The trained model that contains information about the model and has weights of the neurons is model.h5
  • Implemented GUI where users can easily interact with our chatbot is


Lemmatizing is the process of converting a word into its lemma form.


  • bow : It calls clean_up_sentence function, once the pattern is tokenized checks if words are in the vocabulary and if are found in any bag to finally return it.
  • predict_class : It calls bow function, filters predictions using a threshold and then sort the list by strength of probability.
  • chatbot_response : Main function of getting a response, it uses user input and the model we created.