/  Technology   /  Build ChatBot Using Python
Build a ChatBot Using Python

Build ChatBot Using Python

Python is a very famous language to learn. It is the fastest moving language in terms and libraries, applications that can be used in machine learning, Artificial intelligence, web development, and many other things which python has covered. With the rise of Data Science i.e. machine learning and artificial intelligence, it has come into the limelight. It is famous for its simple programming syntax, code readability which makes it more productive and easy.

Python can be used for making a web application, mobile application, machine learning algorithm, GUI application, and many more things. In this article, we will discuss how to build chatbot using python.

 

Chatbot

A chatbot is a computer program that interacts with humans or simulates a human conversation with a machine via a written message or voice. It is programmed to work independently without the intervention of human operators. Most of the chatbot answer the question by using a natural language processing system, while some chatbot answer by scanning fir keyword within the input and then reporting with the most matched keyword from the database. It responds to question based on what it knows at that point of time. Based on the above approach chatbots there are two variants of chatbots.

  1. Self learning Bots which uses machine learning based approach to chat.
  2. Rule based Bot which answers questions based on some rules on which it is trained on.

We are already using a chatbot in our day-to-day life such as Google assistant, apple’s Siri, amazon’s Alexa, or Microsoft’s Cortana. Many companies are now using a chatbot to improve and simplify their customer experience while shopping, giving the customer service or booking.

In this article, we will learn to build a chatbot using Python NLTK library. We will use rule based approach to create our bot. We will not be  using any of the Machine Learning or Deep Learning Algorithms, which means our chatbot will be a decent one but not an intelligent one.

 

NLTK

The natural language tool kit is a famous python library which is used in natural language processing. It is one of the trending platform for working with human data and developing application services which are able to understand it.

To create a simple chatbot we will be using nltk.chat module. NLTK module nltk.chat which simplifies to build the chatbot. In this article, we will use 2 imports from nltk.chat.util

which are chat and reflection.

Chat : It contains all the logic and process for conversation between the user and chatbot.

reflection : reflection is basically a dictionary that contains a set of input values and it’s corresponding output values.

 

Build ChatBot Using Python

We can also create our own reflections dictionary as per our requirement and choice. And can use it in our choice. It can be created as

my_reflections= {
    "hello"  : "hey there",
    "hi"     : "Hello!",
    "Good morning" : "I am having one"
}

 

And can use it as

chat = Chat(pairs, my_reflections)

Before using this library we first need to install nltk library. You can install it by using

pip install nltk

After installing the nltk library we are ready use it and can build chatbot using it’s nltk.chat.util module.

First I will show you a very basic program to help get started with building a chatbot.

from nltk.chat.util import Chat , reflections
pairs = [ ['My name is Akshay', ['Hi Akshay']],]
chat = Chat(pairs, reflections)
chat.converse()

Output :

 

Build ChatBot Using Python

 

We started by importing the relevant libraries. Then we created a variable called pairs which is a list of patterns or a set of rules that will be used to train our chatbot. The element in the list is the user input and the second element is the response from the bot. Next we created a chat object which contain pairs as the parameter and then used the converse() method.

As you can see the chatbot responded to ‘My name is Akshay’ because we have trained it. It returned None when we used the sentence or rule on which it is not trained. So we need to train our chatbot on each and everything we need it to answer.

So instead of training our chatbot on every single name we should use a generalized approach, where we don’t need to code explicitly. For this we can use regular expression. You can use it as

from nltk.chat.util import Chat , reflections
pairs = [ ['My name is (.*)', ['Hi %1']],]
chat = Chat(pairs, reflections)
chat.converse()

 

Output :

 

Build ChatBot Using Python

 

From the above example you must have understood that for creating a chatbot we need to train our bot on every question we need it to answer for ourselves. So now let’s proceed further to add more features to our chatbot.

 

Building the Chatbot

from nltk.chat.util import Chat , reflections
pairs = [
    [
        r"my name is (.*)",
        ["Hello %1, Nice to have you here. How can i help you",]
    ],
    [
        r"hi|hey|hello",
        ["Hello", "Hey there","hi there",]
    ],
    [
        r"what is your name?",
        ["my name is Akii",]
    ],
    [
        r"how are you ?",
        ["pretty good, thank you! Hope you are doing well!!!",]
    ],
    [
        r"I am fine, thank you",
        ["great to hear that, How can i help you?",]
    ], 
    [
        r"i'm (.*) doing good",
        ["That's great to hear","How can i help you?)",]
    ],
    [
        r"(.*) created you?",
        ["Akshay created me",]
    ],
    [
        r"how is the weather in (.*)",
        ["The weather in %1 is pretty awesome as always",]          
    ],
    [
        r"can you help(.*)",
        ["Ofcourse I can help you",]
    ],
    [
        r"(.*)(location|city)(.*)",
        ["Pune, Maharashtra",]
    ],
    [
       r"which (.*) (sport|game) ?",
        ["I Love Cricket",]
    ],
    [
        r"who (.*) sportsperson ?",
        ["Rohit Sharma",]
    ],
    [
        r"can you suggest me any online learning platform to learn python",
        ["i2 tutorials is a great option to learn python and all the things related to it. You can check their website",]
    ],
    [
        r"thank you so much, that was amazing",
        ["I am happy to help", "No problem, you're welcome",]
    ],
    [
        r"quit",
    ["Bye, Have a nice day. Hope to see you soon :) ","It was nice talking to you. Hope to see you soon :)"]
],
]
def bot():
        print("Hi, I'm Akii and I'm here to help you")
bot()
chat = Chat(pairs, reflections)
chat.converse()

 

Output :

 

 

Here first we created rules and trained our chatbot on this set of rules. These rules are created in pairs object. Next we created a chat object which contain pairs as the parameter and then used the converse() method. We also created a function bot, which prints a message whenever it is invoked that gives a good interface to our bot. You can also interact more with the chatbot.

 

Output :

 

Build ChatBot Using Python

In this article so far we have learnt how to create your own chatbot. You can also add many more questions to your chatbot and make it more advance. Hope this article will help you in creating your own chatbot.

Leave a comment