-
Notifications
You must be signed in to change notification settings - Fork 7
/
regroup.py
executable file
·48 lines (39 loc) · 1.51 KB
/
regroup.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
#!venv/bin/python
"""
standalone program that reads input and outputs a regex that describes it
"""
from regroup import DAWG, DAWGRelaxer
if __name__ == '__main__':
# ./regroup.py --relax --cluster-prefix-len=2
import argparse
import re
import sys
# commandline options
parser = argparse.ArgumentParser()
parser.add_argument('--relax', action='store_true',
help='attempt to simplify pattern where possible')
parser.add_argument('--cluster-prefix-len', type=int,
help='split by prefix of a given length')
parser.add_argument('--count', action='store_true',
help='count matches against input')
args = parser.parse_args()
# run
lines = [line.rstrip('\r\n') for line in sys.stdin.readlines()]
dawg = DAWG.from_iter(lines)
if args.relax:
dawg = DAWGRelaxer(dawg).relax()
# output
# either we split/cluster one big pattern into sub-patterns by some method...
# ...or we just dump one big pattern
if args.cluster_prefix_len:
clusters = dawg.cluster_by_prefixlen(args.cluster_prefix_len)
for prefix, suffix_tree in clusters:
d = DAWG.from_dawg(suffix_tree)
pattern = prefix + DAWGRelaxer(d).relax().serialize()
if args.count:
cnt = sum(re.match('^' + pattern + '$', line) is not None for line in lines)
print(cnt, pattern)
else:
print(pattern)
else:
print(dawg.serialize())