/
json_to_seq_v1.py
257 lines (200 loc) · 7.74 KB
/
json_to_seq_v1.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
import argparse
import re
import json
import multiprocessing
import itertools
import tqdm
import joblib
import numpy as np
from datetime import datetime
from pathlib import Path
from sklearn import model_selection as sklearn_model_selection
METHOD_NAME, NUM = 'METHODNAME', 'NUM'
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', required=True, type=str)
parser.add_argument('--valid_p', type=float, default=0.2)
parser.add_argument('--max_path_length', type=int, default=8)
parser.add_argument('--max_path_width', type=int, default=2)
parser.add_argument('--use_method_name', type=bool, default=True)
parser.add_argument('--use_nums', type=bool, default=True)
parser.add_argument('--output_dir', required=True, type=str)
parser.add_argument('--n_jobs', type=int, default=multiprocessing.cpu_count())
parser.add_argument('--seed', type=int, default=239)
def __collect_asts(json_file):
asts = []
with open(json_file, 'r', encoding='utf-8') as f:
for line in f:
record = json.loads(line.strip())
if len(record['files']) >1:
print('helps')
ast = record['files'][0]
asts.append(ast)
return asts
def __terminals(ast, node_index, args):
stack, paths = [], []
def dfs(v):
stack.append(v)
v_node = ast['vertices'][v]
#class
if 'Class' in v_node['term']:
if v == node_index: # Top-level func def node.
if args.use_method_name:
paths.append((stack.copy(), METHOD_NAME))
else:
v_type = v_node['term']
if v_type in ['Empty', 'MemberAccess', 'Boolean', 'Import', 'RequiredParameter', 'Alias', 'Context', 'Integer', 'TextElement', 'Identifier', 'Class', 'Call', 'Null', 'Assignment', 'QualifiedAliasedImport', 'Comment', 'Negate', 'Statements', 'Decorator', 'Return', 'Function']:
paths.append((stack.copy(), v_node['term']))
else:
pass
children = return_children_nodes(ast['edges'], v)
if len(children) > 0:
for child in children:
dfs(child)
stack.pop()
def return_children_nodes(children, vertex):
query_idx = vertex + 1
return [i['target'] - 1 for i in ast['edges'] if i['source'] == query_idx]
dfs(node_index)
return paths
def __merge_terminals2_paths(v_path, u_path):
s, n, m = 0, len(v_path), len(u_path)
while s < min(n, m) and v_path[s] == u_path[s]:
s += 1
prefix = list(reversed(v_path[s:]))
lca = v_path[s - 1]
suffix = u_path[s:]
return prefix, lca, suffix
def __raw_tree_paths(ast, node_index, args):
tnodes = __terminals(ast, node_index, args)
tree_paths = []
for (v_path, v_value), (u_path, u_value) in itertools.combinations(
iterable=tnodes,
r=2,
):
prefix, lca, suffix = __merge_terminals2_paths(v_path, u_path)
if (len(prefix) + 1 + len(suffix) <= args.max_path_length) \
and (abs(len(prefix) - len(suffix)) <= args.max_path_width):
path = prefix + [lca] + suffix
tree_path = v_value, path, u_value
tree_paths.append(tree_path)
return tree_paths
def __delim_name(name):
if name in {METHOD_NAME, NUM}:
return name
def camel_case_split(identifier):
matches = re.finditer(
'.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)',
identifier,
)
return [m.group(0) for m in matches]
blocks = []
for underscore_block in name.split('_'):
blocks.extend(camel_case_split(underscore_block))
return '|'.join(block.lower() for block in blocks)
def __collect_sample(ast, fd_index, args):
root = ast['vertices'][fd_index]
if root['term'] not in ('Class'):
raise ValueError('Wrong node type.')
target = root['term']
tree_paths = __raw_tree_paths(ast, fd_index, args)
contexts = []
for tree_path in tree_paths:
start, connector, finish = tree_path
start, finish = __delim_name(start), __delim_name(finish)
connector = '|'.join(ast['vertices'][v]['term'] for v in connector)
context = f'{start},{connector},{finish}'
contexts.append(context)
if len(contexts) == 0:
return None
target = __delim_name(target)
context = ' '.join(contexts)
return f'{target} {context}'
def __collect_samples(ast, args):
samples = []
# Parse the AST if it is a class or a function with no parent node
targets = set()
for edge_index, edge in enumerate(ast['edges']):
targets.add(edge['target'])
for node_index, node in enumerate(ast['vertices']):
# if ((node['term'] == 'Class') or
# (node['term'] == 'Function' and node['vertexId'] not in targets)):
if node['term'] == 'Class':
sample = __collect_sample(ast, node_index, args)
if sample is not None:
samples.append(sample)
break #break on first sample returned
return samples
def __collect_all_and_save(asts, args, output_file, labels):
parallel = joblib.Parallel(n_jobs=args.n_jobs)
func = joblib.delayed(__collect_samples)
samples = parallel(func(ast, args) for ast in tqdm.tqdm(asts))
samples = list(itertools.chain.from_iterable(samples))
with open(output_file, 'w') as f:
for line_index, line in enumerate(samples):
f.write(line + ('' if line_index == len(samples) - 1 else '\n'))
def main():
args = parser.parse_args()
np.random.seed(args.seed)
print('start')
print(datetime.now())
data_dir = Path(args.data_dir)
positives = __collect_asts(data_dir / 'parsed_positive.json')
negatives = __collect_asts(data_dir / 'parsed_negative.json')
print('blah1')
#trains=1
positives_labels = np.ones((len(positives),))
negative_labels = np.zeros((len(negatives),))
training_set = positives + negatives
labels = np.concatenate((positives_labels, negative_labels))
# train, valid = sklearn_model_selection.train_test_split(
# trains,
# test_size=args.valid_p,
# )
X_train, X_test, y_train, y_test = sklearn_model_selection.train_test_split(
training_set, labels,
test_size=args.valid_p,
)
output_dir = Path(args.output_dir)
output_dir.mkdir(exist_ok=True)
# for split_name, split in zip(
# ('train', 'valid', 'test'),
# (train, valid, test),
# ):
for split_name, split, labels in zip(
('train', 'test'),
(X_train, X_test),
(y_train, y_test)
):
# save labels
output_labels_file = output_dir / f'{split_name}_label_output_file.npy'
with open(output_labels_file, 'wb') as f:
np.save(f, labels)
# save parsed sequence
output_file = output_dir / f'{split_name}_output_file.txt'
__collect_all_and_save(split, args, output_file,labels)
print(datetime.now())
print('done')
# def main():
# args = parser.parse_args()
# np.random.seed(args.seed)
# print('blah')
# data_dir = Path(args.data_dir)
# trains = __collect_asts(data_dir / 'python100k_train.json')
# evals = __collect_asts(data_dir / 'python50k_eval.json')
# print('blah1')
# print(len(trains))
# train, valid = sklearn_model_selection.train_test_split(
# trains,
# test_size=args.valid_p,
# )
# test = evals
# output_dir = Path(args.output_dir)
# output_dir.mkdir(exist_ok=True)
# for split_name, split in zip(
# ('train', 'valid', 'test'),
# (train, valid, test),
# ):
# output_file = output_dir / f'{split_name}_output_file.txt'
# __collect_all_and_save(split, args, output_file)
if __name__ == '__main__':
main()