-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolvetree_sitter.py
198 lines (196 loc) · 6.78 KB
/
solvetree_sitter.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
import os
from tqdm import tqdm
import pickle
import json
import numpy as np
lst = ['train', "test1"]#["train", "dev", "test1"]
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codet5-small")
rules = tokenizer.get_vocab()
onelist = ['argument_list', 'formal_parameters', 'block', 'array_initializer', 'switch_block', 'type_arguments', "method_declaration", "modifiers"]
rulelist = []
fatherlist = []
fathername = []
depthlist = []
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 getRule(node, nls, currId, d):
global rules
global onelist
global rulelist
global fatherlist
global depthlist
global copynode
global rulead
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 "]]
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 node.name == "string_literal":
if "string_literal -> srini_string" not in rules:
rules["string_literal -> srini_string"] = len(rules)
rulelist.append(rules["string_literal -> srini_string"])
fatherlist.append(currId)
fathername.append(node.name)
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)
#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:
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, nls, currid, d + 1)
else:
#assert(0)
for x in (child):
if (True):
rule = node.name + " -> " + x.name
if rule in rules:
rulelist.append(rules[rule])
else:
rules[rule] = len(rules)
rulelist.append(rules[rule])
fatherlist.append(currId)
fathername.append(node.name)
getRule(x, nls, len(rulelist) - 1, d + 1)
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)
#return rulelist, fatherlistd
for j in range(0, 2):
filename = lst[j]
processdata = []
#inputdir = x + "_input/"
#outputdir = x + "_output/"
data = pickle.load(open('%sdata.pkl'%filename, 'rb'))
for i, entry in enumerate(tqdm(data)):
nls = entry['input']
root = entry['root']
#print(root.printTree(root))
#addException(root)
rulelist = []
fatherlist = []
fathername = []
try:
getRule(root, nls, -1, 2)
except:
print(i)
#assert(0)
continue
processdata.append({"rulelist": rulelist, "fatherlist": fatherlist, "fathername": fathername, 'input':nls})
rulelist = []
fatherlist = []
fathername = []
#assert(0)
open('processed%s.pkl'%filename, 'wb').write(pickle.dumps(processdata))
print(rules)
print(len(rules))
print(nodess)
open('rule2.pkl', 'wb').write(pickle.dumps(rules))