-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchatbot.py
More file actions
173 lines (122 loc) · 5.34 KB
/
Copy pathchatbot.py
File metadata and controls
173 lines (122 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import json
import pickle
import nltk
import numpy as np
from keras.models import Sequential, load_model
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import SGD
from nltk.stem.porter import PorterStemmer
ps = PorterStemmer()
#Loading the Json File
with open("chat.json") as file:
bot = json.load(file)
try:
with open("data.pickle", "rb") as f:
words, labels, train, output = pickle.load(f)
except Exception as err:
print(f"Data could not be read due to {err}")
#Creating List to be used for iterations
words = []
labels = []
intent_part_x = []
intent_part_y = []
#Iterating through the Json File and appending it to neccessary list
for intent in bot["intents"]:
for pattern in intent["patterns"]:
token = nltk.wordpunct_tokenize(pattern) #Splitting the Pattern into tokens using NLTK
#Since token is a list no need of appending we just use the function extend
words.extend(token)
intent_part_x.append(token)
intent_part_y.append(intent["tag"])
if intent["tag"] not in labels:
labels.append(intent["tag"])
words = [ps.stem(w.lower()) for w in words] #Stemming the word
words = sorted(list(set(words)))
labels = sorted(labels) #Sorting the Label list Alpahbetically
train = []
output = []
out_empty = [0 for _ in range(len(labels))] #Creating a list of 0 to be used for the Bag of Words list
for x, doc in enumerate(intent_part_x):
bow = []
token = [ps.stem(w) for w in doc]
for w in words:
if w in token:
bow.append(1)
else:
bow.append(0)
output_row = out_empty[:]
output_row[labels.index(intent_part_y[x])] = 1
train.append(bow)
output.append(output_row)
train = np.array(train)
output = np.array(output)
with open("data.pickle", "wb") as f:
pickle.dump((words, labels, train, output), f)
try:
model = load_model("model.h5")
except Exception as err:
print(f'Error Message: {err}')
#Depp Learning Nueral Net Layers to be used
model = Sequential()
model.add(Dense(128, input_shape=(len(train[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, input_shape=(len(train[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(32, input_shape=(len(train[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(output[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 the model to the train and output for prediction
model.fit(train, output, epochs=200, batch_size=8, verbose=1)
model.save("model.h5") #Saving the model
#Functions for the Chat Bot
def bag_of_words(sentence, words):
"""
Input:
This functions create a bag of word for the sentence the user enters
Output:
It returns a numpy array to be used
Note: The Numpy array was reshaped to an array of 79 because the length of our train[0] is 79
If you make changes or edit the json file to suit your needs then the Length of your train will change
Do well to play around the codes, make changes to suit your need and understand how it works
"""
bag = [0 for i, word in enumerate(words)]
sentence_token = nltk.wordpunct_tokenize(sentence)
sentence_token = [ps.stem(word.lower()) for word in sentence_token]
for sentences in sentence_token:
for i, word in enumerate(words):
if word == sentences:
bag[i] = 1
return np.array(bag).reshape(-1, 79)
def chat():
"""
Input:
This is function that Activate the ChatBot for the User to Interact with it
Output:
It gives a Responses randomly.
We selected a threshold to prevent it from giving out irrelevatnt response to the user
You can quit talking with the Chat Bot by type quit in the chat Column
Note: The Threshold was not selected Randomly, it was done after sereies of conversation with the bot to know,
which threshold it starts giving out irrelevant responses if the responses is not in it database
"""
print("Start Talking with the Bot,To Stop type quit\n")
while True:
inp = input("You : ")
if inp.lower() == "quit":
break
input_data = [bag_of_words(inp, words)]
results = model.predict(input_data)[0]
results_index = np.argmax(results)
tag = labels[results_index]
#print(f"Model Prediction: {results[results_index]}")
if results[results_index] >= 0.876:
#Looping through the json file
for tags in bot["intents"]:
if tags["tag"] == tag:
responses = tags["responses"]
print(f"Chat Bot: {np.random.choice(responses)}\n")
else:
print("Chat Bot: I dont quite Understand, Ask another question\n")
chat()