-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.java
More file actions
72 lines (64 loc) · 2.31 KB
/
Copy pathDictionary.java
File metadata and controls
72 lines (64 loc) · 2.31 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
package test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Dictionary {
String[] fileNames;
CacheManager exist;
CacheManager notExist;
BloomFilter bloomFilter;
public Dictionary(String... fileNames) {
this.fileNames = fileNames;
this.exist = new CacheManager(400, new LRU());
this.notExist = new CacheManager(100, new LFU());
this.bloomFilter = new BloomFilter(256, "MD5", "SHA1");
this.insertBloomFilter(fileNames);
}
private void insertBloomFilter(String... fileNames) {
// Iterate over each file name
for (String fileName : fileNames) {
// The resource (reader) will be automatically closed when exiting this try block
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
Set<String> uniqueWords = new HashSet<>();
// Read each line from the file
while ((line = reader.readLine()) != null) {
// Split the line into words by whitespace and add them to the set
String[] words = line.split("\\s+");
uniqueWords.addAll(Arrays.asList(words));
}
// Add each unique word to the Bloom filter
for (String word : uniqueWords) {
bloomFilter.add(word);
}
} catch (IOException e) {
e.printStackTrace(); // Handle or log the exception
}
}
}
public boolean query(String word) {
if (this.exist.query(word)){ return true; }
if (this.notExist.query(word)){ return false; }
if (this.bloomFilter.contains(word)) {
this.exist.add(word);
return true;
}
else {
this.notExist.add(word);
return false;
}
}
public boolean challenge(String word) {
boolean result = IOSearcher.search(word, this.fileNames);
if (result) {
this.exist.add(word);
}
else {
this.notExist.add(word);
}
return result;
}
}