Skip to content

Commit a925fe9

Browse files
committed
Added main and intents
1 parent 6f0e63e commit a925fe9

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

intents.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{"intents": [
2+
{"tag": "greeting",
3+
"patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up"],
4+
"responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"],
5+
"context_set": ""
6+
},
7+
{"tag": "goodbye",
8+
"patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day"],
9+
"responses": ["Sad to see you go :(", "Talk to you later", "Goodbye!"],
10+
"context_set": ""
11+
},
12+
{"tag": "age",
13+
"patterns": ["how old", "how old is tim", "what is your age", "how old are you", "age?"],
14+
"responses": ["I am 23 years old!"],
15+
"context_set": ""
16+
},
17+
{"tag": "name",
18+
"patterns": ["what is your name", "what should I call you", "whats your name?"],
19+
"responses": ["You can call me JuanCa.", "I'm JuanCa!"],
20+
"context_set": ""
21+
},
22+
{"tag": "shop",
23+
"patterns": ["Id like to buy something", "whats on the menu", "what do you reccommend?", "could i get something to eat"],
24+
"responses": ["We sell chocolate chip cookies for $2!", "Cookies are on the menu!"],
25+
"context_set": ""
26+
},
27+
{"tag": "hours",
28+
"patterns": ["when are you guys open", "what are your hours", "hours of operation"],
29+
"responses": ["We are open 7am-4pm Monday-Friday!"],
30+
"context_set": ""
31+
}
32+
]
33+
}

main.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import nltk
2+
from nltk.stem.lancaster import LancasterStemmer
3+
import numpy as np
4+
import tflearn
5+
import tensorflow as tf
6+
import random
7+
import json
8+
import pickle
9+
import os
10+
11+
# nltk.download('punkt')
12+
steamer = LancasterStemmer()
13+
14+
with open("intents.json") as file:
15+
data = json.load(file)
16+
17+
if os.path.exists("data.pickle"):
18+
with open("data.pickle", "rb") as file:
19+
words, labels, training, output = pickle.load(file)
20+
else:
21+
words = []
22+
labels = []
23+
docs_x = []
24+
docs_y = []
25+
26+
for intent in data["intents"]:
27+
for pattern in intent["patterns"]:
28+
word_list = nltk.word_tokenize(pattern)
29+
words.extend(word_list)
30+
docs_x.append(word_list)
31+
docs_y.append(intent["tag"])
32+
33+
if intent["tag"] not in labels:
34+
labels.append(intent["tag"])
35+
36+
words = [steamer.stem(w.lower()) for w in words if w != "?"]
37+
words = sorted(list(set(words)))
38+
39+
labels = sorted(labels)
40+
41+
training = []
42+
output = []
43+
44+
out_empty = [0 for _ in labels]
45+
46+
for x, doc in enumerate(docs_x):
47+
bag = []
48+
49+
word_list = [steamer.stem(w) for w in doc]
50+
51+
for w in words:
52+
if w in word_list:
53+
bag.append(1)
54+
else:
55+
bag.append(0)
56+
57+
output_row = out_empty[:]
58+
output_row[labels.index(docs_y[x])] = 1
59+
60+
training.append(bag)
61+
output.append(output_row)
62+
63+
training = np.array(training)
64+
output = np.array(output)
65+
66+
with open("data.pickle", "wb") as file:
67+
pickle.dump((words, labels, training, output), file)
68+
69+
tf.compat.v1.reset_default_graph()
70+
71+
net = tflearn.input_data(shape=[None, len(training[0])])
72+
net = tflearn.fully_connected(net, 8)
73+
net = tflearn.fully_connected(net, 8)
74+
net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
75+
net = tflearn.regression(net)
76+
77+
model = tflearn.DNN(net)
78+
if os.path.exists("model.tflearn.meta"):
79+
model.load("model.tflearn")
80+
else:
81+
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
82+
model.save("model.tflearn")
83+
84+
85+
def bag_of_words(sentence, words_list):
86+
bag_words = [0 for _ in words_list]
87+
88+
s_words = nltk.word_tokenize(sentence)
89+
s_words = [steamer.stem(word.lower()) for word in s_words]
90+
91+
for se in s_words:
92+
for i, w in enumerate(words_list):
93+
if w == se:
94+
bag_words[i] = 1
95+
96+
return np.array(bag_words)
97+
98+
99+
def chat():
100+
print("Start talking")
101+
while True:
102+
inp = input("You: ")
103+
if inp.lower() == "quit":
104+
break
105+
106+
result = model.predict([bag_of_words(inp, words)])[0]
107+
result_index = np.argmax(result)
108+
109+
if result[result_index] > 0.8:
110+
label = labels[result_index]
111+
112+
responses = []
113+
for tag in data["intents"]:
114+
if tag["tag"] == label:
115+
responses = tag["responses"]
116+
print(random.choice(responses))
117+
else:
118+
print("I don't quite understand. Please ask a different question")
119+
120+
121+
chat()

0 commit comments

Comments
 (0)