-
Notifications
You must be signed in to change notification settings - Fork 0
/
lu_v1.py
136 lines (112 loc) · 3.24 KB
/
lu_v1.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
import tensorflow
import json
import numpy
import random
import nltk
import pickle
from tensorflow import keras
words=[]
docs_x=[]
labels=[]
docs_y=[]
stemmer= nltk.PorterStemmer()
try:
x
with open("data.pickle","rb") as f:
words,labels,training,output = pickle.load(f)
except:
with open("intents.json") as intent:
data = json.load(intent)
for x in data['intents']:
for y in x['patterns']:
wrds=nltk.word_tokenize(y)
words.extend(wrds)
docs_x.append(wrds)
docs_y.append(x["tag"])
if x['tag'] not in labels:
labels.append(x['tag'])
words=[stemmer.stem(w.lower()) for w in words]
words=sorted(list(set(words)))
#print(docs_x)
labels=sorted(labels)
training=[]
output=[]
#print(classes)
out_empty=[0 for _ in range(len(labels))]
#print(out_empty)
for x,doc in enumerate(docs_x):
bag=[]
# print(x,doc)
wrds = [stemmer.stem(w.lower()) for w in doc if w not in "?"]
# print(wrds)
for w in words:
if w in wrds:
#print(w,wrds)
bag.append(1)
else:
# print(w,wrds)
bag.append(0)
output_row = out_empty[:]
output_row[labels.index(docs_y[x])]=1
# print(x,doc)
# print(docs_y[x])
# print(labels.index(docs_y[x]))
training.append(bag)
output.append(output_row)
with open("data.pickle","wb") as f:
pickle.dump((words, labels, training, output),f)
#print(output)
#print(training)
training = numpy.array(training)
output = numpy.array(output)
#print(training)
tensorflow.compat.v1.reset_default_graph()
#print(len(training[0]))
#print(training[0])
#print(numpy.size(training))
#print(len(words))
model=keras.Sequential(
[
keras.layers.Dense(48,input_shape=(len(words),)),
keras.layers.Dense(24,activation="relu"),
keras.layers.Dense(12,activation="softmax"),
keras.layers.Dense(len(output[0]),activation="softmax")
]
)
keras_model_path = "/tmp/lu_v1"
try:
keras.models.load_model(keras_model_path)
print("happy me")
except:
model.compile(optimizer='adam',loss='mse',metrics=['accuracy'])
model.fit(training,output,epochs=6000)
# model.save(keras_model_path)
def bag(data,word):
value=[]
data=str(data)
data=nltk.word_tokenize(data)
data=[stemmer.stem(d.lower()) for d in data]
for x in word:
if x in data:
value.append(1)
else:
value.append(0)
value=numpy.array([value])
return value
def chat():
with open("intents.json") as intent:
d = json.load(intent)
print("Lets chat:- :) ")
while True:
data = input("")
if data.lower() == "quit":
break
else:
inten=""
results=model.predict((bag(data,words)))
tag_val=numpy.argmax(results)
for x in d['intents']:
if x['tag'] == labels[tag_val]:
inten=x['responses']
print(random.choice(inten))
chat()