-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaho-corasick.py
154 lines (128 loc) · 4.57 KB
/
aho-corasick.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
class Node:
def __init__(self):
self.children = {}
self.suffix = ""
self.endOfString = False
self.failureLink = None
self.dictionaryLink = None
class AhoCorasickAutomaton:
def __init__(self):
self.root = Node()
def searchString(self, word):
currentNode = self.root
for i in word:
node = currentNode.children.get(i)
if node == None:
return self.root
currentNode = node
if currentNode.endOfString == True:
return currentNode
else:
return self.root
def insertString(self, word):
current = self.root
for i in range(len(word)):
ch = word[i]
node = current.children.get(ch)
if node == None:
node = Node()
current.children.update({ch:node})
node.suffix = word[i:]
current = node
current.endOfString = True
def deleteString(root, word, index):
ch = word[index]
currentNode = root.children.get(ch)
canNodeBeDeleted = False
if len(currentNode.children) > 1:
deleteString(currentNode, word, index+1)
return False
if index == len(word) - 1:
if len(currentNode.children) >= 1:
currentNode.endOfString = False
return False
else:
root.children.pop(ch)
return True
if currentNode.endOfString == True:
deleteString(currentNode, word, index+1)
return False
canNodeBeDeleted = deleteString(currentNode, word, index+1)
if canNodeBeDeleted == True:
root.children.pop(ch)
return True
else:
return False
def traverse(root):
if (root == None):
return []
nodes = [root]
for child in root.children.values():
nodes.extend(traverse(child))
# childNodes = traverse(child)
# for node in childNodes:
# nodes.append(node)
return nodes
# def traverse(root):
# answer = []
# traverseUtil(root, answer)
# return answer
#
# def traverseUtil(root, answer):
# if (root == None):
# return
# for child in root.children:
# answer.append(traverseUtil(child, answer))
# answer.append(root)
# return
def ahoCorasick(database):
trie = AhoCorasickAutomaton()
for entry in database:
trie.insertString(entry)
nodeList = traverse(trie.root)
nodeList = traverse(trie.root)
# failure link logic
# for each node in the trie
# get a list of all its proper suffixes
# make the failure link of the node the longest suffix in the trie
for node in nodeList:
suffixes = []
for i in range(len(node.suffix)):
suffixes.append(node.suffix[i:])
print(suffixes)
longestSuffix = ""
max = 0
for suffix in suffixes:
if(trie.searchString(suffix) != False):
if(len(suffix) > max):
max = len(suffix)
longestSuffix = suffix
node.failureLink = trie.searchString(longestSuffix)
for node in nodeList:
current = node
while(current != trie.root and not current.failureLink.endOfString):
current = current.failureLink
if(current.failureLink.endOfString):
node.dictionaryLink = current.failureLink
# traverse failure links until reach word node or root
# if end up at root, no dictionary link
# else, dictionary link of original equals first word node found
print("Number of Nodes: ", len(nodeList))
numFailureLinks = 0
numDictionaryLinks = 0
for node in nodeList:
if (node.failureLink):
numFailureLinks += 1
if (node.dictionaryLink):
numDictionaryLinks += 1
print("Number of Failure Links: ", numFailureLinks)
print("Number of Dictionary Links: ", numDictionaryLinks)
for node in nodeList:
print("IsWordNode: ", node.endOfString)
print("Suffix: ", node.suffix)
print("Failure Link: ", node.failureLink)
print("Dictionary Link: ", node.dictionaryLink)
# output: list of nodes, number of nodes
# failure links, number of failure links
# dictionary links, number of dictionary links
ahoCorasick(["MovieTheatre","Movie","Pop","Popcorn","Move","SodaPop","Mover"])