-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_utils.py
More file actions
425 lines (377 loc) · 15.2 KB
/
_utils.py
File metadata and controls
425 lines (377 loc) · 15.2 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import os
import json
import logging
from preprocess_MCMD import check_angular_convention, get_content
logger = logging.getLogger(__name__)
def add_lang_by_task(target_str, task, sub_task):
if task == 'summarize':
target_str = '<en> ' + target_str
elif task == 'cmt_msg_gen':
target_str = '<en> ' + target_str
elif task == 'refine':
target_str = '<java> ' + target_str
elif task == 'translate':
if sub_task == 'java-cs':
target_str = '<c_sharp> ' + target_str
else:
target_str = '<java> ' + target_str
elif task == 'concode':
target_str = '<java> ' + target_str
elif task == 'defect':
target_str = target_str
return target_str
def convert_examples_to_features(item):
example, example_index, tokenizer, args, stage, setting, filter_none_scope, target_is_knwon_part, no_pos_no_segment = item
if args.model_type in ['t5', 'codet5'] and args.add_task_prefix:
if args.sub_task != 'none':
source_str = "{} {}: {}".format(args.task, args.sub_task, example.source)
else:
source_str = "{}: {}".format(args.task, example.source)
else:
source_str = example.source
source_str = source_str.replace('</s>', '<unk>')
source_ids = tokenizer.encode(source_str, max_length=args.max_source_length, padding='max_length', truncation=True)
noise_or_not = False
assert source_ids.count(tokenizer.eos_token_id) == 1
if stage == 'test' and args.task not in ['cmt_msg_gen']:
target_ids = []
else:
target_str = example.target
if args.add_lang_ids:
target_str = add_lang_by_task(example.target, args.task, args.sub_task)
if args.task in ['defect', 'clone']:
if target_str == 0:
target_str = 'false'
elif target_str == 1:
target_str = 'true'
else:
raise NameError
target_str = target_str.replace('</s>', '<unk>')
if args.task in ['cmt_msg_gen']:
if args.co_teaching:
noise_or_not = bool(example.noise)
add_label = None
if args.multi_task:
add_label = args.add_prompt
target_ids_dict = dict()
target_ids_dict['type_scope'] = tokenizer.encode(get_content(target_str, content_type=['type','scope'], add_label=add_label), \
max_length=args.max_part_length['type_scope'], \
padding='max_length', truncation=True)
target_ids_dict['subject'] = tokenizer.encode(get_content(target_str, content_type=['subject'], add_label=add_label), \
max_length=args.max_part_length['subject'], \
padding='max_length', truncation=True)
if setting is None:
if stage == 'train':
logger.warning("args.setting should be set.")
else:
setting = {'front_part':['type','scope'], 'back_part': ['subject']}
else:
setting = {'front_part':setting['known_part'], 'back_part': setting['unknown_part']}
if target_is_knwon_part:
target_ids_dict["front_part"] = tokenizer.encode(target_str, \
max_length=args.max_target_length-args.max_part_length['_'.join(setting["back_part"])], \
padding='max_length', truncation=True)
else:
if '_'.join(setting["front_part"]) in ['type_scope', 'subject'] and '_'.join(setting["back_part"]) in ['type_scope', 'subject']:
target_ids_dict["front_part"] = target_ids_dict['_'.join(setting["front_part"])]
target_ids_dict["back_part"] = target_ids_dict['_'.join(setting["back_part"])]
else:
target_ids_dict["front_part"] = tokenizer.encode(get_content(target_str, content_type=setting["front_part"], add_label=add_label), \
max_length=args.max_target_length-args.max_unknown_part_length, \
padding='max_length', truncation=True)
target_ids_dict["back_part"] = tokenizer.encode(get_content(target_str, content_type=setting["back_part"], add_label=add_label), \
max_length=args.max_unknown_part_length, \
padding='max_length', truncation=True)
target_ids = tokenizer.encode(target_str, max_length=args.max_target_length, padding='max_length',
truncation=True)
if args.task in ['cmt_msg_gen']:
target_ids = [tokenizer.pad_token_id] * args.max_target_length
front_part_eos_idx = target_ids_dict["front_part"].index(tokenizer.eos_token_id)
target_ids[:front_part_eos_idx] = target_ids_dict["front_part"][:front_part_eos_idx]
target_ids[front_part_eos_idx] = tokenizer.eos_token_id
if not target_is_knwon_part:
target_ids[front_part_eos_idx:front_part_eos_idx+args.max_part_length['_'.join(setting["back_part"])]] = target_ids_dict["back_part"]
target_ids[front_part_eos_idx] = tokenizer.eos_token_id
assert target_ids.count(tokenizer.eos_token_id) == 2
position_ids, segment_ids = [0]*(len(target_ids)), [0]*(len(target_ids))
if not no_pos_no_segment:
if setting["front_part"] == ['subject']:
segment_ids[0] = 1
eos_idx_list = [i for i, v in enumerate(target_ids) if v == tokenizer.eos_token_id]
position_ids[1:1+eos_idx_list[0]] = list(range(eos_idx_list[0]))
segment_ids[1:1+eos_idx_list[0]] = [1]*(eos_idx_list[0])
if not target_is_knwon_part:
position_ids[1+eos_idx_list[0]:1+eos_idx_list[1]] = list(range(eos_idx_list[1]-eos_idx_list[0]))
return InputFeatures(
example_index,
source_ids,
target_ids,
position_ids,
segment_ids,
noise_or_not,
index=example.abs_idx,
url=None #example.url
)
else:
assert target_ids.count(tokenizer.eos_token_id) == 1
return InputFeatures(
example_index,
source_ids,
target_ids,
url=None #example.url
)
def convert_clone_examples_to_features(item):
example, example_index, tokenizer, args = item
if args.model_type in ['t5', 'codet5'] and args.add_task_prefix:
source_str = "{}: {}".format(args.task, example.source)
target_str = "{}: {}".format(args.task, example.target)
else:
source_str = example.source
target_str = example.target
code1 = tokenizer.encode(source_str, max_length=args.max_source_length, padding='max_length', truncation=True)
code2 = tokenizer.encode(target_str, max_length=args.max_source_length, padding='max_length', truncation=True)
source_ids = code1 + code2
return CloneInputFeatures(example_index, source_ids, example.label, example.url1, example.url2)
def convert_defect_examples_to_features(item):
example, example_index, tokenizer, args = item
if args.model_type in ['t5', 'codet5'] and args.add_task_prefix:
source_str = "{}: {}".format(args.task, example.source)
else:
source_str = example.source
code = tokenizer.encode(source_str, max_length=args.max_source_length, padding='max_length', truncation=True)
return DefectInputFeatures(example_index, code, example.target)
class CloneInputFeatures(object):
"""A single training/test features for a example."""
def __init__(self,
example_id,
source_ids,
label,
url1,
url2
):
self.example_id = example_id
self.source_ids = source_ids
self.label = label
self.url1 = url1
self.url2 = url2
class DefectInputFeatures(object):
"""A single training/test features for a example."""
def __init__(self,
example_id,
source_ids,
label
):
self.example_id = example_id
self.source_ids = source_ids
self.label = label
class InputFeatures(object):
"""A single training/test features for a example."""
def __init__(self,
example_id,
source_ids,
target_ids,
position_ids,
segment_ids,
noise_or_not,
index,
url=None
):
self.example_id = example_id
self.source_ids = source_ids
self.target_ids = target_ids
self.position_ids = position_ids
self.segment_ids = segment_ids
self.noise_or_not = noise_or_not
self.index = index
self.url = url
class Example(object):
"""A single training/test example."""
def __init__(self,
idx,
abs_idx,
source,
target,
noise=0,
url=None,
task='',
sub_task=''
):
self.idx = idx
self.abs_idx = abs_idx
self.source = source
self.target = target
self.noise = noise
self.url = url
self.task = task
self.sub_task = sub_task
class CloneExample(object):
"""A single training/test example."""
def __init__(self,
code1,
code2,
label,
url1,
url2
):
self.source = code1
self.target = code2
self.label = label
self.url1 = url1
self.url2 = url2
def read_translate_examples(filename, data_num):
"""Read examples from filename."""
examples = []
assert len(filename.split(',')) == 2
src_filename = filename.split(',')[0]
trg_filename = filename.split(',')[1]
idx = 0
with open(src_filename) as f1, open(trg_filename) as f2:
for line1, line2 in zip(f1, f2):
src = line1.strip()
trg = line2.strip()
examples.append(
Example(
idx=idx,
source=src,
target=trg,
)
)
idx += 1
if idx == data_num:
break
return examples
def read_refine_examples(filename, data_num):
"""Read examples from filename."""
examples = []
assert len(filename.split(',')) == 2
src_filename = filename.split(',')[0]
trg_filename = filename.split(',')[1]
idx = 0
with open(src_filename) as f1, open(trg_filename) as f2:
for line1, line2 in zip(f1, f2):
examples.append(
Example(
idx=idx,
source=line1.strip(),
target=line2.strip(),
)
)
idx += 1
if idx == data_num:
break
return examples
def read_concode_examples(filename, data_num):
"""Read examples from filename."""
examples = []
with open(filename) as f:
for idx, line in enumerate(f):
x = json.loads(line)
examples.append(
Example(
idx=idx,
source=x["nl"].strip(),
target=x["code"].strip()
)
)
idx += 1
if idx == data_num:
break
return examples
def read_summarize_examples(filename, data_num):
"""Read examples from filename."""
examples = []
with open(filename, encoding="utf-8") as f:
for idx, line in enumerate(f):
line = line.strip()
js = json.loads(line)
if 'idx' not in js:
js['idx'] = idx
code = ' '.join(js['code_tokens']).replace('\n', ' ')
code = ' '.join(code.strip().split())
nl = ' '.join(js['docstring_tokens']).replace('\n', '')
nl = ' '.join(nl.strip().split())
examples.append(
Example(
idx=idx,
source=code,
target=nl,
)
)
if idx + 1 == data_num:
break
return examples
def read_cmt_msg_examples(filename, data_num, select_idx_list=None):
"""Read examples from filename."""
msg_file_path = filename.replace(".diff.txt", ".msg.txt")
noise_file_path = filename.replace(".diff.txt", ".noise.txt")
examples = []
with open(filename, encoding="utf-8") as diff_f, open(msg_file_path, encoding="utf-8") as msg_f:
diff = diff_f.readlines()
msg = msg_f.readlines()
if os.path.exists(noise_file_path):
with open(noise_file_path) as noise_f:
noise_list = noise_f.read().strip().split("\n")
noise_list = [int(i.strip()) for i in noise_list]
else:
noise_list = [0] * len(msg)
later_idx = 0
for idx in range(len(msg)):
if select_idx_list is None or idx in select_idx_list:
examples.append(
Example(
idx=later_idx,
abs_idx=idx,
source=diff[idx],
target=msg[idx],
noise=noise_list[idx],
)
)
if later_idx + 1 == data_num:
break
later_idx+=1
return examples
def read_defect_examples(filename, data_num):
"""Read examples from filename."""
examples = []
with open(filename, encoding="utf-8") as f:
for idx, line in enumerate(f):
line = line.strip()
js = json.loads(line)
code = ' '.join(js['func'].split())
examples.append(
Example(
idx=js['idx'],
source=code,
target=js['target']
)
)
if idx + 1 == data_num:
break
return examples
def read_clone_examples(filename, data_num):
"""Read examples from filename."""
index_filename = filename
url_to_code = {}
with open('/'.join(index_filename.split('/')[:-1]) + '/data.jsonl') as f:
for line in f:
line = line.strip()
js = json.loads(line)
code = ' '.join(js['func'].split())
url_to_code[js['idx']] = code
data = []
with open(index_filename) as f:
idx = 0
for line in f:
line = line.strip()
url1, url2, label = line.split('\t')
if url1 not in url_to_code or url2 not in url_to_code:
continue
if label == '0':
label = 0
else:
label = 1
data.append(CloneExample(url_to_code[url1], url_to_code[url2], label, url1, url2))
idx += 1
if idx == data_num:
break
return data