Python Chatbot Project
Python Chatbot Project
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.
Let’s create a retrieval based chatbot using NLTK, Keras, Python, etc.
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.
pip install tensorflow, keras, pickle, nltk
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
lemmatizer = WordNetLemmatizer()
import json
import pickle
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import SGD
import random
words=[]
classes = []
documents = []
ignore_words = ['?', '!']
data_file = open('intents.json').read()
intents = json.loads(data_file)
This is how our intents.json file looks like.
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
nltk.word_tokenize() function and append each word in the words list. We also
create a list of classes for our tags.
# 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]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(train_y[0]), activation='softmax'))
# Compile model. Stochastic gradient descent with Nesterov accelerated gradient gives good results for this
model
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
#fitting and saving the model
hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
model.save('chatbot_model.h5', hist)
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 nltk
from nltk.stem import WordNetLemmatizer
lemmatizer = 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())
words = pickle.load(open('words.pkl','rb'))
classes = pickle.load(open('classes.pkl','rb'))
To predict the class, we will need to provide input in the same way as we did while
training. So we will create some functions that will perform text preprocessing and
then predict the class.
def clean_up_sentence(sentence):
# tokenize the pattern - 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 pattern
sentence_words = clean_up_sentence(sentence)
# bag of words - matrix of N words, vocabulary matrix
bag = [0]*len(words)
for s in sentence_words:
for i,w in enumerate(words):
if w == s:
# assign 1 if current word is in the vocabulary position
bag[i] = 1
if show_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)
res = model.predict(np.array([p]))[0]
ERROR_THRESHOLD = 0.25
results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD]
# sort by strength of probability
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
return return_list
After predicting the class, we will get a random response from the list of intents.
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 can
easily chat with the bot.
Screenshots:
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.