-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.py
1252 lines (1109 loc) · 48 KB
/
demo.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# dependencies.
import os
os.system('pip install clang==6.0.0.2')
os.system('pip install nltk==3.3')
os.system('pip install natsort')
import re
import gc
import time
import sys
import math
import random
import numpy as np
import pandas as pd
import nltk
nltk.download('stopwords')
from nltk.tokenize import TweetTokenizer
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import clang.cindex
import clang.enumerations
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as torchdata
from sklearn.metrics import accuracy_score
import pickle
from natsort import natsorted
# environment settings.
_COLAB_ = 0 if (os.getenv('COLAB_GPU', 'NONE') == 'NONE') else 1 # 0 : Local environment, 1 : Google Colaboratory.
# file paths.
rootPath = './drive/My Drive/Colab Notebooks/' if (_COLAB_) else './'
# dataPath = rootPath + '/data/'
# sDatPath = dataPath + '/security_patch/'
# pDatPath = dataPath + '/positives/'
# nDatPath = dataPath + '/negatives/'
testPath = rootPath + '/testdata/'
modlPath = rootPath + '/model/'
tempPath = rootPath + '/tmp/'
resuPath = rootPath + '/results/'
logsPath = rootPath + '/logs/'
# hyper-parameters. (affect GPU memory size)
_DiffEmbedDim_ = 128 # 128
_DiffMaxLen_ = 600 # 200(0.7), 314(0.8), 609(0.9), 1100(0.95), 2200(0.98), 3289(0.99), 5000(0.995), 10000(0.9997)
_DRnnHidSiz_ = 16 # 16
_MsgEmbedDim_ = 128 # 128
_MsgMaxLen_ = 200 # 54(0.9), 78(0.95), 130(0.98), 187(0.99), 268(0.995), 356(0.998), 516(0.999), 1434(1)
_MRnnHidSiz_ = 16 # 16
_TwinEmbedDim_ = 128 # 128
_TwinMaxLen_ = 800 # 224(0.8), 425(0.9), 755(0.95), 1448(0.98), 2270(0.99)
_TRnnHidSiz_ = 16 # 16
# hyper-parameters. (affect training speed)
_DRnnBatchSz_ = 128 # 128
_DRnnLearnRt_ = 0.0001 # 0.0001
_MRnnBatchSz_ = 128 # 128
_MRnnLearnRt_ = 0.0001 # 0.0001
_PRnnBatchSz_ = 256 # 256
_PRnnLearnRt_ = 0.0005 # 0.0005
_TRnnBatchSz_ = 256 # 256
_TRnnLearnRt_ = 0.0005 # 0.0005
# hyper-parameters. (trivial network parameters, unnecessary to modify)
_DiffExtraDim_ = 2 # 2
_TwinExtraDim_ = 1 # 1
_DRnnHidLay_ = 1 # 1
_MRnnHidLay_ = 1 # 1
_TRnnHidLay_ = 1 # 1
# hyper-parameters. (epoch related parameters, unnecessary to modify)
_DRnnMaxEpoch_ = 1000 # 1000
_DRnnPerEpoch_ = 1 # 1
_DRnnJudEpoch_ = 10 # 10
_MRnnMaxEpoch_ = 1000 # 1000
_MRnnPerEpoch_ = 1 # 1
_MRnnJudEpoch_ = 10 # 10
_PRnnMaxEpoch_ = 1000 # 1000
_PRnnPerEpoch_ = 1 # 1
_PRnnJudEpoch_ = 10 # 10
_TRnnMaxEpoch_ = 1 # 1000
_TRnnPerEpoch_ = 1 # 1
_TRnnJudEpoch_ = 10 # 10
# hyper-parameters. (flow control)
_DEBUG_ = 0 # 0 : release
# 1 : debug
_LOCK_ = 0 # 0 : unlocked - create random split sets.
# 1 : locked - use the saved split sets.
_MODEL_ = 1 # 0 : unlocked - train a new model.
# 1 : locked - load the saved model.
_DTYP_ = 1 # 0 : maintain both diff code and context code.
# 1 : only maintain diff code.
_CTYP_ = 1 # 0 : maintain both the code and comments.
# 1 : only maintain code and delete comments.
_NIND_ = 1 # -1 : not abstract tokens. (and will disable _NLIT_)
# 0 : abstract identifiers with VAR/FUNC.
# 1 : abstract identifiers with VARn/FUNCn.
_NLIT_ = 1 # 0 : abstract literals with LITERAL.
# 1 : abstract literals with LITERAL/n.
_TWIN_ = 1 # 0 : only twin neural network.
# 1 : twins + msg neural network.
# global variable.
start_time = time.time() #mark start time
# Logger: redirect the stream on screen and to file.
class Logger(object):
def __init__(self, filename = "log.txt"):
self.terminal = sys.stdout
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
def RunTime():
pTime = ' [TIME: ' + str(round((time.time() - start_time), 2)) + ' sec]'
return pTime
def ReadData():
'''
Read data from the files.
:return: data - a set of commit message, diff code, and labels.
[[['', ...], [['', ...], ['', ...], ...], 0/1], ...]
'''
def ReadCommitMsg(filename):
'''
Read commit message from a file.
:param filename: file name (string).
:return: commitMsg - commit message.
['line', 'line', ...]
'''
fp = open(filename, encoding='utf-8', errors='ignore') # get file point.
lines = fp.readlines() # read all lines.
#numLines = len(lines) # get the line number.
#print(lines)
# initialize commit message.
commitMsg = []
# get the wide range of commit message.
for line in lines:
if line.startswith('diff --git'):
break
else:
commitMsg.append(line)
#print(commitMsg)
# process the head of commit message.
while (1):
headMsg = commitMsg[0]
if (headMsg.startswith('From') or headMsg.startswith('Date:') or headMsg.startswith('Subject:')
or headMsg.startswith('commit') or headMsg.startswith('Author:')):
commitMsg.pop(0)
else:
break
#print(commitMsg)
# process the tail of commit message.
dashLines = [i for i in range(len(commitMsg))
if commitMsg[i].startswith('---')] # finds all lines start with ---.
if (len(dashLines)):
lnum = dashLines[-1] # last line number of ---
marks = [1 if (' file changed, ' in commitMsg[i] or ' files changed, ' in commitMsg[i]) else 0
for i in range(lnum, len(commitMsg))]
if (sum(marks)):
for i in reversed(range(lnum, len(commitMsg))):
commitMsg.pop(i)
#print(commitMsg)
#msgShow = ''
#for i in range(len(commitMsg)):
# msgShow += commitMsg[i]
#print(msgShow)
return commitMsg
def ReadDiffLines(filename):
'''
Read diff code from a file.
:param filename: file name (string).
:return: diffLines - diff code.
[['line', ...], ['line', ...], ...]
'''
fp = open(filename, encoding='utf-8', errors='ignore') # get file point.
lines = fp.readlines() # read all lines.
numLines = len(lines) # get the line number.
# print(lines)
atLines = [i for i in range(numLines) if lines[i].startswith('@@ ')] # find all lines start with @@.
atLines.append(numLines)
# print(atLines)
diffLines = []
for nh in range(len(atLines) - 1): # find all hunks.
# print(atLines[nh], atLines[nh + 1])
hunk = []
for nl in range(atLines[nh] + 1, atLines[nh + 1]):
# print(lines[nl], end='')
if lines[nl].startswith('diff --git '):
break
else:
hunk.append(lines[nl])
diffLines.append(hunk)
# print(hunk)
# print(diffLines)
# print(len(diffLines))
# process the last hunk.
lastHunk = diffLines[-1]
numLastHunk = len(lastHunk)
dashLines = [i for i in range(numLastHunk) if lastHunk[i].startswith('--')]
if (len(dashLines)):
lnum = dashLines[-1]
for i in reversed(range(lnum, numLastHunk)):
lastHunk.pop(i)
# print(diffLines)
# print(len(diffLines))
return diffLines
# create temp folder.
if not os.path.exists(tempPath):
os.mkdir(tempPath)
fp = open(tempPath + 'testfilelist.txt', 'w')
# initialize filelist.
filelist = []
for root, ds, fs in os.walk(testPath):
for file in fs:
if '.DS_Store' in file: continue
filename = os.path.join(root, file).replace('\\', '/')
filelist.append(filename)
filelist = natsorted(filelist)
# initialize data.
data = []
for filename in filelist:
# print(filename)
fp.write(filename + '\n')
commitMsg = ReadCommitMsg(filename)
diffLines = ReadDiffLines(filename)
data.append([commitMsg, diffLines, 0])
fp.close()
# save dataLoaded.
np.save(tempPath + '/testdata.npy', data, allow_pickle=True)
np.save(tempPath + '/testfilelist.npy', filelist, allow_pickle=True)
print('[INFO] <ReadData> Save ' + str(len(data)) + ' raw data to ' + tempPath + '/testdata.npy.' + RunTime())
return data, filelist
def GetDiffProps(data):
'''
Get the properties of the code in diff files.
:param data: [[[line, , ], [[line, , ], [line, , ], ...], 0/1], ...]
:return: props - [[[tokens], [nums], [nums], 0/1], ...]
'''
def RemoveSign(line):
'''
Remove the sign (+/-) in the first character.
:param line: a code line.
:return: process line.
'''
return ' ' + line[1:] if (line[0] == '+') or (line[0] == '-') else line
def GetClangTokens(line):
'''
Get the tokens of a line with the Clang tool.
:param line: a code line.
:return: tokens - ['tk', 'tk', ...] ('tk': string)
tokenTypes - [tkt, tkt, ...] (tkt: 1, 2, 3, 4, 5)
diffTypes - [dft, dft, ...] (dft: -1, 0, 1)
'''
# remove non-ascii
line = line.encode("ascii", "ignore").decode()
# defination.
tokenClass = [clang.cindex.TokenKind.KEYWORD, # 1
clang.cindex.TokenKind.IDENTIFIER, # 2
clang.cindex.TokenKind.LITERAL, # 3
clang.cindex.TokenKind.PUNCTUATION, # 4
clang.cindex.TokenKind.COMMENT] # 5
tokenDict = {cls: index + 1 for index, cls in enumerate(tokenClass)}
#print(tokenDict)
# initialize.
tokens = []
tokenTypes = []
diffTypes = []
# clang sparser.
idx = clang.cindex.Index.create()
tu = idx.parse('tmp.cpp', args=['-std=c++11'], unsaved_files=[('tmp.cpp', RemoveSign(line))], options=0)
for t in tu.get_tokens(extent=tu.cursor.extent):
#print(t.kind, t.spelling, t.location)
tokens.append(t.spelling)
tokenTypes.append(tokenDict[t.kind])
diffTypes.append(1 if (line[0] == '+') else -1 if (line[0] == '-') else 0)
#print(tokens)
#print(tokenTypes)
#print(diffTypes)
return tokens, tokenTypes, diffTypes
def GetWordTokens(line):
'''
Get the word tokens from a code line.
:param line: a code line.
:return: tokens - ['tk', 'tk', ...] ('tk': string)
'''
tknzr = TweetTokenizer()
tokens = tknzr.tokenize(RemoveSign(line))
return tokens
def GetString(lines):
'''
Get the strings from the diff code
:param lines: diff code.
:return: lineStr - All the diff lines.
lineStrB - The before-version code lines.
lineStrA - The after-version code lines.
'''
lineStr = ''
lineStrB = ''
lineStrA = ''
for hunk in lines:
for line in hunk:
# all lines.
lineStr += RemoveSign(line)
# all Before lines.
lineStrB += RemoveSign(line) if line[0] != '+' else ''
# all After lines.
lineStrA += RemoveSign(line) if line[0] != '-' else ''
return lineStr, lineStrB, lineStrA
def GetDiffTokens(lines):
'''
Get the tokens for the diff lines.
:param lines: the diff code.
:return: tokens - tokens ['tk', 'tk', ...] ('tk': string)
tokenTypes - token types [tkt, tkt, ...] (tkt: 1, 2, 3, 4, 5)
diffTypes - diff types [dft, dft, ...] (dft: -1, 0, 1)
'''
# initialize.
tokens = []
tokenTypes = []
diffTypes = []
# for each line of lines.
for hunk in lines:
for line in hunk:
#print(line, end='')
tk, tkT, dfT = GetClangTokens(line)
tokens.extend(tk)
tokenTypes.extend(tkT)
diffTypes.extend(dfT)
#print('-----------------------------------------------------------------------')
#print(tokens)
#print(tokenTypes)
#print(diffTypes)
return tokens, tokenTypes, diffTypes
#lines = data[0][1]
#print(lines)
#hunk = data[0][1][0]
#print(hunk)
#line = data[0][1][0][0]
#print(line)
# for each sample data[n].
numData = len(data)
props = []
for n in range(numData):
# get the lines of the diff file.
diffLines = data[n][1]
# properties.
tk, tkT, dfT = GetDiffTokens(diffLines)
label = data[n][2]
prop = [tk, tkT, dfT, label]
#print(prop)
props.append(prop)
print('[INFO] <GetDiffProps> Processing Tokens ' + str(n+1) + '/' + str(numData) + '...' + RunTime())
# save props.
if not os.path.exists(tempPath):
os.mkdir(tempPath)
np.save(tempPath + '/testprops.npy', props, allow_pickle=True)
print('[INFO] <GetDiffProps> Save ' + str(len(props)) + ' diff property data to ' + tempPath + '/testprops.npy.' + RunTime())
return props
def ProcessTokens(props, dType=1, cType=1):
'''
only maintain the diff parts of the code.
:param props: the features of diff code.
[[[tokens], [nums], [nums], 0/1], ...]
:param dType: 0 - maintain both diff code and context code.
1 - only maintain diff code.
:param cType: 0 - maintain both the code and comments.
1 - only maintain code and delete comments.
:return: props - the normalized features of diff code.
[[[tokens], [nums], [nums], 0/1], ...]
'''
# process diff code.
if (1 == dType):
propsNew = []
for item in props:
# the number of tokens.
numTokens = len(item[1])
# item[0]: tokens, item[1]: tokenTypes, item[2]: diffTypes, item[3]: label.
tokens = [item[0][n] for n in range(numTokens) if (item[2][n])]
tokenTypes = [item[1][n] for n in range(numTokens) if (item[2][n])]
diffTypes = [item[2][n] for n in range(numTokens) if (item[2][n])]
label = item[3]
# reconstruct sample.
sample = [tokens, tokenTypes, diffTypes, label]
propsNew.append(sample)
props = propsNew
print('[INFO] <ProcessTokens> Only maintain the diff parts of the code.' + RunTime())
# process comments.
if (1 == cType):
propsNew = []
for item in props:
# the number of tokens.
numTokens = len(item[1])
# item[0]: tokens, item[1]: tokenTypes, item[2]: diffTypes, item[3]: label.
tokens = [item[0][n] for n in range(numTokens) if (item[1][n] < 5)]
tokenTypes = [item[1][n] for n in range(numTokens) if (item[1][n] < 5)]
diffTypes = [item[2][n] for n in range(numTokens) if (item[1][n] < 5)]
label = item[3]
# reconstruct sample.
sample = [tokens, tokenTypes, diffTypes, label]
propsNew.append(sample)
props = propsNew
print('[INFO] <ProcessTokens> Delete the comment parts of the diff code.' + RunTime())
#print(props[0])
return props
def AbstractTokens(props, iType=1, lType=1):
'''
abstract the tokens of identifiers, literals, and comments.
:param props: the features of diff code.
[[[tokens], [nums], [nums], 0/1], ...]
:param iType: -1 - not abstract tokens.
0 - only abstract variable type and function type. VAR / FUNC
1 - abstract the identical variable names and function names. VAR0, VAR1, ... / FUNC0, FUNC1, ...
:param lType: -1 - not abstract tokens.
0 - abstract literals with LITERAL.
1 - abstract literals with LITERAL/n.
:return: props - the abstracted features of diff code.
[[[tokens], [nums], [nums], 0/1], ...]
'''
if (iType not in [0, 1]) or (lType not in [0, 1]):
print('[INFO] <AbstractTokens> Not abstract the tokens of identifiers, literals, and comments.' + RunTime())
return props
for item in props:
# get tokens and token types.
tokens = item[0]
tokenTypes = item[1]
numTokens = len(tokenTypes)
#print(tokens)
#print(tokenTypes)
#print(numTokens)
# abstract literals and comments, and separate identifiers into variables and functions.
markVar = list(np.zeros(numTokens, dtype=int))
markFuc = list(np.zeros(numTokens, dtype=int))
for n in range(numTokens):
# 2: IDENTIFIER, 3: LITERAL, 5: COMMENT
if 5 == tokenTypes[n]:
tokens[n] = 'COMMENT'
elif 3 == tokenTypes[n]:
if (0 == lType):
tokens[n] = 'LITERAL'
elif (1 == lType):
if (not tokens[n].isdigit()):
tokens[n] = 'LITERAL'
elif 2 == tokenTypes[n]:
# separate variable name and function name.
if (n < numTokens-1):
if (tokens[n+1] == '('):
markFuc[n] = 1
else:
markVar[n] = 1
else:
markVar[n] = 1
#print(tokens)
#print(markVar)
#print(markFuc)
# abstract variables and functions.
if (0 == iType):
for n in range(numTokens):
if 1 == markVar[n]:
tokens[n] = 'VAR'
elif 1 == markFuc[n]:
tokens[n] = 'FUNC'
elif (1 == iType):
# get variable dictionary.
varList = [tokens[idx] for idx, mark in enumerate(markVar) if mark == 1]
varVoc = {}.fromkeys(varList)
varVoc = list(varVoc.keys())
varDict = {tk: 'VAR' + str(idx) for idx, tk in enumerate(varVoc)}
# get function dictionary.
fucList = [tokens[idx] for idx, mark in enumerate(markFuc) if mark == 1]
fucVoc = {}.fromkeys(fucList)
fucVoc = list(fucVoc.keys())
fucDict = {tk: 'FUNC' + str(idx) for idx, tk in enumerate(fucVoc)}
#print(varDict)
#print(fucDict)
for n in range(numTokens):
if 1 == markVar[n]:
tokens[n] = varDict[tokens[n]]
elif 1 == markFuc[n]:
tokens[n] = fucDict[tokens[n]]
#print(tokens)
print('[INFO] <AbstractTokens> Abstract the tokens of identifiers with iType ' + str(iType), end='')
print(' (VAR/FUNC).' + RunTime()) if (0 == iType) else print(' (VARn/FUNCn).' + RunTime())
print('[INFO] <AbstractTokens> Abstract the tokens of literals, and comments with iType ' + str(lType), end='')
print(' (LITERAL/COMMENT).' + RunTime()) if (0 == lType) else print(' (LITERAL/n/COMMENT).' + RunTime())
return props
def GetDiffEmbed(tokenDict, embedSize):
'''
Get the pre-trained weights for embedding layer from the dictionary of diff vocabulary.
:param tokenDict: the dictionary of diff vocabulary.
{'tk': 0, 'tk': 1, ..., '<pad>': N}
:param embedSize: the dimension of the embedding vector.
:return: preWeights - the pre-trained weights for embedding layer.
[[n, ...], [n, ...], ...]
'''
# number of the vocabulary tokens.
numTokens = len(tokenDict)
# initialize the pre-trained weights for embedding layer.
preWeights = np.zeros((numTokens, embedSize))
for index in range(numTokens):
preWeights[index] = np.random.normal(size=(embedSize,))
print('[INFO] <GetDiffEmbed> Create pre-trained embedding weights with ' + str(len(preWeights)) + ' * ' + str(len(preWeights[0])) + ' matrix.' + RunTime())
return preWeights
def DivideBeforeAfter(diffProps):
# create temp folder.
if not os.path.exists(tempPath):
os.mkdir(tempPath)
# fp = open(tempPath + 'testtwinlen.csv', 'w')
twinProps = []
maxLen = 0
# for each sample in diffProps.
for item in diffProps:
# get the tk, tkT, dfT, lb.
tokens = item[0]
tokenTypes = item[1]
diffTypes = item[2]
label = item[3]
numTokens = len(diffTypes)
# reconstruct tkB, tkTB, tkA, tkTA.
tokensB = [tokens[i] for i in range(numTokens) if (diffTypes[i] <= 0)]
tokenTypesB = [tokenTypes[i] for i in range(numTokens) if (diffTypes[i] <= 0)]
tokensA = [tokens[i] for i in range(numTokens) if (diffTypes[i] >= 0)]
tokenTypesA = [tokenTypes[i] for i in range(numTokens) if (diffTypes[i] >= 0)]
# reconstruct new sample.
sample = [tokensB, tokenTypesB, tokensA, tokenTypesA, label]
twinProps.append(sample)
# get max length.
maxLenAB = max(len(tokenTypesB), len(tokenTypesA))
maxLen = maxLenAB if (maxLen < maxLenAB) else maxLen
# fp.write(str(len(tokenTypesB)) + '\n')
# fp.write(str(len(tokenTypesA)) + '\n')
# fp.close()
#print(twinProps[0])
#print(maxLen)
# print.
print('[INFO] <DivideBeforeAfter> Divide diff code into BEFORE-version and AFTER-version code.' + RunTime())
print('[INFO] <DivideBeforeAfter> The max length in BEFORE/AFTER-version code is ' + str(maxLen) + ' tokens. (hyperparameter: _TwinMaxLen_ = ' + str(_TwinMaxLen_) + ')' + RunTime())
return twinProps, maxLen
def GetTwinMapping(props, maxLen, tokenDict):
'''
Map the feature data into indexed data.
:param props: the features of diff code.
[[[tokens], [nums], [tokens], [nums], 0/1], ...]
:param maxLen: the max length of a twin code.
:param tokenDict: the dictionary of diff vocabulary.
{'tk': 1, 'tk': 2, ..., 'tk': N, '<pad>': 0}
:return: np.array(data) - feature data.
[[[n, {0~5}, n, {0~5}], ...], ...]
np.array(labels) - labels.
[[0/1], ...]
'''
def PadList(dList, pad, length):
'''
Pad the list data to a fixed length.
:param dList: the list data - [ , , ...]
:param pad: the variable used to pad.
:param length: the fixed length.
:return: dList - padded list data. [ , , ...]
'''
if len(dList) <= length:
dList.extend(pad for i in range(length - len(dList)))
elif len(dList) > length:
dList = dList[0:length]
return dList
# initialize the data and labels.
data = []
labels = []
# for each sample.
for item in props:
# initialize sample.
sample = []
# process tokensB.
tokens = item[0]
tokens = PadList(tokens, '<pad>', maxLen)
tokens2index = []
for tk in tokens:
if (tk in tokenDict.keys()):
tokens2index.append(tokenDict[tk])
else:
tokens2index.append(0)
sample.append(tokens2index)
# process tokenTypesB.
tokenTypes = item[1]
tokenTypes = PadList(tokenTypes, 0, maxLen)
sample.append(tokenTypes)
# process tokensA.
tokens = item[2]
tokens = PadList(tokens, '<pad>', maxLen)
tokens2index = []
for tk in tokens:
if (tk in tokenDict.keys()):
tokens2index.append(tokenDict[tk])
else:
tokens2index.append(0)
sample.append(tokens2index)
# process tokenTypesA.
tokenTypes = item[3]
tokenTypes = PadList(tokenTypes, 0, maxLen)
sample.append(tokenTypes)
# process sample.
sample = np.array(sample).T
data.append(sample)
# process label.
label = item[4]
labels.append([label])
if _DEBUG_:
print('[DEBUG] data:')
print(data[0:3])
print('[DEBUG] labels:')
print(labels[0:3])
# print.
print('[INFO] <GetTwinMapping> Create ' + str(len(data)) + ' feature data with ' + str(len(data[0])) + ' * ' + str(len(data[0][0])) + ' matrix.' + RunTime())
print('[INFO] <GetTwinMapping> Create ' + str(len(labels)) + ' labels with 1 * 1 matrix.' + RunTime())
# save files.
# if (not os.path.exists(tempPath + '/tdata_' + str(maxLen) + '.npy')) \
# | (not os.path.exists(tempPath + '/tlabels_' + str(maxLen) + '.npy')):
# np.save(tempPath + '/tdata_' + str(maxLen) + '.npy', data, allow_pickle=True)
# print('[INFO] <GetTwinMapping> Save the mapped numpy data to ' + tempPath + '/tdata_' + str(maxLen) + '.npy.')
# np.save(tempPath + '/tlabels_' + str(maxLen) + '.npy', labels, allow_pickle=True)
# print('[INFO] <GetTwinMapping> Save the mapped numpy labels to ' + tempPath + '/tlabels_' + str(maxLen) + '.npy.')
return np.array(data), np.array(labels)
def UpdateTwinTokenTypes(data):
'''
Update the token type in the feature data into one-hot vector.
:param data: feature data. [[[n, {0~5}, n, {0~5},], ...], ...]
:return: np.array(newData). [[[n, 0/1, 0/1, 0/1, 0/1, 0/1, n, 0/1, 0/1, 0/1, 0/1, 0/1], ...], ...]
'''
newData = []
# for each sample.
for item in data:
# get the transpose of props.
itemT = item.T
# initialize new sample.
newItem = []
newItem.append(itemT[0])
newItem.extend(np.zeros((5, len(item)), dtype=int))
newItem.append(itemT[2])
newItem.extend(np.zeros((5, len(item)), dtype=int))
# assign the new sample.
for i in range(len(item)):
tokenType = itemT[1][i]
if (tokenType):
newItem[tokenType][i] = 1
tokenType = itemT[3][i]
if (tokenType):
newItem[tokenType+6][i] = 1
# get the transpose of new sample.
newItem = np.array(newItem).T
# append new sample.
newData.append(newItem)
if _DEBUG_:
print('[DEBUG] newData:')
print(newData[0:3])
# print.
print('[INFO] <UpdateTwinTokenTypes> Update ' + str(len(newData)) + ' feature data with ' + str(len(newData[0])) + ' * ' + str(len(newData[0][0])) + ' matrix.' + RunTime())
# save files.
# if (not os.path.exists(tempPath + '/newtdata_' + str(len(newData[0])) + '.npy')):
# np.save(tempPath + '/newtdata_' + str(len(newData[0])) + '.npy', newData, allow_pickle=True)
# print('[INFO] <UpdateTwinTokenTypes> Save the mapped numpy data to ' + tempPath + '/newtdata_' + str(len(newData[0])) + '.npy.')
# change marco.
global _TwinExtraDim_
_TwinExtraDim_ = 5
return np.array(newData)
def GetCommitMsgs(data):
'''
Get the commit messages in diff files.
:param data: [[[line, , ], [[line, , ], [line, , ], ...], 0/1], ...]
:return: msgs - [[[tokens], 0/1], ...]
'''
def GetMsgTokens(lines):
'''
Get the tokens from a commit message.
:param lines: commit message. [line, , ]
:return: tokensStem ['tk', , ]
'''
# concatenate lines.
# get the string of commit message.
msg = ''
for line in lines:
msg += line[:-1] + ' '
#print(msg)
# pre-process.
# remove url.
pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
msg = re.sub(pattern, ' ', msg)
# remove independent numbers.
pattern = r' \d+ '
msg = re.sub(pattern, ' ', msg)
# lower case capitalized words.
pattern = r'([A-Z][a-z]+)'
def LowerFunc(matched):
return matched.group(1).lower()
msg = re.sub(pattern, LowerFunc, msg)
# remove footnote.
patterns = ['signed-off-by:', 'reported-by:', 'reviewed-by:', 'acked-by:', 'found-by:', 'tested-by:', 'cc:']
for pattern in patterns:
index = msg.find(pattern)
if (index > 0):
msg = msg[:index]
#print(msg)
# clearance.
# get the tokens.
tknzr = TweetTokenizer()
tokens = tknzr.tokenize(msg)
# clear tokens that don't contain any english letter.
for i in reversed(range(len(tokens))):
if not (re.search('[a-z]', tokens[i])):
tokens.pop(i)
# clear tokens that are stopwords.
for i in reversed(range(len(tokens))):
if (tokens[i] in stopwords.words('english')):
tokens.pop(i)
pattern = re.compile("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)")
for i in reversed(range(len(tokens))):
if (pattern.findall(tokens[i])):
tokens.pop(i)
#print(tokens)
# process tokens with stemming.
porter = PorterStemmer()
tokensStem = []
for item in tokens:
tokensStem.append(porter.stem(item))
#print(tokensStem)
return tokensStem
# for each sample data[n].
numData = len(data)
msgs = []
for n in range(numData):
# get the lines of the commit message.
commitMsg = data[n][0]
mtk = GetMsgTokens(commitMsg)
# get the label.
label = data[n][2]
#print([mtk, label])
# append the message tokens.
msgs.append([mtk, label])
print('[INFO] <GetCommitMsg> Processing Commit ' + str(n+1) + '/' + str(numData) + '...' + RunTime())
# save commit messages.
if not os.path.exists(tempPath):
os.mkdir(tempPath)
np.save(tempPath + '/testmsgs.npy', msgs, allow_pickle=True)
print('[INFO] <GetCommitMsg> Save ' + str(len(msgs)) + ' commit messages to ' + tempPath + '/testmsgs.npy.' + RunTime())
return msgs
def GetMsgEmbed(tokenDict, embedSize):
'''
Get the pre-trained weights for embedding layer from the dictionary of msg vocabulary.
:param tokenDict: the dictionary of msg vocabulary.
{'tk': 0, 'tk': 1, ..., '<pad>': N}
:param embedSize: the dimension of the embedding vector.
:return: preWeights - the pre-trained weights for embedding layer.
[[n, ...], [n, ...], ...]
'''
# number of the vocabulary tokens.
numTokens = len(tokenDict)
# initialize the pre-trained weights for embedding layer.
preWeights = np.zeros((numTokens, embedSize))
for index in range(numTokens):
preWeights[index] = np.random.normal(size=(embedSize,))
print('[INFO] <GetMsgEmbed> Create pre-trained embedding weights with ' + str(len(preWeights)) + ' * ' + str(len(preWeights[0])) + ' matrix.' + RunTime())
return preWeights
def GetMsgMapping(msgs, maxLen, tokenDict):
'''
Map the feature data into indexed data.
:param props: the features of commit messages.
[[[tokens], 0/1], ...]
:param maxLen: the max length of the commit message.
:param tokenDict: the dictionary of commit message vocabulary.
{'tk': 1, 'tk': 2, ..., 'tk': N, '<pad>': 0}
:return: np.array(data) - feature data.
[[n, ...], ...]
np.array(labels) - labels.
[[0/1], ...]
'''
def PadList(dList, pad, length):
'''
Pad the list data to a fixed length.
:param dList: the list data - [ , , ...]
:param pad: the variable used to pad.
:param length: the fixed length.
:return: dList - padded list data. [ , , ...]
'''
if len(dList) <= length:
dList.extend(pad for i in range(length - len(dList)))
elif len(dList) > length:
dList = dList[0:length]
return dList
# initialize the data and labels.
data = []
labels = []
# for each sample.
for item in msgs:
# process tokens.
tokens = item[0]
tokens = PadList(tokens, '<pad>', maxLen)
# convert tokens into numbers.
tokens2index = []
for tk in tokens:
if (tk in tokenDict.keys()):
tokens2index.append(tokenDict[tk])
else:
tokens2index.append(0)
data.append(tokens2index)
# process label.
label = item[1]
labels.append([label])
if _DEBUG_:
print('[DEBUG] data:')
print(data[0:3])
print('[DEBUG] labels:')
print(labels[0:3])
# print.
print('[INFO] <GetMsgMapping> Create ' + str(len(data)) + ' feature data with 1 * ' + str(len(data[0])) + ' vector.' + RunTime())
print('[INFO] <GetMsgMapping> Create ' + str(len(labels)) + ' labels with 1 * 1 matrix.' + RunTime())
# # save files.
# if (not os.path.exists(tempPath + '/mdata_' + str(maxLen) + '.npy')) \
# | (not os.path.exists(tempPath + '/mlabels_' + str(maxLen) + '.npy')):
# np.save(tempPath + '/mdata_' + str(maxLen) + '.npy', data, allow_pickle=True)
# print('[INFO] <GetMsgMapping> Save the mapped numpy data to ' + tempPath + '/mdata_' + str(maxLen) + '.npy.')
# np.save(tempPath + '/mlabels_' + str(maxLen) + '.npy', labels, allow_pickle=True)
# print('[INFO] <GetMsgMapping> Save the mapped numpy labels to ' + tempPath + '/mlabels_' + str(maxLen) + '.npy.')
return np.array(data), np.array(labels)
def CombineTwinMsgs(props, msgs, plabels, mlabels):
'''
Combine the twin props with the commit messages.
:param props: twin data. [[[n, {0~5}, n, {0~5}], ...], ...] or [[[n, 0/1, 0/1, 0/1, 0/1, 0/1, n, 0/1, 0/1, 0/1, 0/1, 0/1], ...], ...]
:param msgs: message data. [[n, ...], ...]
:param plabels: twin labels. [[0/1], ...]
:param mlabels: message labels. [[0/1], ...]
:return: np.array(data) - combined data. [[[n, 0/1, 0/1, 0/1, 0/1, 0/1, n, 0/1, 0/1, 0/1, 0/1, 0/1, n], ...], ...]
np.array(plabels) - combined labels. [[0/1], ...]
'''
# check the lengths.
if (len(plabels) != len(mlabels)):
print('[ERROR] <CombineTwinMsgs> the data lengths are mismatch.')
return [[]], [[]]
# check the labels.
cntMatch = 0
for n in range(len(plabels)):
if (plabels[n][0] == mlabels[n][0]):
cntMatch += 1
if (cntMatch != len(plabels)):
print('[ERROR] <CombineTwinMsgs> the labels are mismatch. ' + str(cntMatch) + '/' + str(len(plabels)) + '.')
return [[]], [[]]
#print(props[1], len(props[1]))
#print(msgs[1], len(msgs[1]))
data = []
for n in range(len(plabels)):
# get the twin prop and message.
prop = props[n]
msg = msgs[n]
# pad data.
if (_TwinMaxLen_ >= _MsgMaxLen_):
msg = np.pad(msg, (0, _TwinMaxLen_ - _MsgMaxLen_), 'constant')
else:
prop = np.pad(prop, ((0, _MsgMaxLen_ - _TwinMaxLen_), (0, 0)), 'constant')
#print(msg, len(msg))
#print(prop, len(prop))
# reconstruct sample.
sample = np.vstack((prop.T, msg))
# append the sample to data.
data.append(sample.T)
if _DEBUG_:
print(np.array(data[0:3]))
print('[INFO] <CombineTwinMsgs> Combine the twin props with the commit messages.' + RunTime())
return np.array(data), np.array(plabels)
class TwinRNN(nn.Module):
'''
TwinRNN : convert a patch data into a predicted label.
'''
def __init__(self, preWTwin, preWMsg, hidSizTwin=32, hidSizMsg=32, hidLayTwin=1, hidLayMsg=1):
'''
define each layer in the network model.
:param preWTwin: tensor pre-trained weights for embedding layer for twin.
:param preWMsg: tensor pre-trained weights for embedding layer for msg.
:param hidSizTwin: node number in the hidden layer for twin.
:param hidSizMsg: node number in the hidden layer for msg.
:param hidLayTwin: number of hidden layer for twin.
:param hidLayMsg: number of hidden layer for msg.
'''
super(TwinRNN, self).__init__()
# parameters.
class_num = 2
# twin.
vSizTwin, emDimTwin = preWTwin.size()
# Embedding Layer for twin.
self.embedTwin = nn.Embedding(num_embeddings=vSizTwin, embedding_dim=emDimTwin)
self.embedTwin.load_state_dict({'weight': preWTwin})