-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTreeDictionary.java
More file actions
86 lines (68 loc) · 1.96 KB
/
TreeDictionary.java
File metadata and controls
86 lines (68 loc) · 1.96 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
package predictive;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import predictive.PredictivePrototype;
public class TreeDictionary implements Dictionary{
private TreeDictionary[] treeDict = new TreeDictionary[8];
private Set<String> words;
public TreeDictionary() {
for (int i = 0; i < treeDict.length; i++) {
this.treeDict[i] = null;
}
this.words = new HashSet<String>();
}
public TreeDictionary(String path) {
for (int i = 0; i < treeDict.length; i++) {
this.treeDict[i] = new TreeDictionary();
}
this.words = new HashSet<String>();
Scanner scr = null;
try {
File theFile = new File(path);
scr = new Scanner(theFile);
while (scr.hasNext()) {
String wordx = scr.nextLine().toLowerCase();
if (PredictivePrototype.isValidWord(wordx)) {
String x =PredictivePrototype.wordToSignature(wordx);
int y = Integer.parseInt(x.substring(0, 1));
this.treeDict[y-2].addWords(x, wordx, 1);
}
}
}
catch (IOException e) {
System.out.println("File has not been found.");
} finally {
scr.close();
}
}
public void addWords(String signature, String word, int x) {
this.words.add(word);
if (x < signature.length()) {
int b = Integer.parseInt(signature.substring(x, x+1));
x++;
if(this.treeDict[b-2]==null) {
this.treeDict[b-2] = new TreeDictionary();
}
this.treeDict[b-2].addWords(signature, word, x);
}
}
@Override
public Set<String> signatureToWords(String signature) {
Set<String> setWords = new HashSet<>();
int b = signature.length();
signatureToWordsx(signature).forEach(word -> setWords.add(word.substring(0, b)));
return setWords;
}
public Set<String> signatureToWordsx(String signature) {
if (signature.isEmpty()) {
return words;
}
else {
int b = Integer.parseInt(signature.substring(0, 1));
return this.treeDict[b - 2].signatureToWordsx(signature.substring(1));
}
}
}