Simple Chatbot using NLTK
NLTK stands for
Natural language toolkit used to deal with NLP applications. A chatbot is one
among those applications. This post explains Rule-based chatbots using the NLTK
library.
Terms you should
know.
Chat – Chat is a class that contains complete logic for processing
the text data which the chatbot receives. |
Reflections – Reflections is a dictionary containing basic input and corresponding outputs. You can create your own dictionary with more responses you want.
Steps:
1) Please install the NLTK library first
before working using the pip command.
pip
install nltk
2) Next is to import
the library and classes we need to use.
import nltk
from nltk.chat.util import Chat,
reflections
3) After importing
the libraries, 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. The nltk.chat works on
various regex patterns present in user Intent and corresponding to it, presents
the output to a user.
Here is
the Code:
import nltk
from nltk.chat.util import Chat, reflections
pairs = [
[
r"my
name is (.*)",
["Hello
%1, How are you today ?",]
],
[
r"hi|hey|hello",
["Hello",
"Hey there","Namskar"] # it outputs one of this
response randomly
],
[
r"what
is your name ?",
["I
am a bot created to assist you. You can call me Rupali-Bot!",]
],
[
r"how
are you ?",
["I'm
Super fine ! Hope are too",]
],
[
r"sorry
(.*)",
["Its
alright", "Never mind", "It
happens, don't mind"]
],
[
r"I
am fine",
["Great
to hear that, How can I help you?",]
],
[
r"what
(.*) want ?",
["Make
me an offer I can't refuse",]
],
[
r"(.*)
created ?",
["Rupali
created me using Python's NLTK library ","It is a secret",]
],
[
r"(.*)
(location|city) ?",
['Nasik,
Maharshtra',]
],
[
r"how
is weather in (.*)?",
["Weather
in %1 is awesome like always","%1 has always fantastic weather
","Not Too cold not too hot in %1",]
],
[
r"i
work in (.*)?",
["%1
is an Amazing company, I have heard about it. But make sure to get more
details.",]
],
[
r"how
(.*) health(.*)",
["I'm
a computer program, so I'm always at best of my health ", "Super
Fine",]
],
[
r"(.*)
(sports|game) ?",
["I'm
a very big fan of IPL thats Cricket",]
],
[
r"who
(.*) sportsperson ?",
["Virat
Kohli","Sachin Tendulakr","M.S. Dhoni"]
],
[
r"who
(.*) (moviestar|actor)?",
["Aamir
Khan", "Pariniti Chopra",]
],
[
r"quit",
["BBye
take care. See you soon :) ","It was nice talking to you. See you
soon :)"]
],
]
def chat():
print("Hi! I am a
chatbot created by Rupali for your assistance")
chat = Chat(pairs,
reflections)
chat.converse()
#initiate the conversation
if __name__ == "__main__":
chat()
-------------------------------------- The
output---------------------------------------
Will you try and see it ?
Ok..here is it !!
Today most Chatbots are created using tools like Dialogflow, RASA, etc.
No comments:
Post a Comment