-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_utils.py
152 lines (114 loc) · 4.28 KB
/
cluster_utils.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
import pandas as pd
import numpy as np
import re
import os
import pickle
import sys
from collections import Counter
from collections import defaultdict
try:
import ConfigParser
except ImportError:
import configparser as ConfigParser
def get_options():
cf = ConfigParser.ConfigParser()
if os.path.exists("configurationfile.cof"):
cf.read('config.cof')
else:
print("there is no configurationfile.cof!")
exit()
option_dict = dict()
for key, value in cf.items("CLUSTER"):
option_dict[key] = eval(value)
return option_dict
option_dict = get_options()
filename = option_dict['filename']
clusters = option_dict['clusters']
maxx = option_dict['max']
minn = option_dict['min']
chars = option_dict['chars']
def save_object(name, object):
with open("{}_{}_cache/{}.pkl".format(filename, clusters, name), "wb") as f:
pickle.dump(object, f)
def read_object(filename):
with open(filename, "rb") as f:
object = pickle.load(f)
return object
def save_txt(name, object):
with open("{}_{}_cache/{}.txt".format(filename, clusters, name), "w") as f:
for item in object:
f.write(str(item))
f.write("\n")
def clear_log(line, chars):
pattern_one = re.compile("[0-9]+")
pattern_two = re.compile("|".join(chars))
tmp_line = re.sub(pattern_one, "0", line)
clearned_line = re.sub(pattern_two, "_", tmp_line).strip(" ").strip("_")
return clearned_line
def load_log(filename,chars):
documents = []
count = 0
import pandas
data = [line.strip() for line in open(filename, "r").readlines()]
data = [{"LineNumber": line.split('~')[0], "Text": line.split('~')[1]} for line in data]
data = pandas.DataFrame(data)
for i in range(data.shape[0]):
line=data['Text'][i]
cleared_line = clear_log(line, chars)
documents.append(cleared_line)
print(len(documents))
return documents
def create_sub_files(database):
if not os.path.exists("{}_{}_cache/cluster_files".format(filename, clusters)):
os.mkdir("{}_{}_cache/cluster_files".format(filename, clusters))
for key, value in database.items():
with open("{}_{}_cache/cluster_files/cluster_{}.log".format(filename, clusters, key), "w") as f:
for log in value:
f.write(log)
def create_log_tag(labels, filename):
label_dict = Counter(labels)
labels_info = sorted(label_dict.items(), key=lambda item: item[1])
database = defaultdict(list)
index = 0
with open(filename, mode="r", errors="replace") as f:
while True:
line = f.readline()
if not line:
break
database[labels[index]].append(line)
index += 1
#save_object("database", database)
create_sub_files(database)
return database, labels_info
def create_view(labels_info, database):
num = 5
with open("{}_{}_cache/result.txt".format(filename, clusters), "w") as f:
f.write("log information: \n\n")
for item in labels_info:
f.write("category {}, number: {}\n".format(item[0], item[1]))
f.write("\n----------------------------------------\n")
f.write("\nthe example logs are: \n")
for category in labels_info:
f.write("\ncategory {}\n".format(category[0]))
if num <= len(database[category[0]]):
for log in database[category[0]][0:num]:
f.write(log)
else:
for log in database[category[0]]:
f.write(log)
def create_view_test(labels_info, database):
num = 5
with open("{}_{}_cache/result_test.txt".format(filename, clusters), "w") as f:
f.write("log information: \n\n")
for item in labels_info:
f.write("category {}, number: {}\n".format(item[0], item[1]))
f.write("\n----------------------------------------\n")
f.write("\nthe example logs are: \n")
for category in labels_info:
f.write("\ncategory {}\n".format(category[0]))
if num <= len(database[category[0]]):
for log in database[category[0]][0:num]:
f.write(log)
else:
for log in database[category[0]]:
f.write(log)