-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_correction.py
More file actions
191 lines (158 loc) · 6.11 KB
/
template_correction.py
File metadata and controls
191 lines (158 loc) · 6.11 KB
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
import pandas as pd
import re
import os
input_dir = './full_dataset/'
datasets = [
"Proxifier",
"Linux",
"Apache",
"Zookeeper",
"Hadoop",
"HealthApp",
"OpenStack",
"HPC",
"Mac",
"OpenSSH",
"Spark",
"Thunderbird",
"BGL",
"HDFS",
]
# Ground truth correction function from LUNAR
def correct_single_template(template, user_strings=None):
"""Apply all rules to process a template.
DS (Double Space)
BL (Boolean) # we don't use this
US (User String) # we don't use this
DG (Digit)
HEX (Hex Variables)
PS (Path-like String) # we don't use this
WV (Word concatenated with Variable)
DV (Dot-separated Variables)
CV (Consecutive Variables)
"""
# boolean = {}
# default_strings = {}
path_delimiters = { # reduced set of delimiters for tokenizing for checking the path-like strings
r'\s', r'\,', r'\!', r'\;', r'\:',
r'\=', r'\|', r'\"', r'\'',
r'\[', r'\]', r'\(', r'\)', r'\{', r'\}'
}
token_delimiters = path_delimiters.union({ # all delimiters for tokenizing the remaining rules
r'\.', r'\-', r'\+', r'\@', r'\#', r'\$', r'\%', r'\&',
})
# if user_strings:
# default_strings = default_strings.union(user_strings)
# apply DS
template = template.strip()
template = re.sub(r'\s+', ' ', template)
# apply PS
# p_tokens = re.split('(' + '|'.join(path_delimiters) + ')', template)
# new_p_tokens = []
# for p_token in p_tokens:
# if re.match(r'^(\/[^\/]+)+$', p_token):
# p_token = '<*>'
# new_p_tokens.append(p_token)
# template = ''.join(new_p_tokens)
# tokenize for the remaining rules
tokens = re.split('(' + '|'.join(token_delimiters) + ')', template) # tokenizing while keeping delimiters
new_tokens = []
for token in tokens:
# apply BL, US
# for to_replace in boolean.union(default_strings):
# if token.lower() == to_replace.lower():
# token = '<*>'
# apply DG
if re.match(r'^\d+$', token):
token = '<*>'
# newly added by me
# apply Hex
if re.match(r'0x[0-9a-fA-F]+', token):
token = '<*>'
while "0x<*>" in token:
token = token.replace("0x<*>", "<*>")
# apply WV
if re.match(r'^[^\s\/]*<\*>[^\s\/]*$', token):
if token != '<*>/<*>': # need to check this because `/` is not a deliminator
token = '<*>'
# collect the result
new_tokens.append(token)
# make the template using new_tokens
template = ''.join(new_tokens)
# Substitute consecutive variables only if separated with any delimiter including "." (DV)
while True:
prev = template
template = re.sub(r'<\*>\.<\*>', '<*>', template)
if prev == template:
break
# Substitute consecutive variables only if not separated with any delimiter including space (CV)
# NOTE: this should be done at the end
#print("CV: ", template)
while True:
prev = template
template = re.sub(r'<\*><\*>', '<*>', template)
if prev == template:
break
#print("CV: ", template)
while " #<*># " in template:
template = template.replace(" #<*># ", " <*> ")
while " #<*> " in template:
template = template.replace(" #<*> ", " <*> ")
while "<*>:<*>" in template:
template = template.replace("<*>:<*>", "<*>")
while "<*>#<*>" in template:
template = template.replace("<*>#<*>", "<*>")
while "<*>/<*>" in template:
template = template.replace("<*>/<*>", "<*>")
while "<*>@<*>" in template:
template = template.replace("<*>@<*>", "<*>")
while "<*>.<*>" in template:
template = template.replace("<*>.<*>", "<*>")
while ' "<*>" ' in template:
template = template.replace(' "<*>" ', ' <*> ')
while " '<*>' " in template:
template = template.replace(" '<*>' ", " <*> ")
while "<*><*>" in template:
template = template.replace("<*><*>", "<*>")
# newly added by me
while " <*>. " in template:
template = template.replace(" <*>. ", " <*> ")
while " <*>, " in template:
template = template.replace(" <*>, ", " <*> ")
while "<*>+<*>" in template:
template = template.replace("<*>+<*>", "<*>")
while "<*>##<*>" in template:
template = template.replace("<*>##<*>", "<*>")
while "#<*>#" in template:
template = template.replace("#<*>#", "<*>")
while "<*>-<*>" in template:
template = template.replace("<*>-<*>", "<*>")
while " <*> <*> " in template:
template = template.replace(" <*> <*> ", " <*> ")
while template.endswith(" <*> <*>"):
template = template[:-8] + " <*>"
while template.startswith("<*> <*> "):
template = "<*> " + template[8:]
# newly added by me
while "<*>,<*>" in template:
template = template.replace("<*>,<*>", "<*>")
while "(<*> <*>)" in template:
template = template.replace("(<*> <*>)", "(<*>)")
while " /<*> " in template:
template = template.replace(" /<*> ", " <*> ")
if template.endswith(" /<*>"):
template = template[:-5] + " <*>"
# Attribute key-value pair
if template.count("=<*>") >= 3:
template = template.replace("= ", "=<*> ")
return template
# Correct the templates
for dataset in datasets:
groundtruth_path = os.path.join(input_dir, f"{dataset}/{dataset}_full.log_structured.csv")
groundtruth = pd.read_csv(groundtruth_path, dtype=str)
groundtruth['EventTemplate'] = groundtruth['EventTemplate'].apply(lambda x: correct_single_template(x))
groundtruth.to_csv(os.path.join(input_dir, f"{dataset}/{dataset}_full.log_structured_corrected.csv"), index=False)
templates_path = os.path.join(input_dir, f"{dataset}/{dataset}_full.log_templates.csv")
templates = pd.read_csv(templates_path, dtype=str)
templates['EventTemplate'] = templates['EventTemplate'].apply(lambda x: correct_single_template(x))
templates.to_csv(os.path.join(input_dir, f"{dataset}/{dataset}_full.log_templates_corrected.csv"), index=False)