forked from mandricigor/imrep
-
Notifications
You must be signed in to change notification settings - Fork 4
/
search.py
59 lines (47 loc) · 1.78 KB
/
search.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
from suffix_tree import SuffixTree
from suffix_tree import GeneralisedSuffixTree
class IgorSuffixTree(object):
def __init__(self, string):
self.__stree = SuffixTree(string)
def search_stree(self, string):
current_index = -1
terminal = False
string = list(string)
str_index = 0
current = self.__stree.root
while True:
current_child = current.firstChild
found = False
while True:
if current_child == None:
break
edgeLabel = current_child.edgeLabel
#print edgeLabel
if string[str_index] == edgeLabel[0]:
found = True
break
else:
#elif current_child.edgeLabel == current_child.lastChild.edgeLabel:
# break
current_child = current_child.next
if found:
#print edgeLabel
len_matched = 0
#print edgeLabel
current_index = current_child.index
while len_matched < len(edgeLabel) and str_index < len(string) and string[str_index] == edgeLabel[len_matched]:
str_index += 1
len_matched += 1
#print str_index, len_matched
if len_matched < len(edgeLabel):
if edgeLabel[len_matched] in ["|", "$"]:
terminal = True
break
else:
if str_index == len(string):
terminal = True
break
current = current_child
else:
break
return str_index, current_index, terminal