-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
156 lines (139 loc) · 5.62 KB
/
preprocess.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
"""
Preprocessing script for elastic data.
"""
import os
import glob
from utils.utils import preproces
from pandas import read_csv
from numpy.random import permutation
import json
DATAPATH = "elastic/"
DATAPATH2 = "json_data/data.json"
def make_dirs(dirs):
for d in dirs:
if not os.path.exists(d):
os.makedirs(d)
def build_vocab(filepaths, dst_path, lowercase=True):
vocab = set()
for filepath in filepaths:
with open(filepath) as f:
for line in f:
if lowercase:
line = line.lower()
vocab |= set(line.split())
with open(dst_path, 'w') as f:
for w in sorted(vocab):
f.write(w + '\n')
def splitdata():
train_data = read_csv('metadata.csv', index_col=[0])
train_path = list(train_data['new_path'])
train_label = [1 if i > 0 else 0 for i in train_data['Number of bugs']]
train_hasbug, train_nothave = [], []
print("split data.........................")
for i in range(len(train_label)):
if train_label[i] == 1:
train_hasbug.append((train_path[i], train_label[i]))
else:
train_nothave.append((train_path[i], train_label[i]))
print("split train test .......................")
print(len(train_hasbug), len(train_nothave))
indices_hasbug = permutation(1000)
train_data = [train_hasbug[i] for i in indices_hasbug]
# train_data = [hasbug[i] for i in indices_hasbug[:int(len(indices_hasbug)*0.8)]]
# test_data = [hasbug[i] for i in indices_hasbug[int(len(indices_hasbug)*0.8):]]
indices_nothas = permutation(1000 + 1000)
train_data.extend([train_nothave[i] for i in indices_nothas])
# train_data.extend([nothave[i] for i in indices_nothas[:int(len(indices_nothas)*0.8)]])
# test_data.extend([nothave[i] for i in indices_nothas[int(len(indices_nothas)*0.8):]])
# train_data.extend([nothave[i] for i in indices_hasbug[:int(len(indices_hasbug)*0.8)]])
# test_data.extend([nothave[i] for i in indices_hasbug[int(len(indices_hasbug)*0.8):]])
train_data = [train_data[i] for i in permutation(len(train_data))]
test_data = read_csv('test_metadata.csv', index_col=[0])
test_path = list(test_data['new_path'])
test_label = [1 if i > 0 else 0 for i in test_data['bug']]
test_hasbug, test_nothave = [], []
print("split data.........................")
for i in range(len(test_label)):
if test_label[i] == 1:
test_hasbug.append((test_path[i], test_label[i]))
else:
test_nothave.append((test_path[i], test_label[i]))
print("split train test .......................")
print(len(test_hasbug), len(test_nothave))
test_data = [(test_path[i], test_label[i]) for i in permutation(len(test_data))]
# if not os.path.exists("train"):
# os.mkdir("train")
# if not os.path.exists("test"):
# os.mkdir("test")
vocab = []
print("make ast data.....................")
with open('train.csv', 'w') as file:
file.write("label|data\n")
for i in train_data:
exported = preproces(i[0], vocab, 'train')
if exported == '':
continue
file.write(str(i[1]) + '|' + exported + "\n")
with open('test.csv', 'w') as file:
file.write("label|data\n")
for i in test_data:
exported = preproces(i[0], vocab, 'train')
if exported == '':
continue
file.write(str(i[1]) + '|' + exported + "\n")
vocab = set(vocab)
with open('vocab', 'w') as file:
for i in vocab:
file.write(i + "\n")
def splitdata_2():
with open(DATAPATH2) as file:
data = json.loads(file.read())
hasbug, nothave = [], []
print("split data.........................")
for i in range(len(data["label"])):
if data["label"][i] == 1:
hasbug.append((data["bug"][i], data["method"][i], data["label"][i]))
else:
nothave.append((data["bug"][i], data["method"][i], data["label"][i]))
print("split train .......................")
print(len(hasbug), len(nothave))
indices_hasbug = permutation(1000)
train_data = [hasbug[i] for i in indices_hasbug[:int(len(indices_hasbug)*0.8)]]
test_data = [hasbug[i] for i in indices_hasbug[int(len(indices_hasbug)*0.8):]]
indices_nothas = permutation(1000 + 1000)
train_data.extend([nothave[i] for i in indices_nothas[:int(len(indices_nothas)*0.8)]])
test_data.extend([nothave[i] for i in indices_nothas[int(len(indices_nothas)*0.8):]])
train_data = [train_data[i] for i in permutation(len(train_data))]
test_data = [test_data[i] for i in permutation(len(test_data))]
print(len(train_data), len(test_data))
vocab = []
print("make ast data.....................")
with open('train_owasp.json', 'w') as file:
train_json = {
"bug": [],
"method": [],
"label": []
}
for i in train_data:
train_json['bug'].append(i[0])
train_json['method'].append(i[1])
train_json['label'].append(i[2])
file.write(json.dumps(train_json))
with open('test_owasp.json', 'w') as file:
test_json = {
"bug": [],
"method": [],
"label": []
}
for i in test_data:
test_json['bug'].append(i[0])
test_json['method'].append(i[1])
test_json['label'].append(i[2])
file.write(json.dumps(test_json))
if __name__ == '__main__':
print('=' * 80)
print('Preprocessing elastic dataset')
print('=' * 80)
# splitdata()
splitdata_2()
print("done!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")