Repository navigation
Expand file tree
/
Copy pathAhoCorasick.java
More file actions
161 lines (145 loc) · 4.44 KB
/
Copy pathAhoCorasick.java
File metadata and controls
161 lines (145 loc) · 4.44 KB
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
/*
* AhoCorasick.java
* Created on 2010/12/03
* @package
* @author Hiroki Tanioka
* Aho Corasick Algorithm
* @update 2010/12/03 Hiroki Tanioka 1st edition
* @reference http://www.prefield.com/algorithm/string/aho_corasick.html
*/
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class AhoCorasick {
private class PMA {
PMA[] next = new PMA[0x100]; // next[0] is for fail
List<Integer> accept = new ArrayList<Integer>();
}
private volatile PMA root;
private List<PMA> list = new ArrayList<PMA>();
private List<String> words = new ArrayList<String>();
private PMA newPMA() {
PMA p = null;
try {
p = new PMA();
} catch (Exception e) {
e.printStackTrace();
p = null;
return null;
}
list.add(p);
return p;
}
/**
* constructor
*/
public AhoCorasick() {
root = null;
}
public boolean isBuilt() {
return root != null;
}
public synchronized PMA build(List<byte[]> p) {
final int size = p.size();
PMA _root = newPMA();
root = _root;
for (int i = 0; i < size; ++i) {
byte[] _p = p.get(i);
words.add(new String(_p));
// make trie
PMA t = root;
final int _size = _p.length;
for (int j = 0; j < _size; ++j) {
int c = (int) (_p[j] & 0xff);
if (t.next[c] == null) {
t.next[c] = newPMA();
}
t = t.next[c];
}
t.accept.add(i);
}
Queue<PMA> q = new LinkedList<PMA>(); // make failure link using bfs
for (int c = 0x01; c <= 0xff; ++c) {
if (root.next[c] != null) {
root.next[c].next[0] = root;
q.add(root.next[c]);
} else {
root.next[c] = root;
}
}
while (q.size() > 0) {
PMA t = q.poll();
for (int c = 0x01; c <= 0xff; ++c) {
if (t.next[c] != null) {
q.add(t.next[c]);
PMA r = t.next[0];
while (r.next[c] == null) {
r = r.next[0];
}
t.next[c].next[0] = r.next[c];
}
}
}
return root;
}
public synchronized String match(final byte[] t) {
String result = "";
PMA v = root;
if (v == null) {
System.err.println("wrong root object in match.");
return null;
}
int n = t.length;
PMA _root = v;
for (int i = 0; i < n; ++i) {
int c = (int) (t[i] & 0xff);
if (c < 0 || c > 0xff) {
c = 0xff;
System.err.println("There is an irregular character in URL("
+ t + ").");
}
if (v == null) {
v = root;
}
while (v.next[c] == null) {
v = v.next[0];
if (v == null) {
break;
}
}
if (v == null) {
continue;
}
v = v.next[c];
if (result != null) {
if (v.accept.size() > 0) {
result = words.get(v.accept.get(0));
return result;
}
} else if (v.accept.size() > 0) {
return result;
}
}
return null;
}
public static void main(String[] args) {
AhoCorasick ac = new AhoCorasick();
System.out.println("build ...");
List<byte[]> list = new ArrayList<byte[]>();
for (int i = 0; i < args.length - 1; i++) {
list.add(args[i].getBytes());
System.out.println(args[i]);
}
ac.build(list);
String test = args[args.length - 1];
System.out.println("");
System.out.print("search " + test);
String result = ac.match(test.getBytes());
if (result == null || "".equals(result)) {
System.out.println(" ... none");
} else {
System.out.println(" ... hit!! ");
}
}
}