-
Notifications
You must be signed in to change notification settings - Fork 3
/
subseq_tree.py
298 lines (270 loc) · 8.67 KB
/
subseq_tree.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'''
Tree structure where every node is a common substring of either characters or
character classes. Leaves are lists of strings where no common substring can
be extracted anymore (e.g. ["AA", "A", ""]).
'''
import re
from utils import *
class Decisioner(object):
'''Contains static methods that heuristically decide stuff'''
@staticmethod
def generalize_kleen(minim, maxim):
'''Decides if it's ok to use +/* instead of the more strict {min,Max}'''
if maxim - minim > 4:
return True
@staticmethod
def prioritize_LCSeqs(lcss):
'''Taking a list of longest common sequences, it decides the best'''
# Class priorities (first = largest priority)
# [change this order to adjust priorities]
cls_ordering = ['#', 'O', 'S', 'N', 'A', 'a']
def class_representative(s):
if len(s) == 0:
return '#' # This case should never be reached
repc = s[0]
for c in s:
if c != repc:
return '#' # Mixed classes
return repc
lcsclss = [class_representative(chars_to_classes(s)) for s in lcss]
return [cls_ordering.index(c) for c in lcsclss]
def chars_to_classes(cs):
'''Replaces characters with their class reference'''
# Alphabetic
cs = re.sub(r'[a-z]', 'a', cs)
cs = re.sub(r'[A-Z]', 'A', cs)
# Numerical
cs = re.sub(r'[0-9]', 'N', cs)
# Space has it's own class
cs = re.sub(r' ', 'S', cs)
# Special
cs = re.sub(r'[^aANS]', 'O', cs)
return cs
def gen_tree(strs, tp='chars'):
'''Generates a tree with longest common substrings as nodes'''
lcss = long_substr(strs)
if len(lcss) == 0:
lcs = ''
else:
lcs_prs = Decisioner.prioritize_LCSeqs(lcss)
imin = 0
xmin = 1000
# Find the #1 priority
for i, p in enumerate(lcs_prs):
if p < xmin:
xmin = p
imin = i
lcs = lcss[imin]
if not lcs:
if tp == 'chars':
strs = [chars_to_classes(s) for s in strs]
return gen_tree(strs, tp='classes')
else:
return strs
bgns = []
ends = []
for s in strs:
i = s.find(lcs)
bgn = s[:i]
bgns.append(bgn)
end = s[i+len(lcs):]
ends.append(end)
return {
'root': lcs,
'left': gen_tree(bgns, tp),
'right': gen_tree(ends, tp),
'type': tp
}
def tree_to_regex(tree):
'''
[Based on tree generated by gen_tree()] Generates a regex with an inorder
traversal.
Uses Decisioner class to decide on when to generalize more or less.
'''
def strs_range(strs):
'''Helper function: '''
minim = 99999
maxim = 0
clss = []
for s in strs:
for sc in s:
if sc not in clss:
clss.append(sc)
l = len(s)
if l > maxim:
maxim = l
if l < minim:
minim = l
return (clss, minim, maxim)
clss = {
'a': 'a-z',
'A': 'A-Z',
'N': '0-9',
'S': ' ',
'O': '!@#$%^&*()_+=-`~\'";:,<.>/?\\\]}\[{',
# 'O': '!@#$%',
}
if type(tree) == dict:
if tree['type'] == 'chars':
return tree_to_regex(tree['left']) + \
re.escape(tree['root']) + \
tree_to_regex(tree['right'])
elif tree['type'] == 'classes':
out = ''
last = ''
count = 0
for c in tree['root']:
if c == last:
count += 1
elif last == '':
last = c
count = 1
else:
out += '[%s]' % clss[last]
if count > 1:
out += '{%d}' % count
last = c
count = 1
out += '[%s]' % clss[last]
if count > 1:
out += '{%d}' % count
return tree_to_regex(tree['left']) + \
out + \
tree_to_regex(tree['right'])
else:
cs, m, M = strs_range(tree)
if not M:
return ''
out = '['
for c in cs:
out += clss[c]
out += ']'
if M > 1:
if Decisioner.generalize_kleen(m, M):
if not m:
out += '*'
else:
out += '+'
else:
out += '{%d,%d}' % (m, M)
elif m == 0:
out += '?'
return out
def tree_to_HTML(tree):
'''
[Based on tree generated by gen_tree()] Generates an HTML block with a nice
representation of the regex, using an inorder traversal.
Similar to tree_to_regex(), but generates a visual thing, only for display.
'''
def strs_range(strs):
'''Helper function: '''
minim = 99999
maxim = 0
clss = []
for s in strs:
for sc in s:
if sc not in clss:
clss.append(sc)
l = len(s)
if l > maxim:
maxim = l
if l < minim:
minim = l
return (clss, minim, maxim)
clss = {
'a': 'a-z',
'A': 'A-Z',
'N': '0-9',
'S': '<span class="rgt-space-char rgt-tooltip" title="Space character"> </span>',
'O': '!@#<i class="fa fa-ellipsis-h rgt-ellipsis"></i>',
}
if type(tree) == dict:
if tree['type'] == 'chars':
return tree_to_HTML(tree['left']) + \
'<span class="rgt-exact-match rgt-tooltip">' + \
tree['root'].replace(' ', '<span class="rgt-space-char rgt-tooltip" title="Space character"> </span>') + \
'</span>' + \
tree_to_HTML(tree['right'])
elif tree['type'] == 'classes':
out = ''
last = ''
count = 0
for c in tree['root']:
if c == last:
count += 1
elif last == '':
last = c
count = 1
else:
out += '<span class="rgt-range rgt-tooltip">'
out += '[%s]' % clss[last]
out += '</span>'
if count > 1:
out += '<span class="rgt-counts">'
out += '{%d}' % count
out += '</span>'
last = c
count = 1
out += '<span class="rgt-range rgt-tooltip">'
out += '[%s]' % clss[last]
out += '</span>'
if count > 1:
out += '<span class="rgt-counts">'
out += '{%d}' % count
out += '</span>'
return tree_to_HTML(tree['left']) + \
out + \
tree_to_HTML(tree['right'])
else:
cs, m, M = strs_range(tree)
if not M:
return ''
out = '<span class="rgt-range rgt-tooltip">'
out += '['
for c in cs:
out += clss[c]
out += ']'
out += '</span>'
if M > 1:
if Decisioner.generalize_kleen(m, M):
if not m:
out += '<span class="rgt-quantifier">'
out += '*'
out += '</span>'
else:
out += '<span class="rgt-quantifier">'
out += '+'
out += '</span>'
else:
out += '<span class="rgt-counts">'
out += '{%d,%d}' % (m, M)
out += '</span>'
elif m == 0:
out += '<span class="rgt-quantifier">'
out += '?'
out += '</span>'
return out
if __name__ == '__main__':
import pprint
pp = pprint.PrettyPrinter()
s1 = 'abc$1250'
s2 = 'xby#340'
s3 = 'sbs@00000'
# pp.pprint(gen_tree([s1, s2, s3]))
# print tree_to_HTML(gen_tree([s1, s2, s3]))
print tree_to_regex(gen_tree([s1, s2, s3])) # [a-z]b[a-z][!@#$%^&*()_+=-`~'";:,<.>/?\\]}\[{][0-9]{0,3}0[0-9]{0,4}
s1 = 'skull'
s2 = 'school'
# pp.pprint(gen_tree([s1, s2]))
print tree_to_regex(gen_tree([s1, s2])) # s[a-z][a-z][a-z]{0,2}l[a-z]?
s1 = 'RFC 821'
s2 = 'RFC 6409'
print
print tree_to_regex(gen_tree([s1, s2]))
print
pp.pprint(gen_tree([s1, s2]))
s1 = 'Oh, hello, my friend...'
s2 = 'I prefer Jelly Belly beans...'
s3 = 'When hell freezes... over!'
print tree_to_regex(gen_tree([s1, s2, s3]))
pp.pprint(gen_tree([s1, s2, s3]))