-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclassifier.py
284 lines (249 loc) · 10.6 KB
/
classifier.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
import os
import numpy as np
import pandas as pd
from gensim.models import word2vec
from sklearn.manifold import TSNE
from sklearn.cluster import DBSCAN
source_file = 'data/source_data/responsive-addresses.work'
source_data = 'data/source_data/responsive-addresses.data'
rfc_profile = 'data/save_data/rfc_profile.txt'
ec_profile = 'data/save_data/ec_profile.txt'
ec_cluster = 'data/save_data/ec_cluster.txt'
ipv62vec_profile = 'data/save_data/ipv62vec_profile.txt'
category_path = 'data/category_data/'
rfc_data_path = category_path + 'rfc/data/'
ec_data_path = category_path + 'ec/data/'
ipv62vec_data_path = category_path + 'ipv62vec/data/'
rfc_id_path = category_path + 'rfc/id/'
ec_id_path = category_path + 'ec/id/'
ipv62vec_id_path = category_path + 'ipv62vec/id/'
class RFCBased(object):
def __init__(self, batch_size):
self.classify_dict = {}
self.cmd = 'cat data/source_data/responsive-addresses.work | ../../Tools/ipv6toolkit/addr6 -i -d > data/save_data/rfc_profile.txt'
self.rfc_profile = rfc_profile
self.source_data = source_data
self.rfc_data_path = rfc_data_path
self.rfc_id_path = rfc_id_path
self.batch_size = batch_size
self.emb_data_num = []
def create_category(self):
os.system(self.cmd)
f = open(self.rfc_profile, 'r')
g = open(self.source_data, 'r')
i = 0
for line, data in zip(f, g):
label = line.split('=')[3]
if label not in self.classify_dict.keys():
self.classify_dict[label] = []
self.classify_dict[label].append(data)
i += 1
g.close()
f.close()
for type in self.classify_dict.keys():
f = open(self.rfc_data_path + type + '.txt', 'w')
f.writelines(self.classify_dict[type])
f.close()
def gen_id_file(self, vocab_dict):
emb_file_list = []
for filename in sorted(os.listdir(self.rfc_data_path)):
emb_id_file = self.rfc_id_path + filename[:-3] + 'id'
emb_data_file = self.rfc_data_path + filename
data_num = self.gen_id_data(emb_id_file, emb_data_file, vocab_dict)
if data_num >= self.batch_size:
emb_file_list.append(emb_id_file)
else:
print('Unload %s, data num %s < batch size' % (emb_id_file, data_num))
return emb_file_list
def gen_id_data(self, emb_id_file, emb_data_file, vocab_dict):
conjunction = ' '
g = open(emb_id_file, 'w')
f = open(emb_data_file, 'r')
data_num = 0
for data in f:
id_data = [str(vocab_dict[i]) for i in data[:-1]]
g.write(conjunction.join(id_data) + '\n')
data_num += 1
if data_num >= self.batch_size:
self.emb_data_num.append(data_num)
f.close()
g.close()
return data_num
class EntropyClustering(object):
def __init__(self, batch_size, k=4):
self.classify_dict = {}
self.classify_prefix_dict = {}
self.k = k
self.profile_cmd = 'cat data/source_data/responsive-addresses.data | ' + \
'../../Tools/entropy-clustering/profiles > data/save_data/ec_profile.txt'
self.cluster_cmd = 'cat data/save_data/ec_profile.txt | ' + \
'../../Tools/entropy-clustering/clusters -kmeans -k ' \
+ str(k) + ' > data/save_data/ec_cluster.txt'
self.ec_profile = ec_profile
self.ec_cluster = ec_cluster
self.source_data = source_data
self.ec_data_path = ec_data_path
self.ec_id_path = ec_id_path
self.batch_size = batch_size
self.emb_data_num = []
def create_category(self):
for filename in os.listdir(self.ec_data_path):
os.remove(self.ec_data_path + filename)
for filename in os.listdir(self.ec_id_path):
os.remove(self.ec_id_path + filename)
# os.system(self.profile_cmd)
# os.system(self.cluster_cmd)
for i in range(self.k):
self.classify_dict[str(i)] = []
self.classify_prefix_dict[str(i)] = []
type_pointer = 0
class_prefix = []
f = open(self.ec_cluster, 'r')
for line in f:
if line[0] == '=':
class_prefix = []
elif line[0] == '\n':
self.classify_prefix_dict[str(type_pointer)].extend(class_prefix)
type_pointer += 1
elif line[:7] == 'SUMMARY':
break
else:
class_prefix.append(line[:8])
f.close()
print(self.classify_prefix_dict['3'])
for type in self.classify_prefix_dict.keys():
for prefix in self.classify_prefix_dict[type]:
f = open(self.source_data, 'r')
for line in f:
if prefix == line[:8]:
self.classify_dict[type].append(line)
f.close()
for type in self.classify_dict.keys():
f = open(self.ec_data_path + 'cluster_' + type + '.txt', 'w')
f.writelines(self.classify_dict[type])
f.close()
def gen_id_file(self, vocab_dict):
emb_file_list = []
for filename in sorted(os.listdir(self.ec_data_path)):
emb_id_file = self.ec_id_path + filename[:-3] + 'id'
emb_data_file = self.ec_data_path + filename
data_num = self.gen_id_data(emb_id_file, emb_data_file, vocab_dict)
if data_num >= self.batch_size:
emb_file_list.append(emb_id_file)
else:
print('Unload %s, data num %s < batch size' % (emb_id_file, data_num))
return emb_file_list
def gen_id_data(self, emb_id_file, emb_data_file, vocab_dict):
conjunction = ' '
g = open(emb_id_file, 'w')
f = open(emb_data_file, 'r')
data_num = 0
for data in f:
id_data = [str(vocab_dict[i]) for i in data[:-1]]
g.write(conjunction.join(id_data) + '\n')
data_num += 1
if data_num >= self.batch_size:
self.emb_data_num.append(data_num)
f.close()
g.close()
return data_num
class IPv62Vec(object):
def __init__(self, batch_size):
self.source_data = source_data
self.ipv62vec_profile = ipv62vec_profile
self.ipv62vec_data_path = ipv62vec_data_path
self.ipv62vec_id_path = ipv62vec_id_path
self.batch_size = batch_size
self.emb_data_num = []
def create_category(self):
for filename in os.listdir(self.ipv62vec_data_path):
os.remove(self.ipv62vec_data_path + filename)
for filename in os.listdir(self.ipv62vec_id_path):
os.remove(self.ipv62vec_id_path + filename)
f = open(self.source_data, 'r')
raw_data = f.readlines()
f.close()
index_alpha = '0123456789abcdefghijklmnopqrstuv'
address_sentences = []
for address in raw_data:
address_words = []
for nybble, index in zip(address[:-1], index_alpha):
address_words.append(nybble + index)
address_sentences.append(" ".join(address_words) + '\n')
f = open(self.ipv62vec_profile, 'w')
f.writelines(address_sentences)
f.close()
sentences = word2vec.LineSentence(self.ipv62vec_profile)
model = word2vec.Word2Vec(sentences, alpha=0.025, min_count=0, size=100, window=5,
sg=0, hs=0, negative=5, ns_exponent=0.75, iter=5)
vocab = list(model.wv.vocab.keys())
X_tsne = TSNE(n_components=2, learning_rate=200, perplexity=30).fit_transform(model.wv[vocab])
address_split_sentences = [sentence[:-1].split(' ') for sentence in address_sentences]
x = []
y = []
for address_split_sentence in address_split_sentences:
x_one_sample = []
y_one_sample = []
for word in address_split_sentence:
x_one_sample.append(X_tsne[vocab.index(word), 0])
y_one_sample.append(X_tsne[vocab.index(word), 1])
x.append(np.mean(x_one_sample))
y.append(np.mean(y_one_sample))
dim_reduced_data = []
for i, j in zip(x, y):
dim_reduced_data.append([i, j])
dim_reduced_data = pd.DataFrame(dim_reduced_data)
dim_reduced_data.columns = ['x', 'y']
data = self.cluster(dim_reduced_data)
self.search_cluster(data, raw_data)
def cluster(self, data):
db = DBSCAN(eps=0.0085, min_samples=self.batch_size).fit(data)
data['labels'] = db.labels_
n_clusters_ = 0
try:
n_clusters_ = len(set(db.labels_)) - (1 if -1 in db.labels_ else 0)
if n_clusters_ > 10:
raise ClassNumError(n_clusters_)
except ClassNumError as e:
print('ClassNumError: generated class num %s > 10, please reset IPv62Vec parameters.' % e.n_clusters)
os._exit(0)
print('cluster num', n_clusters_)
return data
def search_cluster(self, data, raw_data):
index_list = []
labels = set(data['labels'])
for label in labels:
index_list.append(data[data['labels'] == label].index)
for index, label in zip(index_list, labels):
f = open(self.ipv62vec_data_path + 'cluster_' + str(label) + '.txt', 'w')
for i in index:
f.write(raw_data[i])
f.close()
def gen_id_file(self, vocab_dict):
emb_file_list = []
for filename in sorted(os.listdir(self.ipv62vec_data_path)):
emb_id_file = self.ipv62vec_id_path + filename[:-3] + 'id'
emb_data_file = self.ipv62vec_data_path + filename
data_num = self.gen_id_data(emb_id_file, emb_data_file, vocab_dict)
if data_num >= self.batch_size:
emb_file_list.append(emb_id_file)
else:
print('Unload %s, data num %s < batch size' % (emb_id_file, data_num))
return emb_file_list
def gen_id_data(self, emb_id_file, emb_data_file, vocab_dict):
conjunction = ' '
g = open(emb_id_file, 'w')
f = open(emb_data_file, 'r')
data_num = 0
for data in f:
id_data = [str(vocab_dict[i]) for i in data[:-1]]
g.write(conjunction.join(id_data) + '\n')
data_num += 1
if data_num >= self.batch_size:
self.emb_data_num.append(data_num)
f.close()
g.close()
return data_num
class ClassNumError(Exception):
def __init__(self, n_clusters):
self.n_clusters = n_clusters