-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsuffixtree.py
257 lines (234 loc) · 9.44 KB
/
suffixtree.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
# -*- coding: utf-8 -*-
class Node:
__num__ = -1
def __init__(self, parentkey, outedges, suffixlink=None):
self.parentkey = parentkey
self.outedges = outedges
self.suffixlink = suffixlink
Node.__num__ += 1
self.id = Node.__num__
def getoutedges(self):
return self.outedges
def setoutedge(self, key, (anode, label_start_index, label_end_index, bnode)):
if self.outedges is None:
self.outedges = {}
self.outedges[key] = (anode, label_start_index, label_end_index, bnode)
def getoutedge(self, key):
if key in self.outedges:
return self.getoutedges()[key]
else:
return None
def getparenkey(self):
return self.parentkey
def setparentkey(self, parentkey):
self.parentkey = parentkey
def getsuffixlink(self):
return self.suffixlink
def setsuffixlink(self, node):
self.suffixlink = node
def getid(self):
return self.id
@staticmethod
def __draw__(rnode, chars, v, ed='#'):
l = len(chars)
edges = rnode.getoutedges().items()
nogc = []
hasgc = []
gc = []
maxlen = len(chars) + 6
for edg in edges:
if v == 0:
if edg[1][3].getoutedges() is None:
nogc.append(edg)
else:
hasgc.append(edg)
else:
if edg[1][3].getoutedges() is None:
hasgc.append(edg)
else:
nogc.append(edg)
gc.extend(hasgc)
gc.extend(nogc)
for k, (parent, s, t, node) in gc:
if ed == '#':
if t == '#':
t = l
else:
if t == '#':
t = ed
linkid = ''
if node.getsuffixlink() is not None:
linkid = '->' + str(node.getsuffixlink().getid())
if v == 0:
print " " * maxlen * v + '|'
print " " * maxlen * v + '|' + ' ' * 3 + chars[s:t + 1]
print '+' + " " * maxlen * v + '-' + '-' * (maxlen - 1) + '● ' + '(' + str(node.getid()) + linkid + ')'
else:
print '|' + " " * maxlen * v + '|'
print '|' + " " * maxlen * v + '|' + ' ' * 3 + chars[s:t + 1]
print '|' + " " * maxlen * v + '+' + '-' * (maxlen - 1) + '● ' + '(' + str(node.getid()) + linkid + ')'
if node.getoutedges() is not None:
Node.__draw__(node, chars, v + 1, ed)
@staticmethod
def draw(root, chars, ed='#'):
print '\n', chars, '\n● (0)'
v = 0
Node.__draw__(root, chars, v, ed)
def build(chars, regularize=False):
root = Node(None, None, None)
actnode = root
actkey = ''
actlen = 0
remainder = 0 # used for splitting
ind = 0
while ind < len(chars):
ch = chars[ind]
if remainder == 0:
if actnode.getoutedges() is not None and ch in actnode.getoutedges():
actkey = ch
actlen = 1
remainder = 1
anode, start, end, bnode = actnode.getoutedge(actkey)
if end == '#':
end = ind
if end - start + 1 == actlen:
actnode = actnode.getoutedge(actkey)[3]
actkey = ''
actlen = 0
else:
aleaf = Node(None, None, None)
aedge = (actnode, ind, '#', aleaf)
aleaf.setparentkey((actnode, chars[ind]))
actnode.setoutedge(chars[ind], aedge)
else:
if actkey == '' and actlen == 0: # compare on node
if ch in actnode.getoutedges():
actkey = ch
actlen = 1
remainder += 1
else:
remainder += 1
remainder, actnode, actkey, actlen = unfold(root, chars, ind, remainder, actnode, actkey, actlen)
else: # compare on edge
anode, start, end, bnode = actnode.getoutedge(actkey)
if end == '#':
end = ind
compareposition = start + actlen
if chars[compareposition] != ch:
remainder += 1
remainder, actnode, actkey, actlen = unfold(root, chars, ind, remainder, actnode, actkey, actlen)
else:
if compareposition < end: # on edge
actlen += 1
remainder += 1
else: # on node
remainder += 1
actnode = actnode.getoutedge(actkey)[3]
if compareposition == end:
actlen = 0
actkey = ''
else:
actlen = 1
actkey = ch
ind += 1
if ind == len(chars) and remainder > 0:
if regularize:
chars = chars + '$'
return root, chars
def unfold(root, chars, ind, remainder, actnode, actkey, actlen):
prenode = None
while remainder > 0:
remains = chars[ind - remainder + 1:ind + 1]
actlen_re = len(remains) - 1 - actlen
actnode, actkey, actlen, actlen_re = hop(ind, actnode, actkey, actlen, remains, actlen_re)
lost, actnode, actkey, actlen, actlen_re = step(chars, ind, actnode, actkey, actlen, remains, actlen_re)
if lost:
if actlen == 1 and prenode is not None and actnode is not root:
prenode.setsuffixlink(actnode)
return remainder, actnode, actkey, actlen
if actlen == 0:
if remains[actlen_re] not in actnode.getoutedges():
aleaf = Node(None, None, None)
aedge = (actnode, ind, '#', aleaf)
aleaf.setparentkey((actnode, chars[ind]))
actnode.setoutedge(chars[ind], aedge)
else: # on edge
anode, start, end, bnode = actnode.getoutedge(actkey)
if remains[actlen_re + actlen] != chars[start + actlen]:
# split
anode, start, end, bnode = actnode.getoutedge(actkey)
newnode = Node(None, None, None)
halfedge1 = (actnode, start, start + actlen - 1, newnode)
halfedge2 = (newnode, start + actlen, end, bnode)
actnode.setoutedge(actkey, halfedge1)
newnode.setparentkey((actnode, actkey))
newnode.setoutedge(chars[start + actlen], halfedge2)
aleaf = Node(None, None, None)
aedge = (newnode, ind, '#', aleaf)
aleaf.setparentkey((newnode, chars[ind]))
newnode.setoutedge(chars[ind], aedge)
else:
return remainder, actnode, actkey, actlen
if prenode is not None and 'aleaf' in locals() and aleaf.getparenkey()[0] is not root:
prenode.setsuffixlink(aleaf.getparenkey()[0])
if 'aleaf' in locals() and aleaf.getparenkey()[0] is not root:
prenode = aleaf.getparenkey()[0]
if actnode == root and remainder > 1:
actkey = remains[1]
actlen -= 1
if actnode.getsuffixlink() is not None:
actnode = actnode.getsuffixlink()
else:
actnode = root
remainder -= 1
return remainder, actnode, actkey, actlen
def step(chars, ind, actnode, actkey, actlen, remains, ind_remainder):
rem_label = remains[ind_remainder:]
if actlen > 0:
anode, start, end, bnode = actnode.getoutedge(actkey)
if end == '#':
end = ind
edgelabel = chars[start:end + 1]
if edgelabel.startswith(rem_label):
actlen = len(rem_label)
actkey = rem_label[0]
return True, actnode, actkey, actlen, ind_remainder
else:
# on node
if ind_remainder < len(remains) and remains[ind_remainder] in actnode.getoutedges():
anode, start, end, bnode = actnode.getoutedge(remains[ind_remainder])
if end == '#':
end = ind
edgelabel = chars[start:end + 1]
if edgelabel.startswith(rem_label):
actlen = len(rem_label)
actkey = rem_label[0]
return True, actnode, actkey, actlen, ind_remainder
return False, actnode, actkey, actlen, ind_remainder
def hop(ind, actnode, actkey, actlen, remains, ind_remainder):
if actlen == 0 or actkey == '':
return actnode, actkey, actlen, ind_remainder
anode, start, end, bnode = actnode.getoutedge(actkey)
if end == '#':
end = ind
edgelength = end - start + 1
while actlen > edgelength:
actnode = actnode.getoutedge(actkey)[3]
ind_remainder += edgelength
actkey = remains[ind_remainder]
actlen -= edgelength
anode, start, end, bnode = actnode.getoutedge(actkey)
if end == '#':
end = ind
edgelength = end - start + 1
if actlen == edgelength:
actnode = actnode.getoutedge(actkey)[3]
actkey = ''
actlen = 0
ind_remainder += edgelength
return actnode, actkey, actlen, ind_remainder
if __name__ == "__main__":
docs = ['abcabxabcd', 'dedododeeodoeodooedeeododooodoede$', 'ooooooooo', 'mississippi']
for text in docs:
tree, pst = build(text, regularize=True)
Node.draw(tree, pst, ed='#')