-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatbot_Main.py
More file actions
119 lines (109 loc) · 3.75 KB
/
Chatbot_Main.py
File metadata and controls
119 lines (109 loc) · 3.75 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
from nltk import metrics, stem
import tensorflow as tf
from tensorflow.keras.layers import Dense,Dropout
import tflearn
import numpy as np
import nltk
nltk.download('punkt')
from nltk.stem.snowball import SnowballStemmer
import json
import random
import pickle
import os
import matplotlib.pyplot as plt
with open("chatbotDataSet.json") as file:
data = json.load(file)
words=[]
labels=[]
train_x=[]
train_y=[]
#storing the intents and their respective patterns
for intents in data['intents']:
for patterns in intents['patterns']:
patterns = patterns.lower()
#spliting the each pattern into space seperated words by removing punctuations(Tokenization)
wrds=nltk.word_tokenize(patterns)
words.extend(wrds)
train_x.append(wrds)
train_y.append(intents['tag'])
if intents['tag'] not in labels:
labels.append(intents["tag"])
stemmer = SnowballStemmer("english")
words=[stemmer.stem(w.lower()) for w in words if w!='?']
words=sorted(list(set(words)))
labels=sorted(labels)
training=[]
output=[]
output_rep=[0 for x in range(len(labels))]
for x,doc in enumerate(train_x):
wrds=[stemmer.stem(w) for w in doc]
b=[1 if j in wrds else 0 for j in words]
output_row=output_rep[:]
output_row[labels.index(train_y[x])]=1
training.append(b)
output.append(output_row)
####converting the data into arrays
training=np.array(training)
output=np.array(output)
#### storing the data
with open("data.pickle","wb") as f:
pickle.dump((words,labels,training,output),f)
####
net = tflearn.input_data(shape = [None, len(training[0])])
net = tflearn.fully_connected(net,8)
net = tflearn.fully_connected(net,8)
net = tflearn.fully_connected(net,len(output[0]), activation = "softmax")
net = tflearn.regression(net)
model = tflearn.DNN(net)
model.fit(training, output, n_epoch = 200, batch_size = 8, show_metric = True)
model.save("chatbot1_model")
def getResponse(ints, intents_json):
tag = ints[0]['intent']
list_of_intents = intents_json['intents']
for i in list_of_intents:
if(i['tag']== tag):
result = random.choice(i['responses'])
break
return result
def chatbot_response(text):
ints = predict_class(text, model)
res = getResponse(ints, intents)
return res
#Creating GUI with tkinter
import tkinter
from tkinter import *
def send():
msg = EntryBox.get("1.0",'end-1c').strip()
EntryBox.delete("0.0",END)
ChatLog.config(state=NORMAL)
ChatLog.insert(END, "You:" + msg + '\n\n')
ChatLog.config(foreground="#442265", font=("Verdana", 12 ))
res = chatbot_response(msg)
ChatLog.insert(END, "ChatBot:" + res + '\n\n')
ChatLog.config(state=DISABLED)
ChatLog.yview(END)
base = Tk()
base.title("ChatBot")
base.geometry("300x500")
base.resizable(width=FALSE, height=FALSE)
#Create Chat window
ChatLog = Text(base, bd=0, bg="white", height="8", width="50", font="Arial",)
ChatLog.config(state=DISABLED)
'''
#Bind scrollbar to Chat window
scrollbar = Scrollbar(base, command=ChatLog.yview, cursor="heart")
ChatLog['yscrollcommand'] = scrollbar.set
#Create Button to send message
SendButton = Button(base, font=("Verdana",12,'bold'), text="Send", width="12", height=5,
bd=0, bg="#de3252", activebackground="#3c9d9b",fg='#ffffff',
command= send )
#Create the box to enter message
EntryBox = Text(base, bd=0, bg="white",width="29", height="5", font="Arial")
#EntryBox.bind("<Return>", send)
#Place all components on the screen
scrollbar.place(x=376,y=6, height=386)
ChatLog.place(x=6,y=6, height=386, width=370)
EntryBox.place(x=128, y=401, height=90, width=265)
SendButton.place(x=6, y=401, height=90)
base.mainloop()
'''