What we need for this tutorial:
- Basic knowledge of Python
- Understanding of Python syntaxes
- Basic of Machine Learning
- Editor of your choice
- Some modules (You can install it by “pip install module_name” on cmd or terminal).
First of all, I’m gonna create our data which will be used to train our chat bot. I’m gonna create response.json and add data is suitable json format.
{
"data": [
{
"type": "greetings",
"questions": [
"Hello",
"Hi",
"Good Morning",
"Good Evening",
"Good Afternoon"
],
"answers": [
"Hello",
"Hi",
"Good Morning to you too",
"Good Evening"
]
},
{
"type": "about",
"questions": [
"What are you?",
"Who are you?",
"Tell me about yourself"
],
"answers": [
"Hello, I'm a Bot",
"Hello, I'm a talking bot created in python",
"I'm a bot, Nice to meet you"
]
},
{
"type": "help",
"questions": [
"Help Me.",
"Can you help me?",
"Do you mind helping me?"
],
"answers" : [
"Sure, How can I help you?",
"I'm always here to help, Just ask.",
"Yeah sure, Ask me."
]
}
]
}
Let’s discuss basic of this data, The Questions are here to train our chat bot, type is so chatbot can decide what type of question was given and Answers will be returned from this data.
So let’s create our train.py, This need to be run everytime you make change to your response.json.
First of all I’m going to import all neccessary modules.
import json
import numpy as np
import tensorflow as tf
import tflearn
Now let’s get our data using json module.
data = []
with open("response.json") as f:
d = json.load(f)
for s in d["data"]:
data.append(s)
So our chat bot understands numbers so we need to convert everything to numbers, This is called Tokenization. So I’m going to create two dictionaries which will be used to convert types to int and vice-versa.
types = [d["type"] for d in data]
types2int = {types[i]: i for i in range(0, len(types))}
int2types = {v: k for k, v in types2int.items()}
Now we will get every word from our questions and convert them to numbers. First we need to remove punctuations and refine some words. I have built a functions.py and it contains some functions.
import re
def clean_text(text):
text = text.lower()
text = re.sub(r"i'm", "i am", text)
text = re.sub(r"he's", "he is", text)
text = re.sub(r"she's", "she is", text)
text = re.sub(r"that's", "that is", text)
text = re.sub(r"what's", "what is", text)
text = re.sub(r"where's", "where is", text)
text = re.sub(r"\'ll", " will", text)
text = re.sub(r"\'ve", " have", text)
text = re.sub(r"\'re", " are", text)
text = re.sub(r"\'d", " would", text)
text = re.sub(r"won't", "will not", text)
text = re.sub(r"can't", "cannot", text)
text = re.sub(r"[!-()\"#/@;:<>{}+=~|.?,]", "", text)
return text
def get_bag(question, words2int):
question = clean_text(question)
bag = []
for b in range(50):
bag.append(0)
i = 0
for word in question.split():
index = words2int.get(word)
if index is None:
index = len(words2int) + 1
words2int[word] = index
bag[i] = index
i+=1
return bag
Now import this functions.py to train.py and convert words to int.
from functions import clean_text, get_bag
words2int = {}
i = 1
for d in data:
for question in d["questions"]:
question = clean_text(question)
for word in question.split():
word = clean_text(word)
if words2int.get(word) is None:
words2int[word] = i
i += 1
So now we have our words2int dictionary. Now let’s get our training data, I’m gonna create train_x and train_y lists.
train_x = []
train_y = []
for d in data:
for question in d["questions"]:
train_x.append(get_bag(question, words2int))
output = []
for i in range(len(types2int)):
output.append(0)
output[types2int[d["type"]]] = 1
train_y.append(output)
train_x = np.array(train_x)
train_y = np.array(train_y)
So our get_bag function converts string to list of numbers and train_y list will contain 0s except where index of type is matched. For example, train_y will contain list [1, 0, 0] when the type is “greetings” and [0, 1, 0] when the type is “about”.
Now let’s create our model.
tf.compat.v1.reset_default_graph()
net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.dropout(net, 0.4)
net = tflearn.fully_connected(net, 8)
net = tflearn.dropout(net, 0.4)
net = tflearn.fully_connected(net, len(train_y[0]), activation="softmax")
net = tflearn.regression(net)
model = tflearn.DNN(net)
Now let’s train our data with train_x and train_y, We can do this by fit function and set epoch to 1000 and Save it to our models folder as model.tflearn
model.fit(train_x, train_y, n_epoch=1000, batch_size=8, show_metric=True)
model.save('models/model.tflearn')
Now dumping words2int, types2int and int2types as data.json. This will be used in our main.py file.
with open('data.json', 'w') as f:
dump_data = {}
dump_data["words2int"] = words2int
dump_data["types2int"] = types2int
dump_data["int2types"] = int2types
json.dump(dump_data, f)
We are done with our train.py. Moving forward with our main.py. Importing necessary modules.
import tflearn
import tensorflow as tf
import json
import random
import numpy as np
from functions import get_bag
Now let’s get our data from data.json and response.json.
int2types = {}
types2int = {}
words2int = {}
data = []
with open("response.json") as f:
d = json.load(f)
for s in d["data"]:
data.append(s)
with open('data.json') as f:
dump_data = json.load(f)
int2types = dump_data["int2types"]
types2int = dump_data["types2int"]
words2int = dump_data["words2int"]
Let’s load our model.
tf.compat.v1.reset_default_graph()
net = tflearn.input_data(shape=[None, 50])
net = tflearn.fully_connected(net, 8)
net = tflearn.dropout(net, 0.4)
net = tflearn.fully_connected(net, 8)
net = tflearn.dropout(net, 0.4)
net = tflearn.fully_connected(net, len(int2types), activation="softmax")
net = tflearn.regression(net)
model = tflearn.DNN(net)
model.load('models/model.tflearn')
Now let’s ask user question and then output answer.
while True:
question = input("Text: ")
prediction = model.predict(np.array([get_bag(question, words2int)]))
result = int2types[str(np.argmax(prediction))]
for d in data:
if d["type"] == result:
answers = d["answers"]
print("Response:",answers[random.randint(0, len(answers)-1)])
Now our chatbot is complete, All we need to do is train it by running trian.py and then our main.py.
Training Chatbot
Running Chatbot
You can download this source code from my Github.