This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
270 lines (199 loc) · 7.11 KB
/
test.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
261
262
263
264
265
266
267
268
269
270
def test_dt_rnn():
import numpy as np
from Models import DT_RNN
from Models import np_dt_rnn
from Helpers import utils
model = DT_RNN(dim=3, word_vector_size=3)
np_W_dep = model.W_dep.get_value()
np_W_x = model.W_x.get_value()
np_b = model.b.get_value()
sentence = "welcome to my house"
dtree = utils.get_dtree(sentence, dim=3)
vectors,parent_indices,is_leaf,dep_tags = dtree.get_rnn_input()
np_ans = np_dt_rnn(vectors, parent_indices, is_leaf, dep_tags, np_W_x, np_W_dep, np_b)
theano_ans = model.get_hidden_states(vectors, parent_indices, is_leaf, dep_tags)
print(np_ans)
print(theano_ans)
assert(np.allclose(np_ans, theano_ans, rtol=1e-04, atol=1e-07))
return
def test_sick_preprocess():
from Helpers.preprocess import SICK
from Helpers import utils
import spacy
nlp = spacy.load('en')
sick = SICK.get_data()
glove = utils.load_glove(200)
data = sick[0]
assert('senetnce_A' not in data['A'])
dtree_entry, dtne_entry = SICK.get_input_tree_single(data, nlp, glove)
for entry in [dtree_entry, dtne_entry]:
for x in ['A', 'B', 'score']:
assert(x in entry)
if x != 'score':
for y in ['word_vectors', 'parent_indices', 'is_leaf', 'dep_tags', 'text']:
assert(y in entry[x])
assert('ent_type' in dtne_entry['A'])
assert('ent_type' in dtne_entry['B'])
return
def test_ans_select():
from Models import AnsSelect
import numpy as np
for inp_dim in [30, 50, 100, 200, 300]:
q = np.random.rand(inp_dim)
ans_sent = np.random.rand(inp_dim)
ans_node = np.random.rand(inp_dim)
ans_parent = np.random.rand(inp_dim)
answer = 1
initializations = [
'glorot_normal','glorot_uniform',
'he_uniform','he_normal'
]
for optimization in ['adadelta', 'adam']:
for initialization in initializations:
model = AnsSelect(inp_dim,
optimization=optimization,
initialization=initialization
)
model.train(q, ans_sent, ans_node, ans_parent, answer)
# x = model.predict(q, ans_sent, ans_node, ans_parent)
# x = model.get_loss(q, ans_sent, ans_node, ans_parent, answer)
# raise AssertionError(x)
return
def test_dtrnn_train():
from Model_Trainer import DT_RNN_Train as dttrain
from Helpers import utils
initializations = [
'glorot_normal','glorot_uniform',
'he_uniform','he_normal'
]
for optimization in ['adadelta', 'adam']:
for initialization in initializations:
sent1 = "this is my house"
sent2 = "this is my home"
score = 5
inputs1 = utils.get_dtree(sent1).get_rnn_input()
inputs2 = utils.get_dtree(sent2).get_rnn_input()
model = dttrain(
n=1, epochs=2, hid_dim=200,
optimization=optimization,
initialization=initialization)
model.train(
inputs1[0],
inputs1[1],
inputs1[2],
inputs1[3],
inputs2[0],
inputs2[1],
inputs2[2],
inputs2[3],
score
)
return
def test_configurations():
from Helpers.deployment_utils import create_config
from Helpers.deployment_utils import get_config
from Helpers.utils import get_file_name
filename = 'age.22__name.mehmood__time.10:12:30__username.meshde.pkl'
create_config(filename, 'test.cfg')
config = get_config('test.cfg')
assert('state' in config)
assert(config['state'] == filename)
del config['state']
output_filename = get_file_name(extension='pkl', **config)
assert(filename == output_filename)
return
def test_dtrnn_cfg():
from Helpers.deployment_utils import get_config
config = get_config('dtrnn.cfg')
assert('dep_len' in config)
assert('word_vector_size' in config)
assert('dim' in config)
return
def test_get_state_file_name():
from Helpers import utils
filename = utils.get_file_name(
extension = 'pkl',
first_name = 'mehmood shakeel deshmukh',
username = 'meshde',
age = 22
)
required = 'age:22__first_name:mehmood_shakeel_deshmukh__username:meshde.pkl'
assert(filename == required)
return
def test_imports():
import Helpers
import Models
import Model_Trainer
return
def test_abcnn_ass_for_babi():
from Models import abcnn_ass
from Helpers import utils
selector = abcnn_ass()
babi = utils.get_babi_raw_for_abcnn(babi_id='1', mode='train')
babi = utils.process_babi_for_abcnn(babi)
babi = babi[:5]
instances = len(babi)
correct_op = 0
for sample in tqdm(babi, total=len(babi), ncols=75, unit='Sample '):
line_numbers, context, question, _, support = sample
ans_sents = selector.ans_select(question, context)
ans_sent, _ = ans_sents[0]
if line_numbers[context.index(ans_sent)] == support:
correct_op += 1
accuracy = correct_op / instances
print('Accuracy: {0:.2f}'.format(accuracy))
return
def test_get_babi_dataset_normal():
from Helpers.preprocess import AnswerExtract
dataset = AnswerExtract.get_babi_dataset(compressed_dataset=False)
keys = [
'question_root',
'answer_root',
'answer_node',
'parent_node',
'label',
]
for key in keys:
assert(key in dataset[0])
return
def test_extract_answer():
sentence = 'John went to the bathroom'
question = 'where is john'
sentences = [
(sentence, 1),
]
from Helpers.deployment_utils import extract_answer_from_sentences
extract_answer_from_sentences(
sentences,
question,
)
return
def test_IR():
from IR import infoRX
import os
file_name = os.path.join("./data/corpus/cricket.txt")
query = "what is the role of bat in cricket"
with open(file_name, 'r') as f:
doc = list(filter(('\n').__ne__, f.readlines()))
print(retrieve_info(doc, query))
return
def test_flask_server():
import requests
import os
input_file_path = os.path.join(".\data\corpus\cricket.txt")
input_filename = 'cricket.txt'
with open(input_file_path) as input:
files = {'file': input}
values = {'filename': input_filename}
resp = requests.post("http://127.0.0.1:5000/filed", files=files, data=values)
print(resp.status_code, resp.reason, resp.text)
assert(resp.status_code == 200 and resp.text == 'File uploaded. Context Ready.')
assert( os.path.isfile('./data/uploads/'+input_filename))
query = "what is the role of bat in cricket"
values = {'query': query}
resp = requests.post("http://127.0.0.1:5000/query", json=values)
print(resp.status_code, resp.reason, resp.text)
assert(resp.status_code == 200)
print('Flask server tests successful.')
if __name__ == '__main__':
test_flask_server()