By Mukul Kumar
This is a simple and quick chatbot for conversational purposes based on a rule-based approach using Natural Language Toolkit in Python.
INTRODUCTION
The Chatbots appear to have tremendous promises in providing quick and convenient assistance to the users specifically to answer their queries. So, I have developed a conversational purpose simple and quick chatbot using a well-known natural language toolkit (NLTK) in python in this project.
CODE EXPLANATION
Step 1: Initially, import the libraries
from nltk.chat.util import Chat from nltk.chat.util import reflections
Step 2: Now, create the chatbots list of recognizable patterns and responses to those patterns. To do this, I have created a variable called pairs.
#Pairs is a list of patterns and responses. pairs = [ [ r"(.*)my name is (.*)", ["Hello %2, How are you today ?",] ], [ r"(.*)help(.*) ", ["Please tell me, I may help you ",] ], [ r"(.*) your name ?", ["My name is theintelligentbot, but you can just call me smartbot or simply chatbot .",] ], [ r"how are you (.*) ?", ["I'm doing very well", "i am great !"] ], [ r"sorry (.*)", ["Its alright","Its OK, never mind that",] ], [ r"i'm (.*) (good|well|okay|ok)", ["Nice to hear that","Alright, great !",] ], [ r"(hi|hey|hello|hola|holla)(.*)", ["Hello", "Hey there",] ], [ r"what (.*) want ?", ["Make me an offer I can't refuse",] ], [ r"(.*)created(.*)", ["Mukul Kumar created me using Python's NLTK library ","top secret ;)","some good programmer"] ], [ r"(.*) (location|city) ?", ['Mumbai, India',] ], [ r"(.*)raining in (.*)", ["No rain in the past 4 days here in %2","In %2 there is a 50% chance of rain",] ], [ r"how (.*) health (.*)", ["Health is very important, but I am a computer, so I don't need to worry about my health ",] ], [ r"(.*)(sports|game|sport)(.*)", ["I'm a very big fan of Cricket",] ], [ r"who (.*) (Cricketer|Batsman)?", ["Rohit Sharma"] ], [ r"quit", ["Bye for now. See you soon :) ","It was nice talking to you. See you soon :)"] ], [ r"(.*)", ['That is nice to hear'] ], ]
Step 3: Now, let's print the Reflections. Reflections are a dictionary file containing a set of input values and corresponding output values and the following code will print all the required Reflections.
print(reflections)
Step 4: Even, anyone can create their own customized reflections dictionary in the acceptable format as mentioned below.
my_dummy_reflections= { "go" : "gone", "hello" : "hey there" }
Step 5: Now, it's a code to print the default message whenever the chatbot starts and code to create the chatbot.
#this is the default message at the start of chat print("Hi, I'm theintelligentbot and I like to chat\nPlease type lowercase English language to start a conversation. Type quit to leave ") #Create Chat Bot chat = Chat(pairs, reflections)
Step 6: Command to start a conversation with an intelligent chatbot.
#Start conversation chat.converse()
CONVERSATION RESULT
Here, Human queries are in bold letters and Chatbot replies are in non-bold letters-
Hi, I'm theintelligentbot and I like to chat
Please type lowercase English language to start a conversation. Type quit to leave
>hi
Hello
>what is your name?
My name is theintelligentbot, but you can just call me smartbot or simply chatbot.
>who created you?
top secret ;)
>will you please help me?
Please tell me, I may help you
>do you like sports?
I'm a very big fan of Cricket
>who is your favourite cricketer?
Rohit Sharma
>bye
That is nice to hear
>quit
Bye for now. See you soon :)
Submitted by Mukul Kumar (mukul102000)
Download packets of source code on Coders Packet
Comments