-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAhoCorasick.c
204 lines (155 loc) · 4.63 KB
/
AhoCorasick.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "AhoCorasick.h"
Node buildTree(char* input[], int size) {
Node root = NodeFactory();
for(int i=0;i<size;i++) {
Node current = root;
for(int j=0;j<strlen(input[i]);j++) {
// don't have this branch, create new
if(current->next[(int) input[i][j]] == NULL) {
Node newNode = NodeFactory();
newNode->subString = input[i][j];
newNode->parent = current;
current->next[(int) input[i][j]] = newNode;
current = newNode;
}
// share
else {
current = current->next[(int) input[i][j]];
}
}
current->pattern = malloc(strlen(input[i]) + 1);
strcpy(current->pattern, input[i]);
}
return root;
}
void buildFailPath(Node root) {
NodeQueue queue = NodeQueueFactory();
NodeQueueOperation queueOperation = NodeQueueOperationFactory();
root->fail = root;
// all node of level 2 's fail path is root.
for(int i=0;i<128;i++) {
if(root->next[i] != NULL) {
for(int j=0;j<128;j++) {
if(root->next[i]->next[j] != NULL) {
queueOperation->enqueue(queue, root->next[i]->next[j]);
}
}
root->next[i]->fail = root;
}
}
while(queue->rear != NULL) {
Node current = queueOperation->dequeue(queue);
for(int j=0;j<128;j++) {
if(current->next[j] != NULL) {
queueOperation->enqueue(queue, current->next[j]);
}
}
char subString = current->subString;
Node parent = current->parent;
while(1) {
if(parent->fail->next[(int) subString] != NULL) {
current->fail = parent->fail->next[(int) subString];
break;
}
else {
if(parent->fail == root) {
current->fail = root;
break;
}
else {
parent = parent->fail->parent;
}
}
}
}
free(queue);
}
MatchingResult search(Node root, char* content) {
MatchingResult result = malloc(sizeof(MatchingResultStruct));
result->next = NULL;
MatchingResult head = result;
Node current = root;
int len = strlen(content), i = 0;
while(i < len) {
int position = content[i];
if(current->next[position] != NULL) {
current = current->next[position];
if(current->pattern) {
MatchingResult res = malloc(sizeof(MatchingResultStruct));
res->start = i - strlen(current->pattern) + 2;
res->end = i + 1;
res->pattern = current->pattern;
res->next = NULL;
result->next = res;
result = res;
//printf(" - find %s at position %d\n", current->pattern, i - strlen(current->pattern) + 2);
}
i++;
}
else {
if(current == root) {
i++;
}
else {
current = current->fail;
if(current->pattern) {
MatchingResult res = malloc(sizeof(MatchingResultStruct));
res->start = i - strlen(current->pattern) + 1;
res->end = i;
res->pattern = current->pattern;
res->next = NULL;
result->next = res;
result = res;
//printf(" - find %s at position %d\n", current->pattern, i - strlen(current->pattern) + 1);
}
}
}
}
while(current != root) {
current = current->fail;
if(current->pattern) {
MatchingResult res = malloc(sizeof(MatchingResultStruct));
res->start = i - strlen(current->pattern) + 1;
res->end = i;
res->pattern = current->pattern;
res->next = NULL;
result->next = res;
result = res;
//printf(" - find %s at position %d\n", current->pattern, i - strlen(current->pattern) + 1);
}
}
return head;
}
void destroy(Node root) {
if(!root) {
return;
}
NodeQueue queue = NodeQueueFactory();
NodeQueueOperation queueOperation = NodeQueueOperationFactory();
Node current = NULL;
queueOperation->enqueue(queue, root);
root = NULL;
while(queue->rear != NULL) {
current = queueOperation->dequeue(queue);
for(int i=0;i<128;i++) {
if(current->next[i] != NULL) {
queueOperation->enqueue(queue, current->next[i]);
}
}
free(current);
}
}
AhoCorasick AhoCorasickFactory() {
static AhoCorasick ahoCorasick = NULL;
if(!ahoCorasick) {
ahoCorasick = malloc(sizeof(AhoCorasickStruct));
ahoCorasick->buildTree = buildTree;
ahoCorasick->buildFailPath = buildFailPath;
ahoCorasick->search = search;
ahoCorasick->destroy = destroy;
}
return ahoCorasick;
}