-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.java
More file actions
67 lines (55 loc) · 1.99 KB
/
dictionary.java
File metadata and controls
67 lines (55 loc) · 1.99 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/*This code was copied from user drewmore on StackOverflow,
* with some slight alterations to the base. If you would like to find the original code,
* you can find it at
* https://codereview.stackexchange.com/questions/48908/java-implementation-of-spell-checking-algorithm
*/
public class dictionary {
private int M = 2029;
final private Bucket[] array;
public dictionary() {
array = new Bucket[M];
for (int i = 0; i < M; i++) {
array[i] = new Bucket();
}
}
private int hash(String key) {
return (key.hashCode() & 0x7fffffff) % M;
}
//call hash() to decide which bucket to put it in, do it.
public void add(String key) {
array[hash(key)].put(key);
}
//call hash() to find what bucket it's in, get it from that bucket.
public boolean contains(String input) {
input = input.toLowerCase();
return array[hash(input)].get(input);
}
public void build(String filePath) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
add(line);
}
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
//this method is used in my unit tests
@SuppressWarnings("rawtypes")
public String[] getRandomEntries(int num){
String[] toRet = new String[num];
for (int i = 0; i < num; i++){
//pick a random bucket, go out a random number
Node n = array[(int)Math.random()*M].first;
int rand = (int)Math.random()*(int)Math.sqrt(num);
for(int j = 0; j<rand && n.getNextNode() != null; j++) n = n.getNextNode();
toRet[i]=(String) n.data;
}
return toRet;
}
}