-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathresolve_data.py
More file actions
572 lines (498 loc) · 16.4 KB
/
resolve_data.py
File metadata and controls
572 lines (498 loc) · 16.4 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#-*-coding:utf-8-*-
import sys
import os
import numpy as np
import random
from copy import deepcopy
project = str(sys.argv[1]) + "/"
def tqdm(a):
try:
from tqdm import tqdm
return tqdm(a)
except:
return a
vocabulary = {}
vocabulary["<Start>"] = 2
vocabulary["NothingHere"] = 0
vocabulary["Unknown"] = 1
char_vocabulary = {}
char_vocabulary["default"] = 0
char_vocabulary["Unknown"] = 1
tree_vocabulary = {}
tree_vocabulary["Unknown"] = 1
tree_vocabulary["NothingHere"] = 0
tree_vocabulary["NoneCopy"] = 2
tree_vocabulary["CopyNode"] = 3
tree_vocabulary["End"] = 5
tree_vocabulary["<StartNode>"] = 4
Rule = []
Nonterminal = []
copy_ast = ""
trainset = [] # for a dataset,
trainset.append([]) # train for trainset[0]
trainset.append([]) # dev for trainset[1]
trainset.append([]) # test for trainset[2]
is_train = False
nl_len = 200
tree_len = 500
parent_len = 20
rules_len = rulelist_len = 200
char_len = 10
function_len = 1
nl_voc_ground = {}
length = [nl_len, tree_len, tree_len, tree_len, parent_len, rulelist_len, rulelist_len, function_len, rulelist_len]
dev_count = 0
dev_ab = 0
def readrules():
f = open(project + "Rule.txt", "r")
lines = f.readlines()
f.close()
for line in lines:
l = []
words = line.split()
l.append(words[0])
Nonterminal.append(words[0])
l1 = []
for i in range(2, len(words)):
l1.append(words[i])
l.append(l1)
Rule.append(l)
readrules()
rulesnum = len(Rule)
classnum = rulesnum + nl_len
def readvoc ():
global comment_vocabulary
global tree_vocabulary
global char_vocabulary
global vocabulary
f = open(project + "tree_voc.txt", "r")
'''lines = f.readlines()
for line in lines:
words = line.strip().split()
if int(words[1]) >= 10:
tree_vocabulary[words[0]] = len(tree_vocabulary)'''
tree_vocabulary = eval(f.readline())
if "HS" not in project:
tree_vocabulary["End"] = len(tree_vocabulary)
f.close()
f = open(project + "char_voc.txt", "r")
lines = f.readlines()
try:
char_vocabulary = eval(lines[0])
except:
#lines = f.readlines()
for line in lines:
words = line.strip().split()
if int(words[1]) >= 10:
char_vocabulary[words[0]] = len(char_vocabulary)
f.close()
f = open(project + "nl_voc.txt", "r")
'''lines = f.readlines()
for line in lines:
words = line.strip().split()
if int(words[1]) >= 2:
vocabulary[words[0]] = len(vocabulary)
nl_voc_ground[words[0]] = len(nl_voc_ground)'''
vocabulary = eval(f.readline())
f.close()
def word2vec (word, tp):
is_train = False
if tp == "nl":
if word not in vocabulary:
#if not is_train:
# print (copy_ast)
return 1
#comment_vocabulary[word] = len(comment_vocabulary)
return vocabulary[word]
else:
if word not in tree_vocabulary:
#if not is_train:
return 1
#tree_vocabulary[word] = len(tree_vocabulary)
return tree_vocabulary[word]
def line2vec (line, tp, length):
vec = np.zeros([length])
tokens = line.split()
if "tree" == tp:
for i in range(min(len(tokens), length)):
vec[i] = word2vec(tokens[i], "tree")
else:
for i in range(min(len(tokens), length)):
vec[i] = word2vec(tokens[i], "nl")
return vec
def rule2classnum (line):
line = str(line)
numbers = line.strip().split()
l = []
#print (line)
for n in numbers:
num = int(n)
if num == 9999:
print (num)
exit()
if num >= 10000: # copy
num = num - 10000 + rulesnum
if num >= classnum: # check length
# print (num)
# print ("NL Length is not enough")
l.append(classnum + 2)
else:
l.append(num)
return l
def rule2classvec(line, length):
l = rule2classnum(line)
ret = np.zeros([length])
for i in range(min(length, len(l))):
ret[i] = l[i]
return ret
def rulebondast (num, father, nl):
if num == 0:
vec = np.zeros([1])
vecson = np.zeros([10])
#for i in range(len(vec)):
# vec[i] = 1
#for i in range(len(vecson)):
# vecson [i] = 1
return vec, vecson
#print (num)
rule = rule2classnum(num - 1)[0]
#print (rule)
vec = np.zeros([1])
vecson = np.zeros([10])
#for i in range(len(vecson)):
# vecson[i] = 1 # NothingHere
words = nl.split()
#print (words)
if rule >= rulesnum and rule < classnum:
#print (rule)
site = rule - rulesnum
#vec[0] = word2vec(father, "tree")
vec[0] = father#word2vec("CopyNode", "tree")
#print (words)
#print (words[site])
if site >= len(words):
#print (site)
exit()
vecson[0] = word2vec("NoneCopy", "tree")
else:
# print (words[site])
vecson[0] = word2vec(words[site] + "", "tree")
elif rule >= classnum:
vec[0] = word2vec("<StartNode>", "tree")
vecson[0] = word2vec("root", "tree")
else:
vec[0] = word2vec(Rule[rule][0], "tree")
l = Rule[rule][1]
for i in range(min(10, len(l))):
vecson[i] = word2vec(l[i], "tree")
return vec, vecson
def line2rules (line, length, father, nl):
vec = np.zeros([length])
vecson = np.zeros([length, 10])
words = line.split()
l = []
l.append(classnum + 2) # <Start Node>
for word in words:
num = int(word)
l.append(num)
#while len(l) < length:
# l.append(0)
#print (len(l))
#print (len(father))
for i in range(min(length, len(l))):
#if l[i] == classnum + 2:
# continue
#vec[i] = 0#word2vec("NothingHere", "tree")
#elif i >= len(father):
# v1, v2 = rulebondast(l[i], "", nl)
# vec[i] = v1[0]
# for t in range(10):
# vecson[i][t] = v2[t]
#else:
v1, v2 = rulebondast(l[i], father[i - 1], nl)
vec[i] = v1[0]
for t in range(10):
vecson[i][t] = v2[t]
return vec, vecson
def one_hot(num, length):
onehot = np.zeros([length])
if num >= length: # check length
print ("NL Length is not enough one-hot")
return onehot
onehot[num] = 1
return onehot
def char2num (c):
global char_vocabulary
if c not in char_vocabulary:
return 1
#char_vocabulary[c] = len(char_vocabulary)
return char_vocabulary[c]
def line2charvec (line, length, charlength):
vec = np.zeros([length, charlength])
tokens = line.strip().split()
for i in range(min(length, len(tokens))):
for t in range(min(charlength, len(tokens[i]))):
vec[i, t] = char2num(tokens[i][t])
return vec
def line2rulevec (line, length):
vec = np.zeros([length])
tokens = line.strip().split()
vec[0] = classnum + 2
for i in range(min(length -1, len(tokens))):
vec[i + 1] = rule2classnum(int(tokens[i]) - 1)[0] + 1
return vec
def line2ground (line, length):
vec = np.zeros([length])
words = line.strip().split()
for i in range(len(words)):
if words[i] not in nl_voc_ground:
continue
vec[i] = nl_voc_ground[words[i]]
return vec
def read_tree_path(tree_path, rules_line, nowsite):
all_lines = len(rules_line.strip().split())
fathers = []
tree_path_len = 10
tree_path_vec = np.zeros([length[5], tree_path_len])
for i in range(nowsite, nowsite + all_lines):
t = i - nowsite
if t >= length[5]:
break
line = tree_path[i]
words = line.strip().split()
for w_site in range(min(len(words), tree_path_len)):
tree_path_vec[t][w_site] = word2vec(words[w_site], "tree")
fathers.append(word2vec(words[0], "tree"))
return tree_path_vec, fathers
def get_father_list(nodes):
father_list = [-1]
stack = []
nowsite = nodes[0]
stack.append([nowsite, 0])
for i in range(1, len(nodes)):
while True:
top = stack[-1]
if nodes[i] > top[0]:
father_list.append(top[1])
stack.append([nodes[i], i])
break
else:
stack.pop()
return father_list
def build_lca(nodes_deepth):
node = nodes_deepth
fathers = []
father_list = nodes_deepth#get_father_list(nodes_deepth)
used = []
for i in range(len(node)):
par = []
index = i
while index != -1:
par.append(index)
index = father_list[index]
if index not in used:
used.append(index)
fathers.append(par[::-1])
term = []
for i in range(len(node)):
if i not in used:
term.append(i)
return fathers, term
def query_lca(start, end, fathers, nodes_deepth):
try:
l_s = len(fathers[start])
l_e = len(fathers[end])
except:
return 1000000
same_site = 0
for i in range(min(l_s, l_e)):
if fathers[start][i] == fathers[end][i]:
same_site = i
else:
break
return len(fathers[start]) + len(fathers[end]) - 2 * (1 + same_site)
def line2mask(lines, length):
nodes_deepth = lines.strip().split()
nodes_deepth = [int(-1)] + nodes_deepth
for i in range(1, len(nodes_deepth)):
nodes_deepth[i] = int(nodes_deepth[i]) + 1
ret = np.zeros([length, length])
fathers, term = build_lca(nodes_deepth)
labels = np.zeros([length])
father_vec = np.zeros([length, length])
termnow = -1
for i in range(length):
try:
site = fathers[i][-1] # next node
except:
break
labels[i] = len(fathers[i])
try:
ret[i][fathers[i][-2]] = 1.0
except:
ret[i][fathers[i][-1]] = 1.0
return ret, father_vec, labels
def read_data (file_name):
file2number = {}
file2number["train_trans.txt"] = 0
file2number["dev_trans.txt"] = 1
file2number["test_trans.txt"] = 2
index_of_dataset = file2number[file_name]
f = open(project + file_name, "r")
file_data = []
for i in range(8):
file_data.append([])
number2file = {}
number2file[0] = project + "train_tree.txt"
number2file[1] = project + "dev_tree.txt"
number2file[2] = project + "test_tree.txt"
f_tree = open(number2file[index_of_dataset], "r")
tree_path = f_tree.readlines()
f_tree.close()
lines = f.readlines()
all_vec = []
count = 0
each_vec = []
bf = ""
rules_line = ""
father = []
now_site = 0
for i in tqdm(range(len(lines))):
lines[i] = str(lines[i]).strip()
t = i % 9
if t == 0 : # the first line; Natural Languages;
if bf != "":
all_vec.append(deepcopy(each_vec))
if bf != lines[i] and bf != "" and len(bf.split()) < length[0]: # length protection
trainset[index_of_dataset].append(deepcopy(all_vec))
all_vec = []
father = []
# 50% datas are selected
#if index_of_dataset in [0, 2, 1] and i >= len(lines) // 20:
# return
bf = lines[i]
if len(lines[i].split()) >= length[0]:
all_vec = []
each_vec = []
# Using vocabulary
each_vec.append(line2vec(lines[i], "nl", length[t]))
# Using char_vocabulary for char embeddding;
each_vec.append(line2charvec(lines[i], length[t], char_len))
elif t == 6: # the line denotes the target output
each_vec.append(rule2classvec(lines[i], length[t]))
elif t == 5: # the line denotes the predict rules
each_vec.append(line2rulevec(lines[i], length[t]))
rules_line = lines[i]
elif t == 8:
vv, vvv, labels = line2mask(lines[i].strip(), length[t])
each_vec.append(vv)
each_vec.append(vvv)
each_vec.append(labels)
#each_vec.append(line2mask(lines[i].strip(), length[t]))
else:
each_vec.append(line2vec(lines[i], "tree",length[t]))
if t == 7:
each_vec.append(bf)
tp_vec, fathers_vec = read_tree_path(tree_path, rules_line, now_site)
each_vec.append(tp_vec)
#print (each_vec[-1])
#exit()
now_site += len(rules_line.strip().split())
bf = "<Start> " + bf
each_vec.append(line2vec(bf, "nl", length[0]))
each_vec.append(line2charvec(bf, length[0], char_len))
each_vec.append(line2ground(bf.replace("<Start> ", ""),length[0]))
#print (now_site)
if t == 5:
tp_vec, fathers_vec = read_tree_path(tree_path, rules_line, now_site)
#print (len(tp_vec) == len(fathers_vec))
v1, v2 = line2rules(lines[i], length[t], fathers_vec, bf)
each_vec.append(v1)
each_vec.append(v2)
if t == 4:
father.append(lines[i].split()[-1].replace("_root", ""))
# the last data;
all_vec.append(deepcopy(each_vec))
trainset[index_of_dataset].append(deepcopy(all_vec))
def random_data (dataset): # shuffle training set
#num_lst = np.random.permutation(range(int(dataset.shape[0])))
random.shuffle(dataset)
def batch_data (batch_size, dataset_name): # get an acceptable data for NN;
dic = {}
dic["train"] = 0
dic["dev"] = 1
dic["test"] = 2
index_of_dataset = dic[dataset_name]
global trainset
data = trainset[index_of_dataset]
#print (data)
all_data = []
all_index = []
data_now = []
for i in range(len(data)):
data_now += [[data[i][q][t] for t in range(19)] for q in range(len(data[i]))]
if index_of_dataset == 0: # random training data;
random.shuffle(data_now)
#data_now = np.array(data_now)
all_data = [data_now[site: min(len(data_now), site + batch_size)] for site in range(0, len(data_now), batch_size)]
#print (all_data[0][0])
ret_data = []
for site in range(len(all_data)):
ret_data += [[[all_data[site][i][t] for i in range(len(all_data[site]))] for t in range(19)]]
#print (all_data[0][0])
#print (len(all_data))
#print (len(all_data[0]))
return ret_data, all_index
if index_of_dataset == 0: # random training data;
random.shuffle(data)
all_data = []
all_index = [] # denotes the card number;
# the 1st dim denotes the batch number;
# the 2nd dim denotes the 9-grams;
# the 3rd dim denotes the data (e.g., NL, Char, ...)
batch_data = [] # denotes the each batches of data;
# the 1st dim denotes the 9-grams;
# the 2nd dim denotes the data (e.g., NL, Char, ...)
for t in range(19):
batch_data.append([])
batch_index = []
index_num = 0
for i in range(len(data)): # i denotes the index of the card;
if len(batch_data[0]) >= batch_size:
for t in range(len(batch_data) - 1): # the batch_data[-1]: natural language
batch_data[t] = np.array(batch_data[t])
all_data.append(deepcopy(batch_data))
all_index.append(deepcopy(batch_index))
batch_data = []
for t in range(19):
batch_data.append([])
batch_index = []
index_num = 0
# for a data:
# the 1st dim means: the cards
# the 2nd dim means: the number of tuples.
# the 3rd dim denotes each of the 9-gram tuples (e.g., NL, Char).
for t in range(len(batch_data)):
batch_data[t].extend([data[i][q][t] for q in range(len(data[i]))])
print (batch_data[9])
print (batch_data[7])
exit()
batch_index.extend([index_num] * len(data[i]))
index_num += 1
if (len(batch_data[0]) > 0):
# the last batch;
for t in range(len(batch_data) - 1):
batch_data[t] = np.array(batch_data[t])
all_data.append(deepcopy(batch_data))
all_index.append(deepcopy(batch_index))
return all_data, all_index
def resolve_data():
global trainset
readvoc()
read_data("train_trans.txt")
read_data("dev_trans.txt")
read_data("test_trans.txt")
if sys.argv[0] == "run.py":
resolve_data()
elif "predict" in sys.argv[0]:
readvoc()