top of page
  • Writer's pictureNagesh Singh Chauhan

Build your first chatbot using Python NLTK

“A chatbot (also known as a talkbot, chatterbot, Bot, IM bot, interactive agent, or Artificial Conversational Entity) is a computer program or an artificial intelligence which conducts a conversation via auditory or textual methods. Such programs are often designed to convincingly simulate how a human would behave as a conversational partner, thereby passing the Turing test. Chatbots are typically used in dialog systems for various practical purposes including customer service or information acquisition. Some chatterbots use sophisticated natural language processing systems, but many simpler systems scan for keywords within the input, then pull a reply with the most matching keywords, or the most similar wording pattern, from a database.”

— source Wikipedia

Chatbots are not very new, one of the foremost of this kind is ELIZA, which was created in the early 1960s and is worth exploring. In order to successfully build a conversational engine, it should take care of the following things:

1. Understand who is the target audience 2. Understand the Natural Language of communication. 3. Understand the intent or desire of the user 4. provide responses that can answer the user

Today we will learn to create a simple chat assistant or chatbot using Python’s NLTK library.

NLTK has a module,, which simplifies building these engines by providing a generic framework.

In this blog, I am using 2 imports from

Chat: This is a class that has all the logic that is used by the chatbot.

Reflections: This is a dictionary that contains a set of input values and corresponding output values. It is an optional dictionary that you can use. You can also create your own dictionary in the same format as below and use it in your code. If you check, you will see its values as below:

reflections = {
  "i am"       : "you are",
  "i was"      : "you were",
  "i"          : "you",
  "i'm"        : "you are",
  "i'd"        : "you would",
  "i've"       : "you have",
  "i'll"       : "you will",
  "my"         : "your",
  "you are"    : "I am",
  "you were"   : "I was",
  "you've"     : "I have",
  "you'll"     : "I will",
  "your"       : "my",
  "yours"      : "mine",
  "you"        : "me",
  "me"         : "you"

You can also create your own reflections dictionary in the same format as above and use it in your code. Here is an example of this:

my_dummy_reflections= {
    "go"     : "gone",
    "hello"    : "hey there"

'and use it as :

chat = Chat(pairs, my_dummy_reflections)

Using the above concept from python’s NLTK library lets build a simple chatbot without using any of the Machine Learning or Deep Learning Algorithms. So obviously our chatbot will be a decent one but not an intelligent one.

Source Code :

from import Chat, reflectionspairs = [
        r"my name is (.*)",
        ["Hello %1, How are you today ?",]
        r"what is your name ?",
        ["My name is Chatty and I'm a chatbot ?",]
        r"how are you ?",
        ["I'm doing good\nHow about You ?",]
        r"sorry (.*)",
        ["Its alright","Its OK, never mind",]
        r"i'm (.*) doing good",
        ["Nice to hear that","Alright :)",]
        ["Hello", "Hey there",]
        r"(.*) age?",
        ["I'm a computer program dude\nSeriously you are asking me this?",]
        r"what (.*) want ?",
        ["Make me an offer I can't refuse",]
        r"(.*) created ?",
        ["Nagesh created me using Python's NLTK library ","top secret ;)",]
        r"(.*) (location|city) ?",
        ['Chennai, Tamil Nadu',]
        r"how is weather in (.*)?",
        ["Weather in %1 is awesome like always","Too hot man here in %1","Too cold man here in %1","Never even heard about %1"]
        r"i work in (.*)?",
        ["%1 is an Amazing company, I have heard about it. But they are in huge loss these days.",]
        r"(.*)raining in (.*)",
        ["No rain since last week here in %2","Damn its raining too much here in %2"]
        r"how (.*) health(.*)",
        ["I'm a computer program, so I'm always healthy ",]
        r"(.*) (sports|game) ?",
        ["I'm a very big fan of Football",]
        r"who (.*) sportsperson ?",
        r"who (.*) (moviestar|actor)?",
        ["Brad Pitt"]],
        ["BBye take care. See you soon :) ","It was nice talking to you. See you soon :)"]],
def chatty():
        print("Hi, I'm Chatty and I chat alot ;)\nPlease type lowercase English language to start a conversation. Type quit to leave ") #default message at the start    chat = Chat(pairs, reflections)
if __name__ == "__main__":

The code is quite simple, still, let's understand it.

Once the function chatty() is invoked, a default message will be displayed:

Next, I’ve created an instance of Chat class containing pairs(list of tuples containing a set of questions and answers) and reflections(discussed above).

The next step is to trigger the conversation:


A simple conversation :

A simple conversation with Chatty

As you can see we have just hardcoded the probable question and answers in the list pairs.

Let's interact more with Chatty :

Some more conversation with Chatty

The chatbots work on the regex of keywords present in your question. So you can add any number of questions in a proper format so that your chatbot doesn’t get confused in determining the regex.

In this blog, I have explained in simple steps as to how you can build your own chatbot using NLTK, and of course its not an intelligent one.

I hope you guys have enjoyed reading.

Happy Learning !!!

For any doubts/suggestions connect with me over LinkedIn.

This article was also published on KDnuggets.

181 views0 comments

Recent Posts

See All


bottom of page