-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_cst_extractor.py
277 lines (202 loc) · 10.1 KB
/
path_cst_extractor.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
from tree_sitter import Language, Parser
import itertools
import os
import random
import hashlib
def trailing_space(node,contents):
start_row=node.start_point[0]
end_row=node.end_point[0]
start_column=node.start_point[1]
end_columns=node.end_point[1]
# the spaces before the node
start_byte = node.start_byte
while start_byte > 0 and contents[start_byte-1:start_byte] in (b' ', b'\n', b'\t'):
start_byte -= 1
spaces_before = contents[start_byte:node.start_byte].decode('utf-8')
# the spaces after the node
end_byte = node.end_byte
while end_byte < len(contents) and contents[end_byte:end_byte+1] in (b' ', b'\n', b'\t'):
end_byte += 1
spaces_after = contents[node.end_byte:end_byte].decode('utf-8')
return spaces_before,spaces_after
def traverse(tree,token,token_dict,contents):
def _traverse(node):
for child in node.children:
if child.children==[]:
name= contents[child.start_byte:child.end_byte].decode('utf8')
if child.type!='comment': #filtra fuori i commenti
token.append(name)
token_dict.append(child)
_traverse(child)
_traverse(tree.root_node)
return(token,token_dict)
def leaf2leaf2(leaf_node, tree, content): #path in a form Terminal-HASH-Terminal, original code2vec format
method_ina_line = []
comb = list(itertools.combinations(leaf_node, 2))
for pair in comb:
start, end = pair
start_bf, start_after = trailing_space(start, content)
start_token = start_bf + content[start.start_byte:start.end_byte].decode('utf8') + start_after
end_bf, end_after = trailing_space(end, content)
end_token = end_bf + content[end.start_byte:end.end_byte].decode('utf8') + end_after
start_token = start_token.replace(" ", "WS").replace(",", "CMA").replace("\n", "NL").replace("\t", "TAB")
end_token = end_token.replace(" ", "WS").replace(",", "CMA").replace("\n", "NL").replace("\t", "TAB")
pathUP, pathDOWN = [start_token], [end_token, "<-"]
p1, p2 = start.parent, end.parent
pathUP.append(p1.type)
pathDOWN.extend([p2.type,"<-"])
while p1 != p2:
p1, p2 = p1.parent, p2.parent
if p1:
pathUP.append(p1.type)
if p2:
pathDOWN.extend([p2.type,"<-"])
pathDOWN.reverse()
dirty_path = pathUP + pathDOWN
path2 = []
go_down = False
for n, item in enumerate(dirty_path):
if item not in path2:
path2.append(item)
if item == "<-":
go_down = True
if not go_down and dirty_path[n+1] != "<-":
path2.append("->")
if item != "<-" and go_down and (n+1 < len(dirty_path)):
path2.append("<-")
if (len(path2) + 1) // 2 < 9: ###lenght of path reale senza le tokenIniziali è len(path2)-2 DIV2 con DIV2 divisione intera, ora è settata a 7
path_string = str(path2[1:-1]).encode('utf-8')
path_hash = str(hashlib.sha256(path_string).hexdigest())
formatted_hash = f'b"{path_hash}"'
path_final = f"{start_token},{formatted_hash},{end_token} "
method_ina_line.append(path_final)
return method_ina_line
def leaf2leaf3(leaf_node, tree, content): #path in a form Terminal- Path String - Terminal
method_ina_line = []
comb = list(itertools.combinations(leaf_node, 2))
for pair in comb:
start, end = pair
start_bf, start_after = trailing_space(start, content)
start_token = start_bf + content[start.start_byte:start.end_byte].decode('utf8') + start_after
end_bf, end_after = trailing_space(end, content)
end_token = end_bf + content[end.start_byte:end.end_byte].decode('utf8') + end_after
start_token = start_token.replace(" ", "WS").replace(",", "CMA").replace("\n", "NL").replace("\t", "TAB")
end_token = end_token.replace(" ", "WS").replace(",", "CMA").replace("\n", "NL").replace("\t", "TAB")
pathUP, pathDOWN = [start_token], [end_token, "<-"]
p1, p2 = start.parent, end.parent
pathUP.append(p1.type)
pathDOWN.extend([p2.type,"<-"])
while p1 != p2:
p1, p2 = p1.parent, p2.parent
if p1:
pathUP.append(p1.type)
if p2:
pathDOWN.extend([p2.type,"<-"])
pathDOWN.reverse()
dirty_path = pathUP + pathDOWN
path2 = []
go_down = False
for n, item in enumerate(dirty_path):
if item not in path2:
path2.append(item)
if item == "<-":
go_down = True
if not go_down and dirty_path[n+1] != "<-":
path2.append("->")
if item != "<-" and go_down and (n+1 < len(dirty_path)):
path2.append("<-")
if (len(path2) + 1) // 2 < 9: ###lenght of path reale senza le tokenIniziali è len(path2)-2 DIV2 con DIV2 divisione intera, ora è settata a 7
path_string = str(path2[1:-1]).encode('utf-8')
path_hash = str(hashlib.sha256(path_string).hexdigest())
path_final = f"{start_token},{path_string},{end_token} "
method_ina_line.append(path_final)
return method_ina_line
def leaf2leaf4(leaf_node, tree, content): #it use the content of the node instead of the type of the node as leaf2leaf3
method_ina_line = []
comb = list(itertools.combinations(leaf_node, 2))
for pair in comb:
start, end = pair
start_token = content[start.start_byte:start.end_byte].decode('utf8')
end_token = content[end.start_byte:end.end_byte].decode('utf8')
start_token = start_token.replace(" ", "WS").replace(",", "CMA").replace("\n", "NL")
end_token = end_token.replace(" ", "WS").replace(",", "CMA").replace("\n", "NL")
pathUP, pathDOWN = [start_token], [end_token, "<-"]
p1, p2 = start.parent, end.parent
pathUP.append(content[p1.start_byte:p1.end_byte].decode('utf8'))
pathDOWN.extend([content[p2.start_byte:p2.end_byte].decode('utf8'),"<-"])
while p1 != p2:
p1, p2 = p1.parent, p2.parent
if p1:
pathUP.append(content[p1.start_byte:p1.end_byte].decode('utf8'))
if p2:
pathDOWN.extend([content[p2.start_byte:p2.end_byte].decode('utf8'),"<-"])
pathDOWN.reverse()
dirty_path = pathUP + pathDOWN
path2 = []
go_down = False
for n, item in enumerate(dirty_path):
if item not in path2:
path2.append(item)
if item == "<-":
go_down = True
if not go_down and dirty_path[n+1] != "<-":
path2.append("->")
if item != "<-" and go_down and (n+1 < len(dirty_path)):
path2.append("<-")
if (len(path2) + 1) // 2 < 9: ###lenght of path reale senza le tokenIniziali è len(path2)-2 DIV2 con DIV2 divisione intera, ora è settata a 7
path_string = str(path2[1:-1]).encode('utf-8')
path_hash = str(hashlib.sha256(path_string).hexdigest())
formatted_hash = f'b"{path_hash}"'
path_final = f"{start_token},{formatted_hash},{end_token} " #prima passavo direttamente path_hash
method_ina_line.append(path_final)
return method_ina_line
def print_node(node, source_code, depth=0, indent=''):
start_bf, start_after = trailing_space(node, source_code)
node_code = start_bf + source_code[node.start_byte:node.end_byte].decode('utf8') + start_after
node_code = node_code.replace(" ", "WS").replace(",", "CMA").replace("\n", "NL").replace("\t", "TAB")
print(f"Node{depth} {indent}{node.type}: {node_code}")
if node.children:
for child in node.children:
print_node(child, source_code, depth + 1, indent + '-')
def create_dataset(origin, destination):
PY_LANGUAGE = Language('build/my-languages.so', 'python') ##importa il linguaggio
parser = Parser()
parser.set_language(PY_LANGUAGE) #crea il parser per python
curr=os.getcwd()
origin= os.path.join(curr, origin)
destination_folder = os.path.join(curr, destination)
destination_file = os.path.join(destination_folder, "datasetgrezzo.txt")
os.makedirs(destination_folder, exist_ok=True)
l=0
tot=0
with open(destination_file, 'w',encoding="utf-8") as dataset:
for root, dirs, files in os.walk(origin, topdown = False):
for name in files:
file_int=""
target_example=""
strexample=""
example=""
target=os.path.split(os.path.split(root)[1])[1]
print(target)
doc= os.path.join(root, name)
with open(doc, 'rb') as f:
token=[]
token_node=[]
contents = f.read()
tree = parser.parse(contents)
#print_node(tree.root_node, contents)
leaf,leaf_node= traverse(tree,token,token_node,contents)
example=set(leaf2leaf2(leaf_node,tree,contents)) #in leaf2leaf viene definita lenght max path
if len(example)!=0:
tot = tot + len(example)
if len(example)>500: ###definisce il numero di path max per riga di esempio
l=l+1
example=random.sample(example, min(500, len(example)))
strexample = ''.join(example)
file_int = strexample + file_int
if len(file_int)>0:
target_example= target + " " + file_int +'\n'
dataset.write(target_example) #scrivere su txt
print("dataset created")
print("numero example>500", l)
print("numero example tot:", tot/590)