-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDLB.java
More file actions
206 lines (157 loc) · 7.66 KB
/
DLB.java
File metadata and controls
206 lines (157 loc) · 7.66 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.io.*;
import java.util.ArrayList;
import java.util.Map;
public class DLB {
private DLBNode root;
private DLBNode suggestionRoot;
public DLB() {
root = new DLBNode(); //main dlb trie for dictionary
suggestionRoot = new DLBNode(); //dlb trie for user history
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public void setTrie(String filename) throws IOException {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
String word;
while ((word = bufferedReader.readLine()) != null) {
word += "$";
if (filename.equals("dictionary.txt")) //ability to use function with history and dictionary
insert(word, root);
else insert(word, suggestionRoot);
}
} catch (FileNotFoundException f) {
System.out.println("File not Found");
}
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public void insert(String word, DLBNode root) {
DLBNode curr = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (i > 0)
while (curr.rightSib != null && curr.value != word.charAt(i - 1)) //find right sib to match current character
curr = curr.rightSib;
if (curr.child == null) //if there is no child, it's a new word, so start building it
addChild(curr, c);
else if (curr.child.value != c && !searchSibling(curr.child, c)) //non existing word with no siblings, create new
addSibling(curr.child, c);
curr = curr.child; //go to next character in word
}
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public void addSibling(DLBNode curr, char c) {
while (curr.rightSib != null) //get to the end of the linked list to add
curr = curr.rightSib;
curr.rightSib = new DLBNode(c);
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public void addChild(DLBNode parent, char c) {
parent.child = new DLBNode(c);
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public boolean searchSibling(DLBNode currLevel, char target) {
while (currLevel != null) {
if (currLevel.value == target) //traverse through level to find match
return true;
currLevel = currLevel.rightSib;
}
return false;
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public DLBNode search(String word, DLBNode root) {
DLBNode curr = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (curr.child == null) //no word found
return null;
else if (curr.child.value == c) //next char has been found, move farther down trie
curr = curr.child;
else if (!searchSibling(curr.child, c)) //no match is found in the childs siblings aka word is not found
return null;
else if (curr.child.value != c && searchSibling(curr.child, c)) //there is a match but its a childs sibling so find that
{
curr = curr.child;
while (curr.rightSib != null && curr.value != word.charAt(i))
curr = curr.rightSib;
}
}
return curr;
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public ArrayList<String> getSuggestions(ArrayList<String> suggestions, String word, boolean type) {
if (suggestions == null)
suggestions = new ArrayList<>();
StringBuilder sb = new StringBuilder();
sb.append(word);
DLBNode curr;
if (type)
curr = search(word, root); //able to use function for history and dictionary tries
else
curr = search(word, suggestionRoot);
if (findSuggestions(suggestions, curr, sb, 1)) // call recursive function
return suggestions;
else
return suggestions;
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public boolean findSuggestions(ArrayList<String> arrayList, DLBNode curr, StringBuilder sb, int direction) {
boolean done = false;
while (arrayList.size() <= 5) {
if (direction != 5) {
if (arrayList.size() == 5) //only return 5 suggestions
return true;
if (curr.value == '$') //word has been found
{
sb.deleteCharAt(sb.length() - 1); //delete delimiting character ($)
if (!arrayList.contains(sb.toString())) //do not add duplicates
arrayList.add(sb.toString());
} else {
if (direction != 2)
done = findSuggestions(arrayList, curr = curr.child, sb.append(curr.value), 3);
if (curr.rightSib != null)
direction = 2;
else
direction = 1;
if (!done && direction != 1)
done = findSuggestions(arrayList, curr = curr.rightSib, sb.append(curr.value), 1);
while (!done && curr.rightSib != null)
done = findSuggestions(arrayList, curr = curr.rightSib, sb.append(curr.value), 1);
}
}
if (sb.length() > 0 && direction == 1 || sb.length() > 0 && direction == 2)
sb.deleteCharAt(sb.length() - 1);
return done;
}
return false;
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public void addToHistory(String word) throws IOException {
try {
FileWriter fileWriter = new FileWriter("user_history.txt", true); //write new word to history
BufferedWriter fbw = new BufferedWriter(fileWriter);
fbw.write(word);
insert(word + "$", suggestionRoot); //add $ delimiter for trie purposes
fbw.newLine();
fbw.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
public void setMap(Map<String, Integer> history) throws IOException {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("user_history.txt"));
String word;
while ((word = bufferedReader.readLine()) != null) //this is called at the start of every run
{
if (history.containsKey(word))
history.replace(word, history.get(word), (history.get(word) + 1)); //build key map to track frequency
else
history.put(word, 1);
}
} catch (FileNotFoundException f) {
System.out.println("File not Found");
}
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
}