-
Notifications
You must be signed in to change notification settings - Fork 1
/
iSMRITIChatbot.py
319 lines (222 loc) · 7.35 KB
/
iSMRITIChatbot.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
########## PART 1 - DATA PREPROCESSING ##########
#OPENING SYNTHETIC FILES
questions = open('address of file', encoding = 'utf-8', errors = 'ignore').read().split('\n')
answers= open('address of file', encoding = 'utf-8', errors = 'ignore').read().split('\n')
# Doing a first cleaning of the texts
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"how's", "how 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"n't", " not", 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
# Cleaning the questions
clean_questions = []
for question in questions:
clean_questions.append(clean_text(question))
clean_questions=clean_questions[:621]
# Cleaning the answers
clean_answers = answers
clean_answers=clean_answers[:621]
clean_answersa=np.array(clean_answers)
#CREATING WORD DICTIONARY
#CREATING BAG OF WORDS MODEL
t={}
j=0
for i in np.unique(clean_answers):
t[i]=j
j=j+1
l=0
for k in clean_answers:
clean_answers[l]=t[k]
l=l+1
from keras.utils import to_categorical
Y=to_categorical(np.array(clean_answers))
word2count = {}
for question in clean_questions:
for word in question.split():
if word not in word2count:
word2count[word] = 1
else:
word2count[word] += 1
threshold_questions = 0
questionswords2int = {}
word_number = 0
for word, count in word2count.items():
if count >= threshold_questions:
questionswords2int[word] = word_number
word_number += 1
print(max(questionswords2int.values()))
X=[]
print(len(Y))
for i in clean_questions:
x=[]
for h in i.split():
x.append(int(questionswords2int[h]))
X.append(x)
X=np.array(X)
#TRAIN TEST SPLIT
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.20, random_state = 1)
#Multi hot encoding the questions
def multi_hot_encode(sequences, dimension):
result=[]
for i in sequences:
g=[0]*dimension
for h in i:
g[h]=1
result.append(g)
result=np.array(result)
return result
x_train = multi_hot_encode(X_train, len(questionswords2int)+1)
x_test = multi_hot_encode(X_test, len(questionswords2int)+1)
#KERAS NN MODEL
import keras
from keras.models import Sequential
from keras.layers import Dense
model=Sequential()
model.add(Dense(199,input_dim=x_train.shape[1],activation='sigmoid'))
model.add(Dense(380,input_dim=199,activation='sigmoid'))
model.add(Dense(64,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#COMPILED THE MODEL
def fit(model):
history=model.fit(x_train, Y_train, epochs=1000, batch_size=100, verbose=1)
history_dict=history.history
scores=model.evaluate(x_test,Y_test,verbose=1)
return scores,history_dict
scores,history_dict = fit(model)
Accuracy=scores[1]*100
print('Accuracy of your model is')
print(scores[1]*100)
#CREATING FILE FOR UN-ANSWERED QUESTIONS
f= open("unansweredquestions.txt","a")
print("Enter bye to exit")
#PREDICTING THE ANSWERS
while(True):
try:
question = input("You: ")
if question == 'bye':
break
ques=clean_text(question)
ques=ques.split()
y=0
for h in ques:
if h not in questionswords2int:
f.write("\r\n")
f.write(question)
ques[y]="_"
y=y+1
id_to_word = {value:key for key,value in questionswords2int.items()}
id_to_ans = {value:key for key,value in t.items()}
train=[]
for i in ques:
train.append(questionswords2int[i])
test=np.zeros(x_test.shape[1])
for i in train:
test[i]=1
test=np.array([test])
#CREATING HIGHEST PROBABILITY ANSWER
s=sorted((model.predict(test)[0]))
b=np.array(s[63])
c = np.setdiff1d(s,b)
max2=max(c)
print(max((model.predict(test)[0])))
print(max2)
if (max((model.predict(test)[0]))<0.50):
#CHECKING IF PROBABILITY IS TOO LOW TO BE SURE
print("Sorry,I could not understand you")
continue
elif (max((model.predict(test)[0]))-max2<0.18):
#CHECKING IF ANOTHER ANSWER HAS A HIGH PROBABILITY
print("You are confusing me")
continue
answer=id_to_ans[np.argmax(model.predict(test))]
print('ChatBot: ' + answer)
except:
f.write("\r\n")
f.write(question)
print("I: Can you please ask something relevent")
continue
f.close()
#GUI For
def tkinput(ques):
try:
question = ques
if question == 'bye' or question=='shutup' or question=='get lost' or question=='get out' or question=='getout' or question=='getlost':
window.destroy()
return None
ques=clean_text(question)
ques=ques.split()
y=0
for h in ques:
if h not in questionswords2int:
ques[y]="_"
y=y+1
id_to_word = {value:key for key,value in questionswords2int.items()}
id_to_ans = {value:key for key,value in t.items()}
train=[]
for i in ques:
train.append(questionswords2int[i])
test=np.zeros(x_test.shape[1])
for i in train:
test[i]=1
test=np.array([test])
s=sorted((model.predict(test)[0]))
b=np.array(s[63])
c = np.setdiff1d(s,b)
max2=max(c)
print(max((model.predict(test)[0])))
print(max2)
if (max((model.predict(test)[0]))<0.50):
print("Sorry,I could not understand you")
answer="Sorry,I could not understand you"
v=[answer,str(s[63])]
return v
elif (max((model.predict(test)[0]))-max2<0.18):
answer="You are confusing me"
v=[answer,str(s[63])]
return v
answer=id_to_ans[np.argmax(model.predict(test))]
print('ChatBot: ' + answer)
answer=('ChatBot: ' + answer)
v=[answer,str(s[63])]
return v
except:
print("I: Can you please ask something relevent")
answer="I: Can you please ask something relevent"
v=[answer,"0"]
return v
from tkinter import *
window = Tk()
window.title("Welcome to iSMRITI Chatbot")
window.geometry('913x450')
lbl = Label(window, text="", padx=5, pady=5,width=100,height=5)
lbln1 = Label(window, text="", padx=5, pady=5,width=100,height=5)
lbl.grid(column=1, row=0)
lbl1=Label(window, text="", padx=5, pady=5,width=100,height=5)
lbl.grid(column=1, row=5)
lbl1.grid(column=1, row=6)
txt = Entry(window,width=70)
txt.grid(column=1, row=1)
lbln1 = Label(window, text="", padx=5, pady=5,width=100,height=5)
lbl.grid(column=1, row=2)
lbln1 = Label(window, text="", padx=5, pady=5,width=100,height=5)
lbl.grid(column=1, row=4)
def clicked():
lbl.configure(text=tkinput(txt.get())[0])
lbl1.configure(text="Answer's Probability:"+tkinput(txt.get())[1])
btn = Button(window, text="Get An Answer", command=clicked,width=100,height=5)
btn.grid(column=1, row=3)
window.mainloop()