NLTK Chatbot Tutorial

NLTK Chatbot Tutorial

Introduction

        As Machine Learning and Artificial Intelligence are getting popular day by day, chatbots are getting popular too. I am writing a 2 part tutorial to show you as to how you can build chatbots both simple and complex. This post is the Part 1 tutorial. Let start with the basics in this tutorial. Natural Language Toolkit (NLTK) is one of the basic things that you need to know to build chatbots as per your requirements.

NLP

        Natural-language processing (NLP) is an area of computer science and artificial intelligence concerned with the interactions between computers and human (natural) languages, in particular how to program computers to fruitfully process large amounts of natural language data.

NLTK

        NLTK stands for Natural Language Toolkit and is a suite of open source Python modules, data sets, and tutorials supporting research and development in Natural Language Processing (NLP). To know more about NLTK please check: NLTK Website.

Requirements to Run the Application:

1. Anaconda
2. Intellij IDE with Python Community Edition Plugin

 Anaconda bundles up Python installation and the most widely used Python libraries for your machine.  If Anaconda is not installed, then Python needs to be installed separately and the individual Python libraries need to be downloaded using the pip install command which would be very time consuming. Hence Anaconda should be setup on your machine. To setup and verify if Anaconda is installed, please refer to my post on: Anaconda Setup.

After installing Anaconda, in Anaconda Prompt, use the following command to install NLTK:
conda install nltk

The chatbot built in this tutorial is based on NLTK Chatbot Examples. In this tutorial I will help you to build a Basic NLTK Chatbot in a few simple steps.

Step 1: Create file: alpha.py

I have named my chatbot as Alpha. In alpha.py, we will be setting up the NLTK chatbot.

Here is alpha.py:
from __future__ import print_function
from nltk.chat.util import Chat, reflections

pairs = (
    (r'quit',
     ( "Thank you. It was good talking to you.",
       "Good-bye.")),

    (r'Who are you(.*)',
     ( "My name is Alpha.",
       "I'm Alpha.")),

    (r'What is your name(.*)',
     ( "My name is Alpha.",
       "I'm Alpha.")),

    (r'What do you do(.*)',
     ( "I speak my mind out.",
       "I chat.",
       "I like making friends.")),

    (r'Then(.*)',
     ( "Nothing special. You tell me.",
       "What else?",
       "Nothing from me. What about you?")),

    (r'How are you(.*)',
     ( "I am fine. Thank You. How are you?",
       "I am fine. How about you?",
       "Fine. Thank You. How about you?")),

    (r'Hi(.*)',
     ( "Hi!!! What is your name?",
       "Hello.",
       "Hi. Great to meet you.")),

    (r'My name is (.*)',
     ( "Hi %1!!!",
       "Hello %1.",
       "Hi %1. Great to meet you.")),

    (r'You tell me(.*)',
     ( "What?",
       "Tell me about yourself.",
       "WHat do you do?")),

    (r'What else(.*)',
     ( "Nothing special. You tell me.",
       "Nothing from me. What about you?")),

    (r'I need (.*)',
     ( "Why do you need %1?",
       "Why?",
       "Are you sure?")),

    (r'How (.*)',
     ( "I am not aure.",
       "I'm not sure. What do you think?",
       "Sorry, I don't know.")),

    (r'Hello(.*)',
     ( "Hi... How are you today?",
       "Hello, How are you?")),

    (r'Yes',
     ( "You seem certain.",
       "Alright")),

    (r'My (.*)',
     ( "I see, your %1.",
       "Oh ok.")),

    (r'(.*)\?',
     ( "Why do you ask that?",
       "I am not sure.",
       "I do not know the answer to your question")),

    (r'(.*) (happy|cheerful|glad|good)',
     ( "%1 %2",
       "%1 %2")),

    (r'(.*)',
     ( "Alright.",
       "I see.",
       "Very interesting."))
)

alpha_chatbot = Chat(pairs, reflections)

def alpha_chat():
    print("Alpha\n---------")
    print("You can talk to me by typing in English.")
    print('Enter "quit" to Quit.')
    print('='*72)
    print("Hello. My name is Alpha. Tell me something about yourself..")

    alpha_chatbot.converse()

def demo():
    alpha_chat()

if __name__ == "__main__":
    demo()
In this file I am using 2 imports from nltk.chat.util:
  1. Chat: This is a class that has all the logic that is used by the chatbot. The code: alpha_chatbot.converse() will start a conversation with the chatbot.
  2. reflections: This is a dictionary that contains a set of input values and its 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 nltk.chat.util, you will see its values as below:
  3. 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 for this:
    my_reflections= {
        "are"     : "am",
        "was"    : "were"
    }
    
    And use it as:
    alpha_chatbot = Chat(pairs, my_reflections)
    
Then if you check the code above, I am using a variable named as pairs. It is a list of tuple. The tuple consists of patterns and responses. A Pattern is nothing but a regular expression that matches the user input and for each pattern there is a list of possible responses. If the user input matches a pattern mentioned, then the program will randomly select and reply with any one of the responses from the list of corresponding responses.

To see reflections in action in your code, define use the following code in pairs 
(r'(.*) (happy|cheerful|glad|good)',
     ( "%1 %2",
       "%1 %2"))
As per above code, here is a sample chatbot interaction using reflections:
>I am glad
you are glad
>you are good
I am good
>i was happy
you were happy

Step 2: Create file: setup.py

setup.py is used to to build and install the application. Add basic information about the application in setup.py. Once you have this file created, you can build and install the application using the commands:
python setup.py build
python setup.py install
Here is setup.py:
from setuptools import setup

setup(name='NLTKBot',
      version='1.0.0',
      description='A Simple NLTK Chatbot'
      )

Run Application:

1. To run the application, in Anaconda Prompt, navigate to your project location and execute the command:
python alpha.py
2. To run the application in Intellij IDE, right click the file alpha.py and click Run 'alpha'

Sample Chat:

Hello. My name is Alpha. Tell me something about yourself..
>my name is Aj Tech Developer
Hello aj tech developer.
>how are you?
Fine. Thank You. How about you?
>i am fine too.
Alright.
>i am happy
you are happy
>you are good
I am good
>what do you do?
I speak my mind out.
>what else?
Nothing special. You tell me.
>quit
Good-bye.

Conclusion:

    In this post I have explained in simple steps as to how you can build your own chatbot using NLTK. The code used in this post is available on GitHub.
    To learn more advanced concepts used by chatbots, you can read my part 2 tutorial post on ML and NLP Chatbot which uses Machine Learning and learns as you interact with it.
    Learn the most popular and trending technologies like Machine Learning, Angular 5, Internet of Things (IoT), Akka HTTP, Play Framework, Dropwizard, Docker, Netflix Eureka, Netflix Zuul, Spring Cloud, Spring Boot and Flask in simple steps by reading my most popular blog posts at Software Developer Central.
    If you like my post, please feel free to share it using the share button just below this paragraph or next to the heading of the post. You can also tweet with #SoftwareDeveloperCentral on Twitter. To get a notification on my latest posts or to keep the conversation going, you can follow me on Twitter. Please leave a note below if you have any questions or comments.     

Comments

Popular Posts

Golang gRPC Microservice

Dropwizard MySQL Integration Tutorial

Asynchronous Processing (@Async) in Spring Boot