-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaturate-reject.py
executable file
·135 lines (120 loc) · 4.37 KB
/
saturate-reject.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
#!/bin/env python
import re
from sys import stdin
from collections import defaultdict
from optparse import OptionParser, OptionGroup
def curly_replace(fmt, matcher):
groups = list(matcher.groups())
groups.insert(0, matcher.group())
return fmt.format(*groups, **matcher.groupdict())
def backref_replace(fmt, matcher):
return matcher.expand(fmt)
class Saturate:
def __init__(self, expr, name, formats, replace_fun):
if isinstance(expr, unicode):
self.expr = re.compile(expr, re.U)
elif isinstance(expr, str):
self.expr = re.compile(expr)
else:
self.expr = expr
self.name = str(name)
self.formats = tuple(formats)
self.replace_fun = replace_fun
def _match(self, s):
result = self.expr.match(s)
if result is None:
return None
if result.end() < len(s):
return None
return result
def matches(self, s):
return self._match(s) is not None
def replacements(self, s):
matcher = self._match(s)
if matcher is not None:
for rep in self.formats:
yield self.replace_fun(rep, matcher)
def _load_saturate_line(line, replace_fun):
cols = line.split('\t')
return Saturate(cols[0], cols[1], cols[2:], replace_fun)
def load_saturate(f, replace_fun):
return tuple(_load_saturate_line(line.strip(), replace_fun) for line in f)
def all_saturate(count, sats, s):
yield s
for sat in sats:
seen = False
for r in sat.replacements(s):
seen = True
yield r
if seen:
count[sat.name] += 1
def saturate(count, sats, f, column, separator):
for line in f:
line = line.strip()
if column is None:
cols = None
s = line
else:
cols = line.split(separator)
s = cols[column]
for r in all_saturate(count, sats, s):
if column is None:
print r
else:
cols[column] = r
print '\t'.join(cols)
if __name__ == '__main__':
parser = OptionParser(
usage='usage: %prog [options] [files]',
description='Saturate dictionaries with regular expression patterns',
epilog='Saturation patterns file format: one per line with 3 or more tab-separated columns: 1. Regular expression (must match the whole entry) 2. Pattern name 3+. Replacement patterns'
)
parser.add_option('-c',
'--column',
action='store',
type='int',
dest='column',
default=None,
help='saturate the COLth column (start at 0, by default saturate the whole line)',
metavar='COL'
)
parser.add_option('-S',
'--separator',
action='store',
type='string',
dest='separator',
default='\t',
help='column separator (default: tab)',
metavar='SEP'
)
parser.add_option('-s',
'--saturation-file',
action='append',
type='string',
dest='saturation_file_list',
default=[],
help='file containing saturation patterns',
metavar='FILE'
)
parser.add_option('-B',
'--backref-format',
action='store_const',
dest='replace_fun',
const=backref_replace,
default=curly_replace,
help='replacement templates in regular expression backreference syntax (backslash instead of curly braces)'
)
options, args = parser.parse_args()
saturates = []
for fn in options.saturation_file_list:
f = open(fn)
saturates.extend(load_saturate(f, options.replace_fun))
f.close()
count = defaultdict(int)
if args:
for fn in args:
f = open(fn)
saturate(count, saturates, f, options.column, options.separator)
f.close()
else:
saturate(count, saturates, stdin, options.column, options.separator)