-
Notifications
You must be signed in to change notification settings - Fork 13
/
joint_mask.py
455 lines (415 loc) · 13.3 KB
/
joint_mask.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
class OuterMask:
def __init__(self, tags_info):
self.tags_info = tags_info
self.mask = 0
self.need = 1
self.SOS = tags_info.tag_size*10
self.relation = tags_info.tag_size*10+1
self.variable_offset = 0
self.relation_offset = 1
self.k_relation_offset = 2
self.p_relation_offset = 3
self.drs_offset = 4
self.six_offset = 5
self.reset()
def reset(self, ):
self.relation_count = 0
self.stack = [self.SOS]
self.stack_ex = [[0 for i in range(6)]]
self.stack_variables = []
self.k = 1
self.p = 1
self.x = 1
self.e = 1
self.s = 1
def get_all_mask(self, inputs):
res = []
#self._print_state()
res.append(self.get_step_mask())
#print res[-1]
for type, ix in inputs:
assert res[-1][ix] != self.mask
self.update(type, ix)
#self._print_state()
res.append(self.get_step_mask())
#print res[-1]
return res
def get_step_mask(self):
if self.stack[-1] == self.SOS:
#SOS
return self._get_sos_mask()
elif self.stack[-1] == 5:
#SDRS
return self._get_sdrs_mask()
elif self.stack[-1] == 6:
#DRS
return self._get_drs_mask()
elif self.stack[-1] in [7, 8, 9]:
#not, nec, pos
return self._get_1_mask()
elif self.stack[-1] in [10, 11, 12]:
#or, imp, duplex
return self._get_2_mask()
elif self.stack[-1] == self.tags_info.k_rel_start or self.stack[-1] == self.tags_info.p_rel_start:
#k p
return self._get_1_mask()
else:
assert False
def _get_sos_mask(self):
if self.stack_ex[-1][self.drs_offset] == 0:
re = self._get_zeros(self.tags_info.tag_size)
re[self.tags_info.tag_to_ix[self.tags_info.rel_drs]] = self.need
return re
else:
re = self._get_zeros(self.tags_info.tag_size)
re[1] = self.need
return re
def _get_sdrs_mask(self):
#SDRS
if self.stack_ex[-1][self.k_relation_offset] < 2:
#only k
re = self._get_zeros(self.tags_info.tag_size)
idx = self.tags_info.k_rel_start + self.k - 1
re[idx] = self.need
return re
else:
#only reduce
re = self._get_zeros(self.tags_info.tag_size)
re[self.tags_info.tag_to_ix[self.tags_info.reduce]] = self.need
if self.relation_count <= 40:
cnt = 0
for i in range(len(self.stack)-1):
if self.stack[i] == 5 and self.stack_ex[i][self.k_relation_offset] == 0:
cnt += 1
if self.k <= self.tags_info.MAX_KV - cnt:
# k is ok
idx = self.tags_info.k_rel_start + self.k - 1
re[idx] = self.need
return re
def _get_drs_mask(self):
re = self._get_zeros(self.tags_info.tag_size)
re[self.tags_info.tag_to_ix[self.tags_info.reduce]] = self.need
if self.relation_count <= 40:
if self.p <= self.tags_info.MAX_PV:
idx = self.tags_info.p_rel_start + self.p - 1
re[idx] = self.need
re[7] = self.need
re[8] = self.need
re[9] = self.need
re[10] = self.need
re[11] = self.need
re[12] = self.need
return re
def _get_1_mask(self):
if self.stack_ex[-1][self.drs_offset] == 0:
re = self._get_zeros(self.tags_info.tag_size)
if self.relation_count <= 40:
cnt = 0
for i in range(len(self.stack)):
if self.stack[i] == 5 and self.stack_ex[i][self.k_relation_offset] == 0:
cnt += 1
if self.k + 1 <= self.tags_info.MAX_KV - cnt: #enough k to produce sdrs
re[5] = self.need
re[6] = self.need
return re
else:
re = self._get_zeros(self.tags_info.tag_size)
re[4] = self.need
return re
def _get_2_mask(self):
if self.stack_ex[-1][self.drs_offset] <= 1:
re = self._get_zeros(self.tags_info.tag_size)
if self.relation_count <= 40:
cnt = 0
for i in range(len(self.stack)):
if self.stack[i] == 5 and self.stack_ex[i][self.k_relation_offset] == 0:
cnt += 1
if self.k + 1 <= self.tags_info.MAX_KV - cnt: # enough k to produce sdrs
re[5] = self.need
re[6] = self.need
return re
else:
re = self._get_zeros(self.tags_info.tag_size)
re[4] = self.need
return re
def update(self, type, ix):
if ix >= 5 and ix <= 12:
self.stack.append(ix)
self.relation_count += 1
self.stack_ex.append([0 for i in range(6)])
elif ix >= self.tags_info.k_rel_start and ix < self.tags_info.p_rel_start:
self.stack.append(self.tags_info.k_rel_start)
self.relation_count += 1
self.stack_ex.append([0 for i in range(6)])
self.k += 1
elif ix >= self.tags_info.p_rel_start and ix < self.tags_info.k_tag_start:
self.stack.append(self.tags_info.p_rel_start)
self.relation_count += 1
self.stack_ex.append([0 for i in range(6)])
self.p += 1
elif ix == 4:
self.stack_ex.pop()
if self.stack[-1] == 5 or self.stack[-1] == 6:
self.stack_ex[-1][self.drs_offset] += 1
elif self.stack[-1] >= 7 and self.stack[-1] <= 12:
self.stack_ex[-1][self.six_offset] += 1
elif self.stack[-1] == self.tags_info.k_rel_start:
self.stack_ex[-1][self.k_relation_offset] += 1
elif self.stack[-1] == self.tags_info.p_rel_start:
self.stack_ex[-1][self.p_relation_offset] += 1
else:
assert False
self.stack.pop()
elif ix == 1:
pass
else:
assert False
def _print_state(self):
print "relation_count", self.relation_count
print "stack", self.stack
print "stack_ex", self.stack_ex
print "stack_variables", self.stack_variables
print "kpxes", self.k, self.p, self.x, self.e, self.s
def _get_zeros(self, size):
return [self.mask for i in range(size)]
def _get_ones(self, size):
return [self.need for i in range(size)]
class RelationMask:
#sdrs should have at least two k(), at least one relation, and the relation should follow k()
#drs should have at least anything, except variables.
#not, nec, pos should have and only have one drs or sdrs
#imp, or, duplex should have and only have two drs or sdrs
#timex should be timex(variables, TIME_NUMBER)
#card should be card(variables, CARD_NUMBER)
#k(, p( should have and only have one drs or sdrs
#variable constrains
def __init__(self, tags_info, encoder_input_size=0):
self.tags_info = tags_info
self.mask = 0
self.need = 1
self.reset(encoder_input_size)
def reset(self, encoder_input_size):
self.encoder_input_size = encoder_input_size
self.is_sdrs = False
def set_sdrs(self, is_sdrs):
self.is_sdrs = is_sdrs
def _get_relations(self):
res = self._get_zeros(self.tags_info.tag_size) + self._get_ones(self.encoder_input_size)
res[1] = self.need
idx = 13
if self.is_sdrs:
idx = self.tags_info.global_start
while idx < self.tags_info.k_rel_start:
res[idx] = self.need
idx += 1
return res
def get_all_mask(self, input_size, least):
res = []
for i in range(input_size+1):
res.append(self._get_relations())
if least:
res[0][1] = self.mask # next of condition
return res
def get_step_mask(self, least):
relations = self._get_relations()
if least:
relations[1] = self.mask
return relations
def _get_zeros(self, size):
return [self.mask for i in range(size)]
def _get_ones(self, size):
return [self.need for i in range(size)]
class VariableMask:
#sdrs should have at least two k(), at least one relation, and the relation should follow k()
#drs should have at least anything, except variables.
#not, nec, pos should have and only have one drs or sdrs
#imp, or, duplex should have and only have two drs or sdrs
#timex should be timex(variables, TIME_NUMBER)
#card should be card(variables, CARD_NUMBER)
#k(, p( should have and only have one drs or sdrs
#variable constrains
def __init__(self, tags_info):
self.tags_info = tags_info
self.mask = 0
self.need = 1
self.k_use = False
self.reset(0)
def reset(self, p_max, k_use=False):
#self.k = 1
self.p_max = p_max
self.x = 1
self.e = 1
self.s = 1
self.stack_ex = []
self.stack = []
self.stack_drs = []
self.prev_variable = -1
self.pre_prev_variable = -1
self.user_k = []
self.k_use = k_use
def set_k(self, user_k):
self.user_k = user_k
def get_step_mask(self):
if self.stack_drs[-1] == 5:
return self._get_sdrs_mask()
else:
return self._get_drs_mask()
def _get_sdrs_mask(self):
#SDRS
if self.prev_variable == -1:
re = self._get_zeros(self.tags_info.tag_size)
if self.k_use:
for idx in self.user_k:
re[idx + self.tags_info.k_tag_start] = self.need
else:
for idx in self.stack_ex[-1]:
re[idx] = self.need
return re
elif self.prev_prev_variable == -1:
re = self._get_zeros(self.tags_info.tag_size)
if self.k_use:
for idx in self.user_k:
re[idx + self.tags_info.k_tag_start] = self.need
else:
for idx in self.stack_ex[-1]:
re[idx] = self.need
assert self.prev_variable != -1
re[self.prev_variable] = self.mask
return re
else:
re = self._get_zeros(self.tags_info.tag_size)
re[1] = self.need
return re
def _get_drs_mask(self):
if self.prev_variable == -1:
re = self._get_zeros(self.tags_info.tag_size)
idx = self.tags_info.x_tag_start
while idx < self.tags_info.e_tag_start and idx < self.tags_info.x_tag_start + self.x:
re[idx] = self.need
idx += 1
idx = self.tags_info.e_tag_start
while idx < self.tags_info.s_tag_start and idx < self.tags_info.e_tag_start + self.e:
re[idx] = self.need
idx += 1
idx = self.tags_info.s_tag_start
while idx < self.tags_info.tag_size and idx < self.tags_info.s_tag_start + self.s:
re[idx] = self.need
idx += 1
idx = self.tags_info.p_tag_start
while idx < self.tags_info.x_tag_start and idx < self.tags_info.p_tag_start + self.p_max:
re[idx] = self.need
idx += 1
#while idx < self.tags_info.p_tag_start + self.max_p and idx < self.tags_info.p_tag_start + self.p:
# re[idx] = self.need
# idx += 1
return re
elif self.prev_prev_variable == -1:
re = self._get_zeros(self.tags_info.tag_size)
re[1] = self.need
if self.stack[-1] == 13:
re[3] = self.need
elif self.stack[-1] == 14:
re[2] = self.need
else:
idx = self.tags_info.x_tag_start
while idx < self.tags_info.e_tag_start and idx < self.tags_info.x_tag_start + self.x:
re[idx] = self.need
idx += 1
idx = self.tags_info.e_tag_start
while idx < self.tags_info.s_tag_start and idx < self.tags_info.e_tag_start + self.e:
re[idx] = self.need
idx += 1
idx = self.tags_info.s_tag_start
while idx < self.tags_info.tag_size and idx < self.tags_info.s_tag_start + self.s:
re[idx] = self.need
idx += 1
idx = self.tags_info.p_tag_start
while idx < self.tags_info.x_tag_start and idx < self.tags_info.p_tag_start + self.p_max:
re[idx] = self.need
idx += 1
#while idx < self.tags_info.p_tag_start + self.max_p and idx < self.tags_info.p_tag_start + self.p:
# re[idx] = self.need
# idx += 1
if self.stack[-1] == 15:
pass
else:
re[self.prev_variable] = self.mask
return re
else:
re = self._get_zeros(self.tags_info.tag_size)
re[1] = self.need
return re
def update(self, ix):
if ix < self.tags_info.tag_size:
if ix == 1:
pass
if ix >= 5 and ix < self.tags_info.k_rel_start:
self.stack.append(ix)
if ix == 5:
self.stack_ex.append([])
self.stack_drs.append(ix)
elif ix == 6:
self.stack_drs.append(ix)
self.prev_prev_variable = self.prev_variable
self.prev_variable = -1
elif ix >= self.tags_info.k_rel_start and ix < self.tags_info.p_rel_start:
self.stack.append(ix)
self.stack_ex[-1].append(ix - self.tags_info.k_rel_start + self.tags_info.k_tag_start)
#self.k += 1
self.prev_prev_variable = self.prev_variable
self.prev_variable = -1
elif ix >= self.tags_info.p_rel_start and ix < self.tags_info.k_tag_start:
self.stack.append(ix)
#self.p += 1
self.prev_prev_variable = self.prev_variable
self.prev_variable = -1
elif ix == 4:
if self.stack[-1] == 5:
self.stack_ex.pop()
self.stack_drs.pop()
elif self.stack[-1] == 6:
self.stack_drs.pop()
self.stack.pop()
self.prev_prev_variable = self.prev_variable
self.prev_variable = -1
else:
if ix >= self.tags_info.k_tag_start and ix < self.tags_info.p_tag_start:
#assert self.k >= ix - self.tags_info.k_tag_start + 1
#if self.k == ix - self.tags_info.k_tag_start + 1:
# self.k += 1
pass
elif ix >= self.tags_info.p_tag_start and ix < self.tags_info.x_tag_start:
#assert self.p >= ix - self.tags_info.p_tag_start + 1
#if self.p == ix - self.tags_info.p_tag_start + 1:
# self.p += 1
pass
elif ix >= self.tags_info.x_tag_start and ix < self.tags_info.e_tag_start:
assert self.x >= ix - self.tags_info.x_tag_start + 1
if self.x == ix - self.tags_info.x_tag_start + 1:
self.x += 1
elif ix >= self.tags_info.e_tag_start and ix < self.tags_info.s_tag_start:
assert self.e >= ix - self.tags_info.e_tag_start + 1
if self.e == ix - self.tags_info.e_tag_start + 1:
self.e += 1
elif ix >= self.tags_info.s_tag_start and ix < self.tags_info.tag_size:
assert self.s >= ix - self.tags_info.s_tag_start + 1
if self.s == ix - self.tags_info.s_tag_start + 1:
self.s += 1
self.prev_prev_variable = self.prev_variable
self.prev_variable = ix
else:
self.stack.append(ix)
self.prev_prev_variable = self.prev_variable
self.prev_variable = -1
def _print_state(self):
print "stack", self.stack
print "stack_ex", self.stack_ex
print "stack_drs", self.stack_drs
print "k_use", self.k_use
print "user_k", self.user_k
# print "kpxes", self.k, self.p, self.x, self.e, self.s
print "xes", self.x, self.e, self.s
def _get_zeros(self, size):
return [self.mask for i in range(size)]
def _get_ones(self, size):
return [self.need for i in range(size)]