-
Notifications
You must be signed in to change notification settings - Fork 0
/
mybot-basic.py
260 lines (230 loc) · 9.03 KB
/
mybot-basic.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#######################################################
# Imports
#######################################################
import io
import json
import os
import string
import warnings
import aiml
import nltk
from keras.models import load_model
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from ImageClassification_Training import preprocess_image
import Transformer_Training as tt
# Unsafe
warnings.filterwarnings("ignore")
#######################################################
# Initialise Keras Model
#######################################################
label_map_file = "data/label_map.json"
model_dir = "data/models/pani_adam_cnn.hdf5"
with open(label_map_file, 'r') as file:
label_map = json.load(file)
model = load_model(model_dir)
#######################################################
# Initialise NLTK Agent
#######################################################
data = "data/data.txt"
readFile = io.open(data, 'r')
corpus = readFile.read()
lowerCorpus = corpus.lower()
# Uncomment these on first run
# nltk.download('punkt')
# nltk.download('wordnet')
corpal_sentences = nltk.sent_tokenize(lowerCorpus)
corpal_words = nltk.word_tokenize(lowerCorpus)
#######################################################
# Normalize tokens using Lemmatization
#######################################################
lemmer = nltk.stem.WordNetLemmatizer()
def lem_tokens(tokens):
lemmed_tokens = []
for token in tokens:
lemmed_tokens.append(lemmer.lemmatize(token))
return lemmed_tokens
# Dictionary of punctuations in unicode
remove_punct_dict = {}
for punctuation in string.punctuation:
remove_punct_dict[ord(punctuation)] = None
def lem_normalize(text):
return lem_tokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
#######################################################
# Transformer Model Interface
#######################################################
encoder_path = "data/checkpoints/encoder_epoch_68.h5"
decoder_path = "data/checkpoints/decoder_epoch_68.h5"
dataset, info = tt.create_dataset(tt.BATCH_SIZE)
encoder, decoder = tt.create_transformer(info['vocab_size'], tt.MODEL_SIZE,
info['max_length'], tt.NUM_LAYERS, tt.ATTENTION_HEADS)
encoder.load_weights(encoder_path)
decoder.load_weights(decoder_path)
print(info)
#######################################################
# POL model Interface
#######################################################
val = """
avocado => {}
mango => {}
bananas => {}
breadfruit => {}
starfruit => {}
guinep => {}
ackee => {}
limes => {}
coconut => {}
tamarind => {}
farm => f1
backyard => b1
farm2 => f2
be_in => {}
George Town => GT
Bodden Town => BT
East End => EE
North Side => NS
West Bay => WB
districts => {GT, WB, BT, EE, NS, WB}
south_of => {(WB,GT), (NS, BT)}
west_of => {(EE, NS), (EE, BT), (EE, WB), (EE, GT), (NS, GT), (NS, WB)}
"""
folval = nltk.Valuation.fromstring(val)
grammar_file = 'data/grammars/CIGrammarFile.fcfg'
objectCounter = 0
#######################################################
# Similarity-Based Response Generation
#######################################################
def response(user_response):
chatBot_Response = ""
corpal_sentences.append(user_response)
tfidf = TfidfVectorizer(tokenizer=lem_normalize, stop_words='english').fit_transform(corpal_sentences)
cos_similarity = cosine_similarity(tfidf[-1], tfidf)
idx = cos_similarity.argsort()[0][-2]
flat = cos_similarity.flatten()
flat.sort()
req_tfidf = flat[-2]
if req_tfidf == 0:
chatBot_Response += "Sorry, I don't know what that is"
return chatBot_Response
else:
chatBot_Response += "\t" + corpal_sentences[idx].split("\n")[1]
return chatBot_Response
#######################################################
# Initialise AIML agent
#######################################################
startup_dir = "data/std-startup.xml"
kern = aiml.Kernel()
kern.setTextEncoding(None)
kern.bootstrap(learnFiles=startup_dir, commands="LOAD BRAIN")
#######################################################
# Welcome user
#######################################################
welcomeMessage = ("Welcome to the Cayman Islands Chat Bot!"
"\nAsk me anything about the Cayman islands including"
" it's geography, national animals and so on."
"\n\n** NEW UPDATE **"
"\nMy owner has implemented a Transformer Model to train me on the SQuAD"
"\nDataset!, refer to their explore page for questions you can ask me.")
print(welcomeMessage)
#######################################################
# Main loop
#######################################################
while True:
# get user input
try:
userInput = input("> ")
except (KeyboardInterrupt, EOFError) as e:
print("Bye!")
break
# activate selected response agent
answer = kern.respond(userInput)
if answer[0] == '#':
params = answer[1:].split('$')
cmd = int(params[0])
if cmd == 0:
print(params[1])
break
elif cmd == 10:
print("Let me think..", end="")
print(response(userInput))
corpal_sentences.remove(userInput)
elif cmd == 4:
o = 'o' + str(objectCounter)
objectCounter += 1
folval['o' + o] = o # insert constant
try:
if len(folval[params[1]]) == 1: # clean up if necessary
if ('',) in folval[params[1]]:
folval[params[1]].clear()
folval[params[1]].add((o,)) # insert type of fruit information
if len(folval["be_in"]) == 1: # clean up if necessary
if ('',) in folval["be_in"]:
folval["be_in"].clear()
folval["be_in"].add((o, folval[params[2]])) # insert location
except nltk.sem.evaluate.Undefined:
print("Those are invalid objects, try again")
elif cmd == 5 or cmd == 6: # Are there any x in y or # Are all x in y
try:
g = nltk.Assignment(folval.domain)
m = nltk.Model(folval.domain, folval)
allOrSome = 'some '
if cmd == 6:
allOrSome = 'all '
sent = allOrSome + params[1] + ' are_in ' + params[2]
results = nltk.evaluate_sents([sent], grammar_file, m, g)[0][0]
if results[2]:
print("Yes.")
else:
print("No.")
except ValueError:
print("Those are invalid objects, try checking for something else")
elif cmd == 7: # Which fruits are in ...
try:
g = nltk.Assignment(folval.domain)
m = nltk.Model(folval.domain, folval)
e = nltk.Expression.fromstring("be_in(x," + params[1] + ")")
results = m.satisfiers(e, "x", g)
if len(results) == 0:
print("There are none, sorry!")
else:
# find satisfying objects in the valuation dictionary,
# #and print their type names
sol = folval.values()
for so in results:
for symbol, value in folval.items():
if len(value) > 0:
valList = list(value)
if len(valList[0]) == 1:
for i in valList:
if i[0] == so:
print(symbol)
break
except nltk.sem.evaluate.Undefined:
print("That's not a valid place, try again")
elif cmd == 8: # All districts in cayman
grammar = nltk.Assignment(folval.domain)
model = nltk.Model(folval.domain, folval)
expression = nltk.Expression.fromstring("districts(x)")
results = model.satisfiers(expression, "x", grammar)
sol = folval.values()
for result in results:
for symbol, value in folval.items():
if len(value) > 0:
if value == result:
print(symbol)
break
# Manage image classification
elif cmd == 11:
uploaddir = "data/upload/"
filetype = ".jpg"
file = userInput.split(" ")
img_tensor = preprocess_image(uploaddir + file[len(file) - 1] + filetype)
classes = model.predict_classes(img_tensor)
for label, num in label_map.items():
if num == classes:
print("Uhhh... This looks like a(n)", label)
elif cmd == 99:
input_sentence = [userInput]
tt.predict(encoder, decoder, info['tokenizer'], input_sentence, info['max_length'])
else:
print(answer)