How amazing it is to talk to someone by asking and telling anything and Not being judged at all, That’s the beauty of a chatbot. A chatbot is an AI-based software that comes under the application of NLP which deals with users to handle their specific queries without Human interference.
Table of Contents
- Brief on Chatbots
- What is the need for Chatbots
- Types of Chatbots
- Rule-Based Chatbots
- Self-Learning Chatbots
- Chatbot using Python
- Chatbot using Python and NLTK
- End Notes
Brief Intro on Chatbot
A chatbot is a smart application that reduces human work and helps an organization to solve basic queries of the customer. Today most of the companies, business from different sector makes use of chatbot in a different way to reply their customer as fast as possible. chatbots also help in increasing traffic of site which is top reason of business to use chatbots.
Chatbot asks for basic information of customers like name, email address, and the query. If a query is simple like product fault, booking mistake, need some information then without any human connection it can solve it automatically and If some problem is high then It passes the details to the human head and helps customer to connect with organization manager easily. And most of the customers like to deal and talk with a chatbot.
Why we need Chatbots?
- Cost and Time Effective ~ Humans cannot be active on-site 24/7 but chatbots can and the replying power of chatbots is much fast than humans.
- Cheap Development cost ~ with the advancement in technology many tools are developed that help easy development and integration of chatbots with little investment.
- Human Resource ~ Today Chatbots can also talk with text o speech technology so it gives the feel as a human is talking on another side.
- Business Branding ~ Businesses are changing with technology and chatbot is one out of them. Chatbot also helps in advertising, branding of organization product and services and give daily updates to users.
Types of Chatbots
There are mainly 2 types of chatbots.
1) Rule-based Chatbots – As the Name suggests, there are certain rules on which chatbot operates. Like a Machine learning model, we train the chatbots on user intents and relevant responses, and based on these intents chatbot identifies the new user’s intent and response to him.
2) Self-learning chatbots – Self-learning bots are highly efficient because they are capable to grab and identify the user’s intent on their own. they are build using advanced tools and techniques of Machine Learning, Deep Learning, and NLP. Self-learning bots are further divided into 2 subcategories.
- Retrieval-based chatbots:- Retrieval-based it is somewhat the same as Rule-based where predefined input patterns and responses are embedded.
- Generative-Based chatbots:- It is based on the same phenomenon as Machine Translation build using sequence 2 sequences neural network.
Most of the organization uses self-learning chatbot along with embedding some rules like Hybrid version of both methods which makes chatbot powerful to handle each situation during a conversation with a customer.
Build one for you using Python
Now we have an immense understanding of the theory of chatbots and their advancement in the future. Let’s make our hands dirty by building one simple rule-based chatbot using python for ourselves.
We will design a simple GUI using the Python Tkinter module using which we will create a text box and button to submit user intent and on the action, we will build a function where we will match the user intent and respond to him on his intent. If you do not have the Tkinter module install, then first install it using the pip command.
pip install tkinter
from tkinter import * root = Tk() root.title("Chatbot") def send(): send = "You -> "+e.get() txt.insert(END, "n"+send) user = e.get().lower() if(user == "hello"): txt.insert(END, "n" + "Bot -> Hi") elif(user == "hi" or user == "hii" or user == "hiiii"): txt.insert(END, "n" + "Bot -> Hello") elif(e.get() == "how are you"): txt.insert(END, "n" + "Bot -> fine! and you") elif(user == "fine" or user == "i am good" or user == "i am doing good"): txt.insert(END, "n" + "Bot -> Great! how can I help you.") else: txt.insert(END, "n" + "Bot -> Sorry! I dind't got you") e.delete(0, END) txt = Text(root) txt.grid(row=0, column=0, columnspan=2) e = Entry(root, width=100) e.grid(row=1, column=0) send = Button(root, text="Send", command=send).grid(row=1, column=1) root.mainloop()
Explanation – First we have created a blank window, After that, we created a text field using the entry method and a Button widget which on triggering calls the function send, and in return, it gets the chatbot response. We have used a basic If-else control statement to build a simple rule-based chatbot. And you can interact with the chatbot by running the application from the interface and you can see the output as below figure.
Implementing Chatbot using Python NLTK Library
NLTK stands for Natural language toolkit used to deal with NLP applications and chatbot is one among them. Now we will advance our Rule-based chatbots using the NLTK library. Please install the NLTK library first before working using the pip command.
pip instal nltk
First thing is to import the library and classes we need to use.
import nltk from nltk.chat.util import Chat, reflections
|
- reflections – Another import we have done is reflections which is a dictionary containing basic input and corresponding outputs. You can also create your own dictionary with more responses you want. if you print reflections it will be something like this.
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" }
let’s start building logic for the NLTK chatbot.
After importing the libraries, First, we have to create rules. The lines of code given below create a simple set of rules. the first line describes the user input which we have taken as raw string input and the next line is our chatbot response. You can modify these pairs as per the questions and answers you want.
pairs = [ [ r"my name is (.*)", ["Hello %1, How are you today ?",] ], [ r"hi|hey|hello", ["Hello", "Hey there",] ], [ r"what is your name ?", ["I am a bot created by Analytics Vidhya. you can call me crazy!",] ], [ r"how are you ?", ["I'm doing goodnHow about You ?",] ], [ r"sorry (.*)", ["Its alright","Its OK, never mind",] ], [ r"I am fine", ["Great to hear that, How can I help you?",] ], [ r"i'm (.*) doing good", ["Nice to hear that","How can I help you?:)",] ], [ r"(.*) age?", ["I'm a computer program dudenSeriously you are asking me this?",] ], [ r"what (.*) want ?", ["Make me an offer I can't refuse",] ], [ r"(.*) created ?", ["Raghav created me using Python's NLTK library ","top secret ;)",] ], [ r"(.*) (location|city) ?", ['Indore, Madhya Pradesh',] ], [ 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 ?", ["Messy","Ronaldo","Roony"] ], [ r"who (.*) (moviestar|actor)?", ["Brad Pitt"] ], [ r"i am looking for online guides and courses to learn data science, can you suggest?", ["Crazy_Tech has many great articles with each step explanation along with code, you can explore"] ], [ r"quit", ["BBye take care. See you soon :) ","It was nice talking to you. See you soon :)"] ], ]
After creating pairs of rules, we will define a function to initiate the chat process. The function is very simple which first greet the user, and ask for any help. And the conversation starts from here by calling a Chat class and passing pairs and reflections to it.
def chat(): print("Hi! I am a chatbot created by Analytics Vidhya for your service") chat = Chat(pairs, reflections) chat.converse() #initiate the conversation if __name__ == "__main__": chat()
We have created an amazing Rule-based chatbot just by using Python and NLTK library. The nltk.chat works on various regex patterns present in user Intent and corresponding to it, presents the output to a user. Let’s run the application and chat with your created chatbot.
End Notes
Chatbots are the top application of Natural Language processing and today it is simple to create and integrate with various social medial handle and websites. Today most Chatbots are created using tools like Dialogflow, RASA, etc. This was a quick introduction to chatbots to present an understanding of how businesses are transforming using Data science and artificial Intelligence.
Thank you for following the article till the end. If you have any queries please post them in the comment section below. If you like the article then please give a read to my other articles too through this link.
About the Author
Raghav Agrawal
I am pursuing my bachelor’s in computer science. I am very fond of Data science and big data. I love to work with data and learn new technologies. Please feel free to connect with me on Linkedin.