-
Notifications
You must be signed in to change notification settings - Fork 12
/
Dictionary.java
executable file
·327 lines (301 loc) · 12.4 KB
/
Dictionary.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.TreeMap;
import java.util.NavigableMap;
import java.util.Map;
import java.util.Collection;
public class Dictionary {
private MultiTreeMap<String> english = new MultiTreeMap<String>(); // One translation can have multiple strokes, and keys are case insensitive.
private NavigableMap<String, String> definitions = new TreeMap<String, String>(); // But one stroke can have only one translation
// Constructor
// Builds lookup and reverse-lookup symbol tables from .json dictionary file
public Dictionary(List<String> dictionaryFiles) {
for (String filename : dictionaryFiles) {
loadDictionary(filename);
}
}
public void loadDictionary(String filename) {
if (filename == null || filename.equals("")) {
throw new java.lang.IllegalArgumentException();
}
String stroke;
String translation;
String line = "";
String[] fields;
try {
BufferedReader file = new BufferedReader(
new InputStreamReader(new FileInputStream(filename), "utf-8"));
while ((line = readLine(file)) != null) {
fields = line.split("\""); // TODO: This is buggy if text contains a quote. Should parse actual JSON.
if ((fields.length >= 4) && (fields[3].length() > 0)) {
stroke = fields[1];
translation = fields[3];
english.putSingle(translation, stroke); // This is the MultiTreeMap, so any duplicate keys get added to the list. This is technically buggy if you use multiple dictionaries and some entries are overwritten. TODO
definitions.put(stroke, translation); // This is the normal TreeMap, so any duplicate keys get overwritten by the last key.
}
}
file.close();
} catch (IOException e) {
System.err.println("Could not find file: "+filename);
}
}
// A class to allow managing stroke/translation pairs
public class Pair {
private String s;
private String translation;
public Pair(String st, String t) {
s = st;
translation = t;
}
public String translation() { return translation; }
public String stroke() { return s; }
}
/*
// spell out a word letter-by-letter
public String fingerspell(String word) {
if (word.length() == 0)
throw new java.lang.IllegalArgumentException();
String result = "";
result = english.get("{&" + word.substring(0,1) + "}").shortest();
for (int i = 1; i < word.length(); i++) {
result += "/" + english.get("{&" + word.substring(i,i+1) + "}").shortest();
}
return result;
}
// look up shortest stroke for a word or phrase
// taking into account multi-word strokes
public String lookup(String phrase) {
if (phrase.trim().length() == 0)
throw new java.lang.IllegalArgumentException();
phrase = phrase.trim();
String result = "";
String word ;
while (phrase.length() > 0) {
word = english.longestPrefixOf(phrase);
// if longest prefix is multiple words, then use it
if (containsSpaces(word) && (phrase.charAt(word.length()) == ' ')) {
result += english.get(word).shortest();
} else {
word = getFirstWord(phrase);
if (english.contains(word))
result += english.get(word).shortest();
else
result += buildUp(word);
}
phrase = phrase.substring(word.length(),phrase.length());
// eliminate autospaces if necessary
if ((phrase.length() > 0) && (phrase.length() == phrase.trim().length())) {
result += "/TK-LS/";
} else {
phrase = phrase.trim();
result += "/";
}
}
return result;
}
// look up the English translation of a stroke or series of strokes
public String translate(String strokes) {
if (strokes.trim().length() == 0)
throw new java.lang.IllegalArgumentException();
strokes = strokes.trim();
String result = "";
String stroke, translation;
Boolean insertSpace;
while (strokes.length() > 0) {
stroke = definitions.longestPrefixOf(strokes);
if (stroke.length() == 0) return "";
insertSpace = true;
translation = definitions.get(stroke);
if (strokes.equals(stroke)) {
strokes = "";
} else {
strokes = strokes.substring(stroke.length()+1,strokes.length());
}
if (isMacro(translation)) {
translation = translation.substring(1,translation.length()-1); // remove braces
if ((translation.charAt(0) == '^') || (translation.charAt(0) == '&')) {
translation = translation.substring(1);
result = result.trim();
}
if (translation.charAt(translation.length()-1) == '^') {
translation = translation.substring(0,translation.length()-1);
insertSpace = false;
}
}
result += translation;
if (insertSpace) result += " ";
}
return result;
}
// return all defined words/phrases that start with what we have already
// returns an iterable sorted from shortest to longest
// ie. fingerspell part of a word to autoLookup the correct stroke
// public Iterable<Pair> autoLookup(String prefix) {
// List<Pair> result = new ArrayList<Pair>();
// if (prefix.length() <= 2) return result; // don't do lookup for short prefixes
// for (String phrase : english.prefixMatch(prefix)) {
// result.add(new Pair(english.get(phrase),phrase));
// }
// Collections.sort(result,new ByPhraseLength());
// return result;
// }
*/
public Iterable<Pair> autoLookup(String stringPrefix, String strokePrefix) {
List<Pair> result = new ArrayList<Pair>();
if (stringPrefix.length() > 2) { // don't do lookup for short prefixes
for (Collection<Tuple<String, String>> pairs : prefixMatch(english, stringPrefix).values()) {
for (Tuple<String, String> pair : pairs) {
result.add(new Pair(pair.y,pair.x));
}
}
}
if (strokePrefix.length() >= 2) {
if (strokePrefix.charAt(strokePrefix.length()-1) != '/')
strokePrefix += "/"; // ensure the stroke is complete
for (Map.Entry<String, String> pair : prefixMatch(definitions, strokePrefix).entrySet()) {
result.add(new Pair(pair.getKey(), pair.getValue()));
}
}
Collections.sort(result,new ByPhraseLength());
return result;
}
// PRIVATE CLASSES AND METHODS
private <T2> NavigableMap<String, T2> prefixMatch(NavigableMap<String, T2> m, String prefix) {
return m.subMap(prefix, true, prefix+"\uffff", true);
}
private static class ByPhraseLength implements Comparator<Pair> {
public int compare(Pair p1, Pair p2) {
if ((p1 == null) || (p2 == null))
throw new java.lang.IllegalArgumentException();
if (p1.equals(p2)) return 0;
int length1 = phraseLength(p1.translation());
int length2 = phraseLength(p2.translation());
if (length1 == length2) {
length1 = strokeLength(p1.stroke());
length2 = strokeLength(p2.stroke());
}
return (length1 - length2);
}
}
private static class ByStrokeLength implements Comparator<Pair> {
public int compare(Pair p1, Pair p2) {
if ((p1 == null) || (p2 == null))
throw new java.lang.IllegalArgumentException();
if (p1.equals(p2)) return 0;
int length1 = strokeLength(p1.stroke());
int length2 = strokeLength(p2.stroke());
if (length1 == length2) {
length1 = phraseLength(p1.translation());
length2 = phraseLength(p2.translation());
}
return (length1 - length2);
}
}
// count the number of slashes in a string
private static int countSlashes(String s) {
int result = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '/') result++;
}
return result;
}
// read a line from a file, catching errors
private static String readLine(BufferedReader file) {
String result = "";
try {
result = file.readLine();
} catch (IOException e) {
System.err.println("Error reading from file");
}
return result;
}
/*
// use fingerspelling, prefixes and suffixes to build an efficient way to
// stroke words not found in the dictionary
private String buildUp(String word) {
String prefix = "";
String suffix = "";
int len;
Pair pair = findPrefix(word);
if (pair != null) {
prefix = pair.stroke() + "/";
len = pair.translation().length()-3; // -3 for meta characters {^}
word = word.substring(len,word.length());
}
pair = findSuffix(word);
if (pair != null) {
suffix = "/" + pair.stroke();
len = pair.translation().length()-3; // -3 for meta characters {^}
word = word.substring(0,(word.length()-len));
}
String result = prefix + fingerspell(word) + suffix;
return result;
}
// return the longest prefix stroke (ends with ^)
private Pair findPrefix(String word) {
StrokeSet strokes;
String paddedWord;
for (int i = 1; i < word.length(); i++) {
paddedWord = "{"+word.substring(0,word.length()-i)+"^}";
strokes = english.get(paddedWord);
if (strokes != null)
return new Pair(strokes,paddedWord);
}
return null;
}
// return the longest suffix (starts with ^)
private Pair findSuffix(String word) {
StrokeSet strokes;
String paddedWord;
for (int i = 1; i < word.length(); i++) {
paddedWord = "{^"+word.substring(i,word.length())+"}";
strokes = english.get(paddedWord);
if (strokes != null)
return new Pair(strokes,paddedWord);
}
return null;
}
private static boolean containsSpaces(String word) {
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == ' ') return true;
}
return false;
}
private static String getFirstWord(String phrase) {
String result = "";
for (int i = 0; i < phrase.length(); i++) {
if (phrase.charAt(i) == ' ') return result;
else result += phrase.charAt(i);
}
return result;
}
private static Boolean isMacro(String word) {
return ((word.charAt(0) == '{') && (word.charAt(word.length()-1) == '}'));
}
*/
private static int strokeLength(String stroke) {
return stroke.split("/").length * 100 + stroke.length();
}
private static int phraseLength(String word) {
return word.split(" ").length * 10 + word.length();
}
public static void main(String[] args) {
List<String> d = new ArrayList<String>();
d.add("/home/brentn/Dropbox/Plover/gbDict2.json");
Dictionary dictionary = new Dictionary(d);
// System.out.println(dictionary.lookup("Unicyclist"));
// System.out.println(dictionary.buildUp("interloping"));
// System.out.println(dictionary.lookup("interloping is what it's all about"));
// System.out.println(dictionary.translate("SPWR/HR*/O*/P*/-G/S/WHA/T-S/AUL/P/"));
for (Pair p : dictionary.autoLookup("unic","HREUL")) {
System.out.println(p.translation()+ " --> "+p.stroke());
};
//System.out.println(dictionary.shorter("HRAEUD/EUS/STK/SKWRE/PHEPB/-F/-T/SKWRUR"));
}
}