-
Notifications
You must be signed in to change notification settings - Fork 3
/
word2vector.py
485 lines (440 loc) · 21.9 KB
/
word2vector.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import sys, os
os.path.abspath(os.path.join('..', './representation'))
from representation.code2vector import Code2vector
import pickle
from representation.CC2Vec import lmg_cc2ftr_interface
import os
from bert_serving.client import BertClient
# from gensim.models import word2vec, Doc2Vec
from nltk.tokenize import word_tokenize
from sklearn.metrics.pairwise import *
# Imports and method code2vec
from representation.code2vector import Code2vector
from representation.code2vec.vocabularies import VocabType
from representation.code2vec.config import Config
from representation.code2vec.model_base import Code2VecModelBase
import numpy as np
import re
import logging
MODEL_MODEL_LOAD_PATH = '/Users/haoye.tian/Documents/University/model/java14_model/saved_model_iter8.release'
MODEL_CC2Vec = '../representation/CC2Vec/'
class Word2vector:
def __init__(self, test_w2v=None, patch_w2v=None, path_patch_root=None):
# self.w2v = word2vec
self.test_w2v = test_w2v
self.patch_w2v = patch_w2v
self.path_patch_root = path_patch_root
# Init and Load the model for test cases
if self.test_w2v == 'code2vec':
config = Config(set_defaults=True, load_from_args=True, verify=False)
config.MODEL_LOAD_PATH = MODEL_MODEL_LOAD_PATH
config.EXPORT_CODE_VECTORS = True
model = Word2vector.load_model_code2vec_dynamically(config)
config.log('Done creating code2vec model')
self.c2v = Code2vector(model)
# =======================
# init for patch vector
if self.patch_w2v == 'cc2vec':
self.dictionary = pickle.load(open(MODEL_CC2Vec+'dict.pkl', 'rb'))
elif self.patch_w2v == 'bert':
import nltk
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
nltk.download('punkt')
logging.getLogger().info('Waiting for Bert server')
self.m = BertClient(check_length=False, check_version=False)
@staticmethod
def load_model_code2vec_dynamically(config: Config) -> Code2VecModelBase:
assert config.DL_FRAMEWORK in {'tensorflow', 'keras'}
if config.DL_FRAMEWORK == 'tensorflow':
from representation.code2vec.tensorflow_model import Code2VecModel
elif config.DL_FRAMEWORK == 'keras':
from representation.code2vec.keras_model import Code2VecModel
return Code2VecModel(config)
def convert_both(self, test_name, test_text, patch_ids):
try:
#1 represent test cases by embedding test function with code2vec(output dimension: 384)
function = test_text
test_vector = self.c2v.convert(function)
#2 represent patch
# first of all, find associated patch path
if len(patch_ids) == 1 and patch_ids[0].endswith('-one'):
# '-one' means this complete patch is the solution for the failed test case
project = patch_ids[0].split('_')[0]
id = patch_ids[0].split('_')[1].replace('-one', '')
path_patch = self.path_patch_root + project + '/' + id + '/'
patch_ids = os.listdir(path_patch)
path_patch_ids = [path_patch + patch_id for patch_id in patch_ids]
else:
path_patch_ids = []
for name in patch_ids:
project = name.split('_')[0]
id = name.split('_')[1]
patch_id = name.split('_')[1] + '_' + name.split('_')[2] + '.patch'
path_patch = self.path_patch_root + project + '/' + id + '/'
path_patch_ids.append(os.path.join(path_patch, patch_id))
# embedding each patch and combine them together to learn the overall behaviour
multi_vector = []
for path_patch_id in path_patch_ids:
if self.patch_w2v == 'cc2vec':
learned_vector = lmg_cc2ftr_interface.learned_feature(path_patch_id, load_model=MODEL_CC2Vec + 'cc2ftr.pt', dictionary=self.dictionary)
learned_vector = list(learned_vector.flatten())
elif self.patch_w2v == 'bert':
learned_vector = self.learned_feature(path_patch_id, self.patch_w2v)
elif self.patch_w2v == 'string':
learned_vector = self.extract_text(path_patch_id, )
multi_vector.append(learned_vector)
if self.patch_w2v == 'string':
patch_vector = ''
for s in multi_vector:
patch_vector += s
patch_vector = [patch_vector]
else:
patch_vector = np.array(multi_vector).sum(axis=0)
except Exception as e:
raise e
if self.patch_w2v == 'string':
if patch_vector == ['']:
raise Exception('null patch string')
return test_vector, patch_vector
else:
if test_vector.size == 0 or patch_vector.size == 0:
raise Exception('null vector')
return test_vector, patch_vector
# def convert(self, test_name, data_text):
# if self.test_w2v == 'code2vec':
# test_vector = []
# for i in range(len(data_text)):
# function = data_text[i]
# try:
# vector = self.c2v.convert(function)
# except Exception as e:
# print('{} test_name:{} Exception:{}'.format(i, test_name[i], 'Wrong syntax'))
# continue
# print('{} test_name:{}'.format(i, test_name[i]))
# test_vector.append(vector)
# return test_vector
#
# if self.patch_w2v == 'cc2vec':
# patch_vector = []
# for i in range(len(data_text)):
# patch_ids = data_text[i]
# # find path_patch
# if len(patch_ids) == 1 and patch_ids[0].endwith('-one'):
# project = patch_ids[0].split('_')[0]
# id = patch_ids[0].split('_')[1].replace('-one','')
# path_patch = self.path_patch_root + project +'/'+ id + '/'
# patch_ids = os.listdir(path_patch)
# path_patch_ids = [path_patch + patch_id for patch_id in patch_ids]
# else:
# path_patch_ids = []
# for name in patch_ids:
# project = name.split('_')[0]
# id = name.split('_')[1]
# patch_id = name.split('_')[1] +'_'+ name.split('_')[2] + '.patch'
# path_patch = self.path_patch_root + project +'/'+ id + '/'
# path_patch_ids.append(os.path.join(path_patch, patch_id))
#
# multi_vector = []
# for path_patch_id in path_patch_ids:
# learned_vector = lmg_cc2ftr_interface.learned_feature(path_patch_id, load_model=MODEL_CC2Vec+'cc2ftr.pt', dictionary=self.dictionary)
# multi_vector.append(list(learned_vector.flatten()))
# combined_vector = np.array(multi_vector).mean(axis=0)
# patch_vector.append(combined_vector)
# return patch_vector
def convert_single_patch(self, path_patch):
try:
if self.patch_w2v == 'cc2vec':
multi_vector = [] # sum up vectors of different parts of patch
# patch = os.listdir(path_patch)
# for part in patch:
for root, dirs, files in os.walk(path_patch):
for file in files:
if file.endswith('.patch'):
p = os.path.join(root, file)
learned_vector = lmg_cc2ftr_interface.learned_feature(p, load_model=MODEL_CC2Vec + 'cc2ftr.pt', dictionary=self.dictionary)
multi_vector.append(list(learned_vector.flatten()))
combined_vector = np.array(multi_vector).sum(axis=0)
elif self.patch_w2v == 'bert':
multi_vector = []
multi_vector_cross = []
patch = os.listdir(path_patch)
for part in patch:
p = os.path.join(path_patch, part)
learned_vector = self.learned_feature(p, self.patch_w2v)
learned_vector_cross = self.learned_feature_cross(p, self.patch_w2v)
multi_vector.append(learned_vector)
multi_vector_cross.append(learned_vector_cross)
combined_vector = np.array(multi_vector).sum(axis=0)
combined_vector_cross = np.array(multi_vector_cross).sum(axis=0)
return combined_vector, combined_vector_cross
elif self.patch_w2v == 'string':
multi_vector = []
patch = os.listdir(path_patch)
for part in patch:
p = os.path.join(path_patch, part)
learned_vector = self.extract_text(p, )
multi_vector.append(learned_vector)
combined_vector = ''
for s in multi_vector:
combined_vector += s
combined_vector = [combined_vector]
# combined_vector = np.array(multi_vector).mean(axis=0)
return combined_vector, None
except Exception as e:
raise e
def extract_text(self, path_patch, ):
try:
bugy_all = self.get_only_change(path_patch, type='buggy')
patched_all = self.get_only_change(path_patch, type='patched')
except Exception as e:
# print('patch: {}, exception: {}'.format(path_patch, e))
raise e
return bugy_all + patched_all
def learned_feature(self, path_patch, w2v):
try:
# bugy_all = self.get_diff_files_frag(path_patch, type='buggy')
# patched_all = self.get_diff_files_frag(path_patch, type='patched')
bugy_all = self.get_only_change(path_patch, type='buggy')
patched_all = self.get_only_change(path_patch, type='patched')
# tokenize word
bugy_all_token = word_tokenize(bugy_all)
patched_all_token = word_tokenize(patched_all)
bug_vec, patched_vec = self.output_vec(w2v, bugy_all_token, patched_all_token)
except Exception as e:
# print('patch: {}, exception: {}'.format(path_patch, e))
raise e
bug_vec = bug_vec.reshape((1, -1))
patched_vec = patched_vec.reshape((1, -1))
# embedding feature cross
# subtract, multiple, cos, euc = self.multi_diff_features(bug_vec, patched_vec)
# embedding = np.hstack((subtract, multiple, cos, euc,))
embedding = self.subtraction(bug_vec, patched_vec)
return list(embedding.flatten())
def learned_feature_cross(self, path_patch, w2v):
try:
bugy_all = self.get_diff_files_frag(path_patch, type='buggy')
patched_all = self.get_diff_files_frag(path_patch, type='patched')
# tokenize word
bugy_all_token = word_tokenize(bugy_all)
patched_all_token = word_tokenize(patched_all)
bug_vec, patched_vec = self.output_vec(w2v, bugy_all_token, patched_all_token)
except Exception as e:
# print('patch: {}, exception: {}'.format(path_patch, e))
raise e
bug_vec = bug_vec.reshape((1, -1))
patched_vec = patched_vec.reshape((1, -1))
# embedding feature cross
subtract, multiple, cos, euc = self.multi_diff_features(bug_vec, patched_vec)
embedding = np.hstack((subtract, multiple, cos, euc,))
return list(embedding.flatten())
def subtraction(self, buggy, patched):
return buggy - patched
def multiplication(self, buggy, patched):
return buggy * patched
def cosine_similarity(self, buggy, patched):
return paired_cosine_distances(buggy, patched)
def euclidean_similarity(self, buggy, patched):
return paired_euclidean_distances(buggy, patched)
def multi_diff_features(self, buggy, patched):
subtract = self.subtraction(buggy, patched)
multiple = self.multiplication(buggy, patched)
cos = self.cosine_similarity(buggy, patched).reshape((1, 1))
euc = self.euclidean_similarity(buggy, patched).reshape((1, 1))
return subtract, multiple, cos, euc
def output_vec(self, w2v, bugy_all_token, patched_all_token):
if w2v == 'bert':
if bugy_all_token == []:
bug_vec = np.zeros((1, 1024))
else:
bug_vec = self.m.encode([bugy_all_token], is_tokenized=True)
if patched_all_token == []:
patched_vec = np.zeros((1, 1024))
else:
patched_vec = self.m.encode([patched_all_token], is_tokenized=True)
elif w2v == 'doc':
# m = Doc2Vec.load('../model/doc_file_64d.model')
m = Doc2Vec.load('../model/Doc_frag_ASE.model')
bug_vec = m.infer_vector(bugy_all_token, alpha=0.025, steps=300)
patched_vec = m.infer_vector(patched_all_token, alpha=0.025, steps=300)
else:
print('wrong model')
raise
return bug_vec, patched_vec
def get_only_change(self, path_patch, type='patched'):
with open(path_patch, 'r+') as file:
lines = ''
p = r"([^\w_])"
# try:
for line in file:
line = line.strip()
if line != '':
if line.startswith('@@') or line.startswith('diff') or line.startswith('index'):
continue
elif type == 'buggy':
if line.startswith('--- ') or line.startswith('-- ') or line.startswith('PATCH_DIFF_ORIG=---'):
continue
elif line.startswith('-'):
if line[1:].strip() == '':
continue
if line[1:].strip().startswith('//'):
continue
line = re.split(pattern=p, string=line[1:].strip())
final = []
for s in line:
s = s.strip()
if s == '' or s == ' ':
continue
# CamelCase to underscore_case
cases = re.split(pattern='(?=[A-Z0-9])', string=s)
for c in cases:
if c == '' or c == ' ':
continue
final.append(c)
line = ' '.join(final)
lines += line.strip() + ' '
else:
# do nothing
pass
elif type == 'patched':
if line.startswith('+++ ') or line.startswith('++ '):
continue
# line = re.split(pattern=p, string=line.split(' ')[1].strip())
# lines += ' '.join(line) + ' '
elif line.startswith('+'):
if line[1:].strip() == '':
continue
if line[1:].strip().startswith('//'):
continue
line = re.split(pattern=p, string=line[1:].strip())
final = []
for s in line:
s = s.strip()
if s == '' or s == ' ':
continue
# CamelCase to underscore_case
cases = re.split(pattern='(?=[A-Z0-9])', string=s)
for c in cases:
if c == '' or c == ' ':
continue
final.append(c)
line = ' '.join(final)
lines += line.strip() + ' '
else:
# do nothing
pass
return lines
def get_diff_files_frag(self, path_patch, type):
with open(path_patch, 'r') as file:
lines = ''
p = r"([^\w_])"
flag = True
# try:
for line in file:
line = line.strip()
if '*/' in line:
flag = True
continue
if flag == False:
continue
if line != '':
if line.startswith('@@') or line.startswith('diff') or line.startswith('index'):
continue
if line.startswith('Index') or line.startswith('==='):
continue
elif '/*' in line:
flag = False
continue
elif type == 'buggy':
if line.startswith('--- ') or line.startswith('-- ') or line.startswith('PATCH_DIFF_ORIG=---'):
continue
# line = re.split(pattern=p, string=line.split(' ')[1].strip())
# lines += ' '.join(line) + ' '
elif line.startswith('-'):
if line[1:].strip() == '':
continue
if line[1:].strip().startswith('//'):
continue
line = re.split(pattern=p, string=line[1:].strip())
final = []
for s in line:
s = s.strip()
if s == '' or s == ' ':
continue
# CamelCase to underscore_case
cases = re.split(pattern='(?=[A-Z0-9])', string=s)
for c in cases:
if c == '' or c == ' ':
continue
final.append(c)
line = ' '.join(final)
lines += line.strip() + ' '
elif line.startswith('+'):
# do nothing
pass
else:
line = re.split(pattern=p, string=line.strip())
final = []
for s in line:
s = s.strip()
if s == '' or s == ' ':
continue
# CamelCase to underscore_case
cases = re.split(pattern='(?=[A-Z0-9])', string=s)
for c in cases:
if c == '' or c == ' ':
continue
final.append(c)
line = ' '.join(final)
lines += line.strip() + ' '
elif type == 'patched':
if line.startswith('+++ ') or line.startswith('++ '):
continue
# line = re.split(pattern=p, string=line.split(' ')[1].strip())
# lines += ' '.join(line) + ' '
elif line.startswith('+'):
if line[1:].strip() == '':
continue
if line[1:].strip().startswith('//'):
continue
line = re.split(pattern=p, string=line[1:].strip())
final = []
for s in line:
s = s.strip()
if s == '' or s == ' ':
continue
# CamelCase to underscore_case
cases = re.split(pattern='(?=[A-Z0-9])', string=s)
for c in cases:
if c == '' or c == ' ':
continue
final.append(c)
line = ' '.join(final)
lines += line.strip() + ' '
elif line.startswith('-'):
# do nothing
pass
else:
line = re.split(pattern=p, string=line.strip())
final = []
for s in line:
s = s.strip()
if s == '' or s == ' ':
continue
# CamelCase to underscore_case
cases = re.split(pattern='(?=[A-Z0-9])', string=s)
for c in cases:
if c == '' or c == ' ':
continue
final.append(c)
line = ' '.join(final)
lines += line.strip() + ' '
# except Exception:
# print(Exception)
# return 'Error'
return lines