-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.py
214 lines (160 loc) · 6.02 KB
/
funcs.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
import nltk
from nltk.stem.lancaster import LancasterStemmer
import numpy as np
import random
import json
import pickle
from pathlib import Path
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
root = Path(dir_path)
classes_path = root / "input" / "classes.txt"
pickle_path = root / "input" / "data.pickle"
tokenizer_path = root / "input" / "tokenizer.txt"
def check_tokenizer():
try:
with open(tokenizer_path,"r") as f:
found = f.read()
if(found == 'True'):
return
except:
try:
data = nltk.data.find('tokenizers/punkt')
print("Found punkt tokenizer at {}".format(data))
with open(tokenizer_path,"w") as f:
f.write("True")
except LookupError:
print("Downloading tokenizer for processing data for the ChatBot")
nltk.download('punkt')
with open(tokenizer_path,"w") as f:
f.write("True")
def load_JSON(path):
data = None
try:
with open(path) as f:
data = json.load(f)
print("\nLoaded JSON file: {} successfully".format(path))
except:
print("\nFile not found in the path specified.Dataset not loaded successfully")
exit()
return data
def getClasses():
with open(classes_path,"r") as f:
tags = f.readlines()
tags = [x.strip() for x in tags]
return tags
def ProcessData(data,train = False):
check_tokenizer()
if(train == False):
try:
with open(pickle_path,"rb") as f:
words,labels,training,output = pickle.load(f)
print("Loaded stemmed data from pickle")
return words,labels,training,output
except:
print("Stemming data from the intents.json file")
stemmer = LancasterStemmer()
words = []
labels = []
docs_X = []
docs_y = []
with open(classes_path,"w") as f:
f.write("")
for intent in data["intents"]:
for pattern in intent["patterns"]:
wrds = nltk.word_tokenize(pattern)
words.extend(wrds)
docs_X.append(wrds)
docs_y.append(intent["tag"])
if intent["tag"] not in labels:
labels.append(intent["tag"])
with open(classes_path,"a") as f:
f.write(intent["tag"]+"\n")
#List of non-redundant words the model has seen
words = [stemmer.stem(w.lower()) for w in words if w != "?"]
words = np.array(words)
words = sorted(np.unique(words))
#labels (sorted)
labels = sorted(labels)
training = []
output = []
out_empty = [0 for _ in range(len(labels))]
for x,doc in enumerate(docs_X):
bag = np.array([])
wrds = [stemmer.stem(w.lower()) for w in doc if w != "?"]
for w in words:
if w in wrds:
bag = np.append(bag,np.array([1]))
else:
bag = np.append(bag,np.array([0]))
output_row = out_empty[:]
output_row[labels.index(docs_y[x])] = 1
training.append(bag)
output.append(np.argmax(output_row))
#into np arrays
training = np.asarray(training)
output = np.asarray(output)
with open(pickle_path,"wb") as f:
print("Stemmed data saved in pickle file...")
pickle.dump((words,labels,training,output),f)
return words,labels,training,output
else:
with open(classes_path,"w") as f:
f.write("")
print("Stemming data from the intents.json file")
stemmer = LancasterStemmer()
words = []
labels = []
docs_X = []
docs_y = []
for intent in data["intents"]:
for pattern in intent["patterns"]:
wrds = nltk.word_tokenize(pattern)
words.extend(wrds)
docs_X.append(wrds)
docs_y.append(intent["tag"])
if intent["tag"] not in labels:
labels.append(intent["tag"])
with open(classes_path,"a") as f:
f.write(intent["tag"]+"\n")
#List of non-redundant words the model has seen
words = [stemmer.stem(w.lower()) for w in words if w != "?"]
words = np.array(words)
words = sorted(np.unique(words))
#labels (sorted)
labels = sorted(labels)
training = []
output = []
out_empty = [0 for _ in range(len(labels))]
for x,doc in enumerate(docs_X):
bag = np.array([])
wrds = [stemmer.stem(w.lower()) for w in doc if w != "?"]
for w in words:
if w in wrds:
bag = np.append(bag,np.array([1]))
else:
bag = np.append(bag,np.array([0]))
output_row = out_empty[:]
output_row[labels.index(docs_y[x])] = 1
training.append(bag)
output.append(np.argmax(output_row))
#into np arrays
training = np.asarray(training)
output = np.asarray(output)
with open(pickle_path,"wb") as f:
print("Stemmed data saved in pickle file...")
pickle.dump((words,labels,training,output),f)
return words,labels,training,output
def bagOfWords(s,words):
stemmer = LancasterStemmer()
bag = [0 for _ in range(len(words))]
s_words = nltk.word_tokenize(s)
s_words = [stemmer.stem(word.lower()) for word in s_words if word != "?"]
s_words = np.array(s_words)
for se in s_words:
for i,w in enumerate(words):
if w == se:
bag[i] = 1
bag = np.array(bag)
bag.shape = (1,bag.shape[0])
return bag