-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.py
189 lines (117 loc) · 4.82 KB
/
temp.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
import nltk
from nltk.stem.lancaster import LancasterStemmer
Stemmer = LancasterStemmer()
import numpy
import tflearn
import json
import tensorflow
import random
import pickle
import speech_recognition as sr
import pyttsx3
#Openning the intents.json file and assigning it to a variable, data.
with open("intents.json") as file:
data = json.load(file)
words = []
docs_x = []
docs_y = []
labels = []
#try to open the previosly loaded data through a pickle file if not exists the create and load one.
try:
with open("xyz.pickle","rb") as f:
words,labels,training,output = pickle.load(f)
except:
for intent in data["intents"]:
for pattern in intent["patterns"]:
# Tokenizing and appending all the values of list patterns into word
wrds = nltk.word_tokenize(pattern)
words.extend(wrds)
#words = ["Hi","How","are","you","Is","anyone"....]
docs_x.append(wrds)
#docs_x = [["Hi"],["How","are","you"],["Is","anyone","there"]...]
docs_y.append(intent["tag"])
if intent["tag"] not in labels:
labels.append(intent["tag"])
#labels = ["greeting","greeting","greeting"...."]
words = [Stemmer.stem(w) for w in words if w not in "?"]
words = sorted(list(set(words)))
labels = sorted(labels)
training = []
output = []
out_empty = [0 for _ in range(len(labels))]
for x,doc in enumerate(docs_x):
bag = []
wrds = [Stemmer.stem(w) for w in doc]
for w in words:
if w in wrds:
bag.append(1)
else:
bag.append(0)
output_row = out_empty[:]
output_row[labels.index(docs_y[x])] = 1
training.append(bag)
output.append(output_row)
training = numpy.array(training)
output = numpy.array(output)
#if xyz.pickle does not exists then make a tuple of words,labels,training,output and dump it in yhe pickle file
with open("xyz.pickle","wb") as f:
pickle.dump((words,labels,training,output),f)
tensorflow.compat.v1.reset_default_graph()
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)
try:
model.load("model.tflearn")
except:
model.fit(training, output, n_epoch = 1000, batch_size = 8, show_metric = True)
model.save("model.tflearn")
def bag_of_words(s,words):
bag = [0 for _ in range(len(words))]
s_word = nltk.word_tokenize(s)
s_word = [Stemmer.stem(w) for w in s_word if w not in "?"]
for x in s_word:
for i,y in enumerate(words):
if x == y:
bag[i] = 1
return numpy.array(bag)
#This function takes input from the user as speech and converts it into text.
def take_input():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
text = r.recognize_google(audio)
return text
except:
print("Sorry could not recognize your voice")
chat()
#Initializing chat functions let's us communicate with the bot.
def chat():
print("Hey there! I am SNAP. How can I help you ?\n")
while True:
inp = take_input()
print(inp)
if inp in ["quit","I am done","Quit","Done","Thank you"]:
break
else:
result = model.predict([bag_of_words(inp,words)])
print(result)
print(labels)
if(result.max() < 0.2):
print("Sorry I don't know about that.")
else:
result_index = numpy.argmax(result)
print(result_index)
tag = labels[result_index]
for tg in data["intents"]:
if tg["tag"] == tag:
responses = tg["responses"]
break
#The code below converts the text output to speech.
text_speech = pyttsx3.init()
text_speech.say(random.choice(responses))
text_speech.runAndWait()
chat()