-
Notifications
You must be signed in to change notification settings - Fork 0
/
BKTree.java
97 lines (78 loc) · 2.8 KB
/
BKTree.java
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class BKTree {
private static final int LIMIT = 3;
private Node root;
private List<Node> nodes = new ArrayList<>();
private static class Node {
private final String word;
private final Map<Integer, Integer> nodeIndex;
private Node(String word) {
this.word = word;
nodeIndex = new HashMap<>();
}
}
private BKTree() {
}
static BKTree build(ArrayList<String> dictionary) {
BKTree tree = new BKTree();
tree.root = new Node(dictionary.get(0));
for (int i = 1; i < dictionary.size(); ++i) {
makeBKTree(tree, tree.root, dictionary.get(i));
}
return tree;
}
private static void makeBKTree(BKTree tree, Node root, String word) {
int distance = findMinimumEditDistance(word, root.word);
Integer ptr = root.nodeIndex.get(distance);
if (null != ptr) {
makeBKTree(tree, tree.nodes.get(ptr), word);
} else {
tree.nodes.add(new Node(word));
root.nodeIndex.put(distance, tree.nodes.size() - 1);
}
}
List<String> getSimilarWords(String search) {
List<String> result = new ArrayList<>();
getSimilarWords(root, result, search);
return result;
}
private void getSimilarWords(Node root, List<String> result, String search) {
int distance = findMinimumEditDistance(search, root.word);
if (distance <= LIMIT) {
result.add(root.word);
}
int start = distance - LIMIT;
if (start < 0) {
start = 1;
}
while (start < distance + LIMIT) {
Integer ptr = root.nodeIndex.get(start);
if (null != ptr) {
getSimilarWords(nodes.get(ptr), result, search);
}
start++;
}
}
private static int findMinimumEditDistance(String source, String destination) {
int sourceLength = source.length();
int destinationLength = destination.length();
int[][] editDP = new int[sourceLength + 1][destinationLength + 1];
for (int s = 0; s <= sourceLength; ++s) {
for (int d = 0; d <= destinationLength; ++d) {
if (0 == s) {
editDP[s][d] = d;
} else if (0 == d) {
editDP[s][d] = s;
} else if (source.charAt(s - 1) == destination.charAt(d - 1)) {
editDP[s][d] = editDP[s - 1][d - 1];
} else {
editDP[s][d] = 1 + Math.min(Math.min(editDP[s][d - 1], editDP[s - 1][d]), editDP[s - 1][d - 1]);
}
}
}
return editDP[sourceLength][destinationLength];
}
}