-
Notifications
You must be signed in to change notification settings - Fork 1
/
solvetree.py
334 lines (332 loc) · 11.9 KB
/
solvetree.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
import os
from tqdm import tqdm
import pickle
import json
import numpy as np
import sys
lst = ['train', "test1"]#["train", "dev", "test1"]
from transformers import AutoTokenizer, AutoModel
import traceback
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codet5-small")
rules = pickle.load(open("pythonrule.pkl", "rb"))
onelist = ['argument_list', 'formal_parameters', 'block', 'array_initializer', 'switch_block', 'type_arguments', "method_declaration", "modifiers", 'annotation_argument_list', 'variable_declarator', 'throws', 'element_value_array_initializer', 'annotation_argument_list', 'switch_block_statement_group', 'class_body', 'catch_type', 'assert_statement', 'try_statement', 'local_variable_declaration', 'try_statement', 'constructor_body', 'type_parameters', 'resource_specification', 'inferred_parameters', 'try_with_resources_statement', 'inits', 'updates', 'conditions']
identifiers = ['identifier', 'type_identifier', 'null_literal', 'decimal_integer_literal', 'character_literal', 'decimal_floating_point_literal', 'hex_integer_literal', 'string_literal']
sonelist = ['formal_parameters', 'block', 'array_initializer', 'argument_list', 'type_arguments', 'annotation_argument_list']
rulelist = []
fatherlist = []
fathername = []
depthlist = []
idenlist = []
expandnode = None
copynode = {}
class Node:
def __init__(self, name, s):
self.name = name
self.id = s
self.father = None
self.child = []
def printTree(self, r):
s = r.name + " "#print(r.name)
if len(r.child) == 0:
s += "^ "
return s
#r.child = sorted(r.child, key=lambda x:x.name)
for c in r.child:
s += self.printTree(c)
s += "^ "#print(r.name + "^")
return s
def parseTree(treestr):
tokens = treestr.split()
root = Node(tokens[0], 0)
currnode = root
for i, x in enumerate(tokens[1:]):
if x != "^":
if tokens[i + 2] == "^":
x = x.lower()
nnode = Node(x, i + 1)
nnode.father = currnode
currnode.child.append(nnode)
currnode = nnode
else:
currnode = currnode.father
return root
def addException(root):
if root.name in ['throws', 'types', 'label', 'case', 'goto']:
for i, x in enumerate(root.child):
nnode = Node('flag', 1)
nnode.father = root
nnode.child.append(x)
x.father = nnode
root.child[i] = nnode
if root.name == 'value':
if root.father.name == 'Assignment':
root.name = 'value_assign'
for x in root.child:
addException(x)
maxnlnum = 40
hascopy = {}
def getcopyid(nls, name):
global maxnlnum
global hascopy
lastcopyid = -1
for i, x in enumerate(nls):
if name.lower() == x.lower() and name != 'function' and name != 'int' and name != 'double' and name != 'boolean' and name != 'String':
lastcopyid = i
if i not in hascopy:
hascopy[i] = 1
return i + 1000000
if lastcopyid != -1:
return lastcopyid + 1000000
return -1
rulead = np.zeros([641, 641])
astnode = {"pad": 0, "Unknown": 1}
nodess = {}
def stringfy(node):
ans = ""
if len(node.child) == 0:
if node.name[0] == 'Ġ':
ans += node.name[1:-4]
else:
ans = node.name[:-4]
else:
for x in node.child:
ans += stringfy(x) + " "
return ans
def getRule(node, currId, d):
global rules
global onelist
global rulelist
global fatherlist
global idenlist
global copynode
global expandnode
if node.name == "str_":
assert(len(node.child) == 1)
if len(node.child) == 0:
return [], []
if " -> End " not in rules:
rules[" -> End "] = len(rules)
return [rules[" -> End "]]
if len(node.child) >= 1 and node.child[0].name == "identifier" and 'extra_id' in node.child[0].child[0].name:
rulelist.append(rules['<' + node.child[0].child[0].name[:-4] + '>'])
fatherlist.append(currId)
idenlist.append(currId + 1)
expandnode = node.name
return
if len(node.child) >= 2 and node.child[1].name == "identifier" and 'extra_id' in node.child[1].child[0].name:
rulelist.append(rules['<' + node.child[1].child[0].name[:-4] + '>'])
fatherlist.append(currId)
idenlist.append(currId + 1)
expandnode = node.name
return
child = node.child#sorted(node.child, key=lambda x:x.name)
if len(node.child) == 1 and len(node.child[0].child) == 0 and ("identifier" in node.name or 'literal' in node.name):
if len(node.child[0].child) != 0:
print(node.child[0].name)
if node.name == "type":
print(node.printTree(node))
if "extra_id" in node.child[0].name:
rulelist.append(rules['<' + node.child[0].name[:-4] + '>'])
fatherlist.append(currId)
fathername.append(node.name)
idenlist.append(currId + 1)
expandnode = 'identifier'
else:
nodess[node.name] = 1
actions = tokenizer.convert_tokens_to_ids(tokenizer.tokenize(" " + node.child[0].name[:-4]))
#for i in range(len(actions)):
# print(tokenizer.convert_ids_to_tokens(actions[i]), end=" ")
#print(tokenizer.tokenize("sort the Array by the second element"))
#assert(0)
if node.name == "string_literal":
#print(node.child[0].name)
#print(tokenizer.tokenize(" " + node.child[0].name[:-4]))
actions = actions[:25]
node.child[0].name = "".join(tokenizer.convert_ids_to_tokens(actions)).replace("Ġ", " ").strip() + "_ter"
actions = [rules["string_literal -> End"]] + actions
#print(node.child[0].name)
#assert(0)
actions.reverse()
'''print(actions, tokenizer.tokenize(node.child[0].name[:-4]))
if len(actions) > 512:
actions = tokenizer.tokenize(node.child[0].name[:-4])#tokenizer.encode(node.child[0].name[:10])
assert(0)'''
for action in actions:
rulelist.append(action)
fatherlist.append(currId)
fathername.append(node.name)
'''rule = node.name + " -> End "
if rule in rules:
rulelist.append(rules[rule])
else:
rules[rule] = len(rules)
rulelist.append(rules[rule])
fatherlist.append(currId)
fathername.append(node.name)'''
currid = len(rulelist) - 1
else:
if node.name not in onelist:
rule = node.name + " -> "
for x in child:
rule += x.name + " "
if rule in rules:
rulelist.append(rules[rule])
else:
print(rule)
assert(0)
rules[rule] = len(rules)
rulelist.append(rules[rule])
fatherlist.append(currId)
fathername.append(node.name)
currid = len(rulelist) - 1
for x in child:
getRule(x, currid, d + 1)
else:
for x in (child):
if (True):
rule = node.name + " -> " + x.name
if rule in rules:
rulelist.append(rules[rule])
else:
print(rule)
assert(0)
rules[rule] = len(rules)
rulelist.append(rules[rule])
fatherlist.append(currId)
fathername.append(node.name)
getRule(x, len(rulelist) - 1, d + 1)
rule = node.name + " -> End "
if rule in rules:
rulelist.append(rules[rule])
else:
assert(0)
rules[rule] = len(rules)
rulelist.append(rules[rule])
fatherlist.append(currId)
fathername.append(node.name)
#return rulelist, fatherlistd
def mergeIdentifier(root):
if root.name in identifiers:
if False:
pass
else:
oname = ""
for x in root.child:
oname += x.name[:-4]
oname += "_ter"
nnode = Node(oname, 0)
nnode.father = root
root.child = [nnode]
for x in root.child:
mergeIdentifier(x)
return
def parseTree(treestr):
tokens = treestr.strip().split('🚀')[:-1]
root = Node(tokens[0], 0)
currnode = root
for i, x in enumerate(tokens[1:]):
if x != "^":
nnode = Node(x, i + 1)
nnode.father = currnode
currnode.child.append(nnode)
currnode = nnode
else:
currnode = currnode.father
return root
def filtererror(root):
if root.name == 'ERROR' and len(root.child) != 0:
return False
for x in root.child:
if not filtererror(x):
return False
return True
def getRuleList(root):
global rulelist
global fatherlist
global idenlist
global expandnode
nroot = Node("java", 0)
nroot.child = [root]
root.father = nroot
root = nroot
rulelist = []
fatherlist = []
try:
getRule(root, -1, 2)
except:
traceback.print_exc()
rulelist = [rules['start -> java']] + rulelist
fatherlist = [-1] + fatherlist
fatherlist = [x + 1 for x in fatherlist]
print(expandnode)
return rulelist, fatherlist, idenlist, expandnode
if __name__ == '__main__':
tmp = 0
idx = int(sys.argv[1])
data = pickle.load(open("datajava%d.pkl"%idx, "rb"))
pdata = []
lst1, lst2 = [], []
for i, entry in enumerate(tqdm(data)):
if i < 0:
continue
tree = entry['root']
nl = entry['input']
code = entry['code']
root = parseTree(tree)
nroot = Node("java", 0)
nroot.child = [root]
root.father = nroot
root = nroot
if not filtererror(root):
continue
rulelist = []
fatherlist = []
fathername = []
try:
getRule(root, -1, 2)
except:
traceback.print_exc()
#assert(0)
continue
#mergeIdentifier(root)
if "\"" in stringfy(root):
code = stringfy(root)
#print(len(tokenizer.encode(code)), len(rulelist))
#assert(0)
#code = stringfy(root)
#if 'for_statement -> for_ter (_ter init condition ;_ter update )_ter body' in rules or 'for_statement -> for_ter (_ter init condition ;_ter update )_ter body ' in rules:
# print(code)
# print(tree.split('🚀')[:-1])
# assert(0)
inputlist = [rules['start -> java']] + rulelist[:-1]
lst1.append(len(tokenizer.encode(code)))
lst2.append(len(inputlist))
'''rrdict = {}
for x in rules:
rrdict[rules[x]] = x
for i in range(len(inputlist)):
print(rrdict[inputlist[i]])'''
rulelist = [rules['start -> java']] + rulelist
if len(rulelist) > 512:
rulelist = []
fatherlist = []
fathername = []
continue
'''if nl != "":
if tmp >= 10:
assert(0)
tmp += 1
print(len(rulelist))
print(nl)
print(code)'''
fatherlist = [-1] + fatherlist
fatherlist = [x + 1 for x in fatherlist]
if nl == "":
pdata.append({'rulelist':rulelist, 'fatherlist':fatherlist})
else:
pdata.append({'nl':tokenizer.encode('<nl> ' + nl), 'rulelist':rulelist, 'fatherlist':fatherlist})
rulelist = []
fatherlist = []
fathername = []
print(np.mean(lst1), np.mean(lst2), rules['start -> java'])
open('pjavadata%d.pkl'%idx, 'wb').write(pickle.dumps(pdata))