-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtrans_train.py
More file actions
179 lines (156 loc) · 4.45 KB
/
trans_train.py
File metadata and controls
179 lines (156 loc) · 4.45 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
f = open("train.txt", "r")
lines = f.readlines()
f.close()
f = open("train_trans.txt", "w")
rule = []
def find_father(words, i):
index = i - 1
count = 0
while index >= 0:
#print ("fa", words[i])
if "^" in words[index]:
count -= 1
else:
count += 1
if count == 1:
# exit()
print (words[index])
return rule[index]
index -= 1
return -1
def find_father_index(words, i):
index = i - 1
count = 0
while index >= 0:
#print ("fa", words[i])
if "^" in words[index]:
count -= 1
else:
count += 1
if count == 1:
# exit()
# print (words[index])
#return words[index]
return index
index -= 1
return -1
def get_deepth(line, rule, index_rule):
words = line.strip().split()
#depth = len(words)
#if words[0] == "arguments":
# return "a" + str(depth)
#return depth
deepth = -1
bf = ""
for i in range(len(words)):
word = words[i]
if "^" in word:
deepth -= 1
else:
if "node_gen" == words[i]:
# if bf == "arguments":
# return "a" + str(deepth)
rule.append(-1)
print ("-----")
print (words[i])
index = find_father_index(words, i)
rule[index] = index_rule
return find_father(words, index)
#return father
deepth += 1
bf = word
if i >= len(rule):
rule.append(-1)
#if "node_gen" == word:
# return deepth
return -1
def write_one(lines, index):
line_now = ""
ast = ""
deepth = []
numbers = 1
for i in range(9):
if i == 5:
line_now = lines[index + i].strip() + " " + str(1 + int(lines[index + i + 1].strip())) + "\n"
numbers = line_now.strip().split()
for tt in range(len(numbers)):
numbers[tt] = str(int(numbers[tt]))
ll = " ".join(numbers)
f.write(ll + "\n")
elif i == 6:
rule_r = line_now.strip().split()
l = []
for r in rule_r:
l.append(int(r))
f.write(str(int(r) - 1))
f.write(" ")
f.write("\n")
elif i == 7:
continue
else:
f.write(lines[index + i].strip() + "\n")
if i == 7:
length = len(numbers)
global rule
rule = []
index_rule = 0
#for site in range(length)[::-1]:
# if i == 7:
# deepth.append(get_deepth(lines[index + i - 6 - 8 * site], rule, index_rule))
# index_rule += 1
#print (deepth)
# exit()
if i == 8:
length = len(numbers)
for site in range(length)[::-1]:
if i == 8:
deepth.append(int(lines[index + i - 9 * site]))
#index_rule += 1
#deepth.append(int(lines[index + i]))
#deepth = deepth[::-1]
for i in deepth:
f.write(str(i) + " ")
f.write("\n")
for i in range(10, len(lines) + 9):
t = i % 9
if t == 5: # predicted rules lines
if i >= len(lines):
rules = []
else:
rules = lines[i].strip().split()
if len(rules) == 0: #
write_one(lines, i - 9 - 5)
#rules = lines[i - 8].strip().split()
f.close()
f = open("dev.txt", "r")
lines = f.readlines()
f.close()
f = open("dev_trans.txt", "w")
for i in range(10, len(lines) + 9):
t = i % 9
if t == 5: # predicted rules lines
if i >= len(lines):
rules = []
else:
rules = lines[i].strip().split()
if len(rules) == 0: #
print (i)
write_one(lines, i - 9 - 5)
#rules = lines[i - 8].strip().split()
f.close()
f = open("test.txt", "r")
lines = f.readlines()
f.close()
f = open("test_trans.txt", "w")
for i in range(10, len(lines) + 9):
t = i % 9
if t == 5: # predicted rules lines
if i >= len(lines):
rules = []
else:
rules = lines[i].strip().split()
if len(rules) == 0: #
print (i)
write_one(lines, i - 9 - 5)
#rules = lines[i - 8].strip().split()
f.close()