Simple Chatbot by Guru.K
Using the 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 nltk.chat.util import Chat, reflections pairs = [ [ r"my name is (.*)", ["Hello %1, How are you today ?",] ], [ r"what is your name ?", ["My name is Chitti 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 :)",] ], [ r"hi|hey|hello", ["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 ?", ["Guru.K 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. ",] ], [ 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","Dhoni"] ], [ r"who (.*) (moviestar|actor)?", ["Rajnikanth"] ], [ r"quit", ["Bye take care. See you soon :) ","It was nice talking to you. See you soon :)"] ], ] def Chitti(): print("Hi, I'm Chitti 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) chat.converse() if __name__ == "__main__": chatty()
As you can see we have just hardcoded the probable question and answers in the list pairs.
Lets interact with Chitti :
Hi, I'm Chitti and I chat alot ;) Please type lowercase English language to start a conversation. Type quit to leave >hi Hello >what's your location? Chennai, Tamil Nadu >how is weather in Chennai? Weather in chennai? is awesome like always >is it raining there None >is it raining in Chennai? Damn its raining too much here in chennai? >which actor you like most? None >who is your favorite actor? Rajnikanth >I work for Urbantree Infrastures Pvt Ltd None >I work in Urbantree Company urbantree company is an Amazing company, I have heard about it. >hi Hello >how are you? I'm doing good How about You ? >I am also doing good None >I'm also doing good Nice to hear that >What is your age I'm a computer program dude Seriously you are asking me this? >Sorry mini It's alright
The nltk.chat 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 article 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
[email protected] / +91 8667722919