We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
Python Chatbot Project — using NLTK & Keras
How amazing it is to tell someone everything and anything and not being
judged at all. A top class feeling it is and that’s what the beauty of a chatbot
is.
What is Chatbot?
A chatbot is an intelligent piece of software that is capable of communicating and
performing actions similar to a human. Chatbots are used a lot in customer
interaction, marketing on social network sites and instantly messaging the client.
‘There are two basic types of chatbot models based on how they are built; Retrieval
based and Generative based models.
1) ThaPe > LeetDiIOy > aaatacpones » nietonoe 4
a ovens
enone (rains
soe
iaevs
Pater
b
1 Retrieval based Chatbots
Aretrieval-based chatbot uses predefined input patterns and responses. It then uses
some type of heuristic approach to select the appropriate response. It is widely used
in the industry to make goal-oriented chatbots where we can customize the tone and
flow of the chatbot to drive our customers with the best experience.
2. Generative based Chatbots
Generative models are not based on some predefined responses.They are based on seq 2 seq neural networks. It is the same idea as machine
translation. In machine translation, we translate the source code from one language
to another language but here, we are going to transform input into an output. It
needs a large amount of data and it is based on Deep Neural networks.
About the Python Project — Chatbot
In this Python project with source code, we are going to build a chatbot using deep
learning techniques. The chatbot will be trained on the dataset which contains
categories (intents), pattern and responses. We use a special recurrent neural
network (LSTM) to classify which category the user's message belongs to and then we
will give a random response from the list of responses.
Let's create a retrieval based chatbot using NLTK, Keras, Python, etc.
Download Chatbot Code & Dataset
The dataset we will be using is ‘intents.json’. This is a JSON file that contains the
patterns we need to find and the responses we want to return to the user.
Prerequisites
The project requires you to have good knowledge of Python, Keras, and Natural
language processing(NLTK). Along with them, we will use some helping modules
which you can download using the python-pip command.
‘tall tensorflow, keras, pickle, nltk
Pi
How to Make Chatbot in Python?
Now we are going to build the chatbot using Python but first, let us see the file
structure and the type of files we will be creating:
adjushup“ =
Intents.json — The data file which has predefined patterns and responses.
train_chatbot.py — In this Python file, we wrote a script to build the model
and train our chatbot.
Words.pkl— This is a pickle file in which we store the words Python object
that contains a list of our vocabulary.
Classes.pkl— The classes pickle file contains the list of categories.
Chatbot_model.h5 — This is the trained model that contains information
about the model and has weights of the neurons.
Chatgui.py - This is the Python script in which we implemented GUI for
our chatbot. Users can easily interact with the bot.
Here are the 5 steps to create a chatbot in Python from scratch:
Bere
. Import and load the data file
. Pre-process data
. Create training and testing data
. Build the model
5.
Predict the response
1. Import and load the data file
First, make a file name as train_chatbot.py. We import the necessary packages for
our chatbot and initialize the variables we will use in our Python project.
The data
file is in JSON format so we used the json package to parse the JSON file
into Python.
import nltk
from nltk.stem import WordNetLemmatizer
Jemmatizer = WordNetLemmatizer()
import jsonimport pickle
import numpy as np
from keras models import Sequential
fiom keras.layers import Dense, Activation, Dropout
from keras.optimizers import SGD
import random
wort
classes = []
documents = []
ignore_words
']
‘pent intents json’).read()
intents = jsontoads(data_file)
This is how our intents.json file looks like.
ow are you", "Ig anyone
ola", *HsL1o", "Good day"l,
Sai there, how can f help?*
", "Sam you Later", "Goodbye", "ice chatting to you, bys
2" you'™, sauve a nice day", ste! Caw back again 200."
wna next ese")
that's helpfal*, "awesome, thanks", “Thaoks Zor helping
xy timel™, “HY ploacure"|.
} cantt underetend you", “lease give me nore into", ‘Wot sure I underoten.
(icagts soptions!
io you could help me?) “khat yoo con do?", “what help you provider", Mew you
T can guide You through Adverse doug zeastion List, Blood pressure tacking, Ho
(eagt: tadverse_azug",
i3e to theck Adverse devs reaction’, "Oren adverse deuss module"
[Sauigutina vo movers Geun sesceien nosulem
vo me 9 1s
2. Pre-process data
When working with text data, we need to perform various preprocessing on the data
before we make a machine learning or a deep learning model. Based on the
requirements we need to apply various operations to preprocess the data.
Tokenizing is the most basic and first thing you can do on text data. Tokenizing is the
process of breaking the whole text into small parts like words.
Here we iterate through the patterns and tokenize the sentence using
nitk,word_tokenize() function and append each word in the words list. We also
create a list of classes for our tags.
for intent in intents[intefor pattern in intemt|‘patterns'):
#tokenize each word
w=nltk.word_tokenize (pattem)
words.extend(w)
#add documents in the corpus
documents.append((w, intent|'tag")))
# add to our classes list
if intemt('tag’) not in clas
classes.appendintent'tay
Now we will lemmatize each word and remove duplicate words from the list.
Lemmatizing is the process of converting a word into its lemma form and then
creating a pickle file to store the Python objects which we will use while predicting.
# lemmatize, lower each word and remove duplicates,
‘words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words]
words = sorted (list(set(words)))
# sort classes
classes = sorted (list set(classes)))
# documents = combination between patterns and intents
print (len(documents), "documents")
# classes = intents
print (Jen(classes). "cla
# words = all words, vocabulary
print (len(words), "unique lemmatized words", words)
pickle.dump(words,open(‘words.pkI','wb’))
Pickle.dump(classes.open(‘classes.pkIWb'))
g and testing data
Now, we will create the training data in which we will provide the input and the
output. Our input will be the pattern and output will be the class our input pattern
belongs to. But the computer doesn’t understand text so we will convert text into
numbers.
our training data
# create an empty array for our output
coutput_empty = [0] * len(classes)
# training set, bag of words for each sentence
for doc in documents:
+ initialize our bag of words
bag =)
# list of tokenized words forthe pattem
pattem_words = doc[0]# lemmatize each word -
te base word, in attempt to represent related words
pattern_words = [lemmatizer.lemmatize(word.lower()) for word in pattern_words]
# create our bag of words array with 1, if word match found in eurre
pattern
for w in words:
bag.append(1 pattern_words else bag.append(0)
# output is a0" for each tag and 'I' for current tag (flor each pattern)
‘output_row = list(output_empty)
‘output_row(classes.index(doe[])
training.append{[bag, output_row))
# shuffle our features and turn into np.array
random.shu(le(training)
ing)
€ train and test Lists. X - pattems, Y - intents
training = np.array (ta
Fon
train_x = list(training|:.0))
train_y »
print(""Training data created")
4. Build the model
We have our training data ready, now we will build a deep neural network that has 3,
layers. We use the Keras sequential API for this. After training the model for 200
epochs, we achieved 100% accuracy on our model. Let us save the model as
‘chatbot_model.hs’.
st(training|
# Create model - 3 layers, First layer 128 neurons, second layer 64 neurons and 3rd output layer contains
number of neurons
# equal to number of intents to predict output intent with softmax
model = Sequential)
model.add(Dense( 128, input_shape(len(train_x[0)),), aetivation='relu’))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu’))
model.add(Dropout(0.5))
model.add(Dense(en(train_y[0)), aetivatio
# Compile model. Stochastic gradient des
‘soflmax’))
1 with Nesterov accelerated gradient gives good results for this
model
Bs
model.compile(loss~categorical_crossentropy’, optimizer=ygd, metris
SGD(I=0.01, decay=1e-6, momentum=0.9, nesterov=True)
fitting and sa
ig the model
hist = model.fit(np.array/train_x), np.array(train_y), epochs=200, batch_$
5", hist)
model saver‘chatbot_mode!
print("model created")
5. Predict the response (Graphical User Interface)To predict the sentences and get a response from the user to let us create a new file
‘chatapp.py’.
We will load the trained model and then use a graphical user interface that will
predict the response from the bot. The model will only tell us the class it belongs to,
so we will implement some functions which will identify the class and then retrieve
us a random response from the list of responses.
Again we import the necessary packages and load the ‘words.pkl’ and ‘classes.pkl’
pickle files which we have created when we trained our model:
import nitk
from nltk.stem import WordNetLemmatizer
Jemm: WordNetLemmatizer()
import pickle
import numpy as np
from keras.models import load_model
model = load_model (‘chatbot_model.h5')
import json
import random
intents = json.loads(open( intents json’).read))
ickle.load(open( words pkl
piekle.load open
To predict the class, we will need to provide input in the same way as we did while
words =
classes
training. So we will create some functions that will perform text preprocessing and
then predict the class.
defclean_up_sentence(sente:
# tokenize the patter - split words into array
sentence_words = nltk.word_tokenize sentence)
# stem each word - create short form for word
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words)
return sentence_words
# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
def bow(sentence, words, show_details=True):
# tokenize the pattem
sentence_words=clean_up_sentence(senten
)
# bag of words - matrix of N words, vocabulary matrix
bag = (0]*len(words)
for
in sentence_words:
for iw in enumerate( words):
itw=s:# assign 1 if current word is in the vocabulary position
bagii]=1
ifshow details:
print ("found in bag: %s" % w)
‘return(np.array(bag))
def predict_class(sentence, model):
# filter out predictions below a threshold
p=bow(sentence, words.show details—False)
‘model_predict(np.array((p)){0]
ERROR_THRESHOLD =0.25
results = [fir] for ir in enumerate(res) if ERROR_THRESHOLD]
# sort by strength of probability
results somt(key=lambda x: x[1}, reverse=True)
return_ist = ()
for rin results:
return_list.append{ {"intent”: classes(r{0)), "probability": strr{1))})
return return ist
After predicting the class, we will get a random response from the list of intents.
res
def getResponse (ints, intents_json):
tag = ints[O]['inten]
list_of intents = intents_json{'intents’)
for i in list_of_intents:
inti(tag’}= tag):
result = random choiee(i('responses'})
break
return result
def chatbot_response(text):
ints = predict_class(text, model)
res= getResponse(ints, intents)
return res
Now we will develop a graphical user interface. Let's use Tkinter library which is
shipped with tons of useful libraries for GUI. We will take the input message from
the user and then use the helper functions we have created to get the response from
the bot and display it on the GUI. Here is the full source code for the GUT.
#Creating GUI with tkinter
import tkinter
from tkinter import *
def send):
msg = EntryBox.get("I.0",end-Ie’) strip)
EntryBox.delete("0.0",END)if msg !=":
ChatLog.contig(stat=NORMAL)
Chatl og insert
D, "You: "+ msg +
1442265
ChatLog.config(foregroun
res = chatbot_response(msg)
Chatl.og.insert(END, "Bot: "+ res * “rin!)
ChatLog.config(state=DISABLED)
ChatLog.yview(END)
base = TK)
baseatitle("Hello")
base.geometry("400x500")
base.resizable(width=PALSE, heigh
reate Chat window
ChatLog = Text(base, bd=0, be="white", heigh
Chatl_og.config(state-DISABLED)
#Bind scrollbar to Chat window
erdana”, 12 ))
‘ALSE)
". width="50", font="Arial",)
scrollbar = Scrollbar(bas
command=ChatLog.yview, cursor="heart")
serollbar.set
ChatLog{'yscrolleommand!
reate Button to send message
SendButton = Button(base, font=(" Verdana’ 12,"bold'), text="Send”, width="12", height=5,
bd=0, bg="#32de97", activebackground="#3e9d9",fe="#IN1T
command= send )
Create the box to enter mess
EntryBox = Text(base, bu=0, bg="white" width="29", height="5", font="Arial”)
#EntryBox.bind("", send)
#Place all components on the sereen
scrollbar place(x=376,y=6, height=386)
=6, height=386, width=370)
128, y=401, height=90, width=265)
ChatLog place(x=6.
EntryBox place(
SendButton place(x=6, y=401, height=90)
base.mainloop()
6. Run the chatbot
To run the chatbot, we have two main files; train_chatbot.py and chatapp.py.
First, we train the model using the command in the terminal:
python train_chatbot.py
If we don’t see any error during training, we have successfully created the model.
‘Then to run the app, we run the second file,
python chatgui.py
‘The program will open up a GUI window within a few seconds. With the GUI you ean
easily chat with the botScreenshots:
Il Select C’vincons\System2cmd.ne
Cre PE SP UETETMTENTESyihon train chotbot.p
caer)
POneoneas
sses [‘adverse_drug’, ‘blood pressure’,
cere opt ion: rst
EMS nean ne iset Rees
Peed A oat
Phave’, ‘hello’, * erates) a
eae ERE
Caan M
arr)
ere ee CT cr ae ese eae acer
Sees Cee Se ORC SSC Soran a
ere
rat
Perey
ara pee
# Helio F
You: Hello there, How are you?
Bot: HI there, how ean T help?
You: what can you do?
Bot: 1 can guide you through Adverse drug +
eaction list, Blood pressure tracking, Hospit
Sis and Pharmacies
You: thanks you
Bot: My pleasure
You: see ya got to gol
Bot: See yout
Summary
In this Python data science project, we understood about chatbots and implemented
a deep learning version of a chatbot in Python which is accurate. You can customize
the data according to business requirements and train the chatbot with great
accuracy. Chatbots are used everywhere and all businesses are looking forward to
implementing bot in their workflow.