forked from chokkan/crfsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.py
47 lines (39 loc) · 1.12 KB
/
bench.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
import re
import collections
LOGDIR='log/'
def seconds(s):
p = s.find(':')
q = s.find(':', p+1)
return int(s[:p]) * 3600 + int(s[p+1:q]) * 60 + int(s[q+1:])
def last(X):
if len(X) >= 1:
return X[-1]
else:
return None
def diffmin(X):
D = []
prev = None
for x in X:
if prev is not None:
D.append(x - prev)
prev = x
return min(D)
def analyze_log(fi, patterns):
P = {}
for name, pattern, index, cast, func in patterns:
P[name] = (re.compile(pattern), index, cast, func)
D = collections.defaultdict(list)
for line in fi:
line = line.strip('\n')
for name, (regex, index, cast, func) in P.iteritems():
m = regex.search(line)
if m is not None:
if isinstance(index, tuple):
for i in index:
D[name].append(cast(m.group(i)))
elif isinstance(index, int):
D[name].append(cast(m.group(index)))
R = {}
for name, (regex, index, cast, func) in P.iteritems():
R[name] = func(D[name])
return R