-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntent_Detection_Model.py
158 lines (107 loc) · 4.85 KB
/
Intent_Detection_Model.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
import sys
import warnings
if not sys.warnoptions:
warnings.simplefilter("ignore")
import numpy as np
from keras.models import load_model
from gensim.models import KeyedVectors
intent_dict = {0:'flight',1:'weather',2:'other'}
try:
print('Loading Gloves (Global Vectors) for word embeddings.. \nThis will take up to 2 minutes due to the size of vocabulary (400,000 words!)')
model = KeyedVectors.load_word2vec_format('glove-wiki-gigaword-300.txt')
print('Sucessfully loaded Gloves!')
except FileNotFoundError:
print('Unable to load file \'glove-wiki-gigaword-300.txt\'...\nPlease make sure the file is placed in the same directory and restart the program')
print('Loading NN models (Intent model & Label model)...')
try:
intent_model = load_model('lstm_intent.h5')
print('Successfully loaded Intent model!')
except (FileNotFoundError, OSError) as e:
print('Unable to load file \'lstm_intent.h5\'\nPlease make sure the file is placed in the same directory and restart the program')
try:
label_model = load_model('lstm_label.h5')
print('Successfully loaded Label model!')
except (FileNotFoundError, OSError) as e:
print('Unable to load file \'lstm_label.h5\'\nPlease make sure the file is placed in the same directory and restart the program')
def input_to_intent(words):
embedded_input = np.zeros((1,50,300))
for index, word in enumerate(words):
try:
embedded_input[0][index] = model.get_vector(word)
except KeyError:
embedded_input[0][index] = model.get_vector('unk')
outputs = intent_model.predict(embedded_input)
intent = intent_dict[np.argmax(outputs[0])]
return intent
def input_to_label(words):
embedded_input = np.zeros((1,50,300))
for index, word in enumerate(words):
try:
embedded_input[0][index] = model.get_vector(word)
except KeyError:
embedded_input[0][index] = model.get_vector('unk')
output = label_model.predict(embedded_input)
#print(output.shape)
label_list = []
destination_index, origin_index, location_index = [], [], []
for index, label in enumerate(output[0]):
label_id = np.argmax(label)
#print(label,label_id)
if label_id == 3:
destination_index.append(index)
elif label_id == 4:
origin_index.append(index)
elif label_id == 1:
location_index.append(index)
return destination_index, origin_index, location_index
def predict_user_input(x):
x = x.strip().lower()
words = x.split()
intent = input_to_intent(words)
destination_index, origin_index, location_index = input_to_label(words)
#print('intent:',intent)
#print('destination_index:', destination_index)
#print('origin_index:', origin_index)
#print('location_index:', location_index)
if intent == 'flight' and (len(origin_index) > 0 or len(destination_index) > 0) and len(location_index) == 0:
destination = ''
origin = ''
for i in destination_index:
destination += words[i] + ' '
for i in origin_index:
origin += words[i] + ' '
print('\n{')
print(' \"intent\": \"flight_booking_intent\",')
print(' \"slots\":')
print('\t[{')
print('\t \"name\": \"origin\",')
print('\t \"value\": \"{}\",'.format(origin.strip()))
print('\t},')
print('\t{')
print('\t \"name\": \"destination\",')
print('\t \"value\": \"{}\",'.format(destination.strip()))
print('\t}]')
print('}\n')
elif intent == 'weather' and len(destination_index) == 0 and len(origin_index) == 0 and len(location_index) > 0:
location = ''
for i in location_index:
location += words[i] + ' '
print('\n{')
print(' \"intent\": \"weather_intent\",')
print(' \"slots\":')
print('\t[{')
print('\t \"name\": \"city\",')
print('\t \"value\": \"{}\",'.format(location.strip()))
print('\t}]')
print('}\n')
else:
print('\nSeems like you are not asking about flight or weather...\n')
print('\n\nWelcome! Ask me anything about flight and weather!\n(Type \'quit\' to exit the program)\n')
user_input = ''
while user_input.lower() != 'quit':
user_input = input('Query (to quit, type quit) :')
if user_input.lower() != 'quit':
predict_user_input(user_input)
else:
print('\nThank you, see you again!\n')
quit()