-
Notifications
You must be signed in to change notification settings - Fork 3
/
vortaro.js
251 lines (210 loc) · 6.8 KB
/
vortaro.js
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
// @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2
// vim: set ts=4 sts=4 sw=4 et:
'use strict';
// ES6 polyfill.
if (!String.prototype.includes) {
String.prototype.includes = function() {
'use strict';
return String.prototype.indexOf.apply(this, arguments) !== -1;
};
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
}
// Definition of a result from the search functions.
var MatchResults = function(dict, exact, inexact) {
// Dictionary array for the indices.
this.dict = dict;
// Arrays of indices into a dictionary, sorted in the order of intended display.
this.exactMatches = exact;
this.inexactMatches = inexact;
}
MatchResults.prototype.isEmpty = function() {
return (this.exactMatches.length + this.inexactMatches.length === 0);
}
// Is a given match exact or inexact?
// All exact matches precede all inexact matches as seen by get().
MatchResults.prototype.isExact = function(i) {
return (i < this.exactMatches.length);
}
MatchResults.prototype.get = function(i) {
var k = this.exactMatches.length;
if (i < k) {
return this.exactMatches[i];
}
return this.inexactMatches[i - k];
}
MatchResults.prototype.length = function() {
if (this.exactMatches === undefined) {
return 0;
}
return (this.exactMatches.length + this.inexactMatches.length);
}
// Main search function.
function search(term, dict, dict_lower) {
term = term.trim();
term = normalize_english(term);
if (term.length === 0) {
return new MatchResults;
}
// This breaks for English words with "ux", but... that's OK.
term = xreplace(term);
var results = search_exact(term, dict, dict_lower);
if (results.length() !== 0) {
return results;
}
// If no results were found, try fixing input to a standard dictionary form.
var normalized = normalize_suffix(term);
if (normalized !== term) {
return search_exact(normalized, dict, dict_lower);
}
return new MatchResults;
}
// Normalize "to foo" => "foo".
function normalize_english(string) {
// Safari 8 doesn't have startsWith.
if (string.substr(0,3) === 'to ') {
return string.substr(3);
}
return string;
}
// Support the x-system.
function xreplace(text) {
var pairs = [
['cx', 'ĉ'], ['cX', 'ĉ'], ['Cx', 'Ĉ'], ['CX', 'Ĉ'],
['gx', 'ĝ'], ['gX', 'ĝ'], ['Gx', 'Ĝ'], ['GX', 'Ĝ'],
['hx', 'ĥ'], ['hX', 'ĥ'], ['Hx', 'Ĥ'], ['HX', 'Ĥ'],
['jx', 'ĵ'], ['jX', 'ĵ'], ['Jx', 'Ĵ'], ['JX', 'Ĵ'],
['sx', 'ŝ'], ['sX', 'ŝ'], ['Sx', 'Ŝ'], ['SX', 'Ŝ'],
['ux', 'ŭ'], ['uX', 'ŭ'], ['Ux', 'Ŭ'], ['UX', 'Ŭ']
];
for (var i = 0; i < pairs.length; ++i) {
text = text.replace(new RegExp(pairs[i][0], 'g'), pairs[i][1]);
}
return text;
}
// Given an esperanto word, try to get a standard form.
function normalize_suffix(word) {
var suffices = [
['as', 'i'],
['os', 'i'],
['is', 'i'],
['us', 'i'],
['u', 'i'],
['oj', 'o'],
['ojn', 'o'],
['on', 'o'],
['aj', 'a'],
['ajn', 'a'],
['an', 'a'],
['en', 'e']
];
for (var i = 0; i < suffices.length; ++i) {
if (word.endsWith(suffices[i][0])) {
return word.slice(0, -1 * suffices[i][0].length) + suffices[i][1];
}
}
return word;
}
// Given an esperanto word subject to normal grammar rules, get the root.
function getroot(word) {
word = normalize_suffix(word);
// Remove the function indicator.
var lastchar = word[word.length - 1]
if (lastchar === 'a' || lastchar === 'i' || lastchar === 'o' || lastchar === 'e') {
word = word.slice(0, -1);
}
// Get out of participle form.
var suffices = [
'ant', 'ont', 'int', 'unt', 'at', 'ot', 'it', 'ut'
];
for (var i = 0; i < suffices.length; ++i) {
if (word.endsWith(suffices[i])) {
word = word.slice(0, -1 * suffices[i].length);
break;
}
}
// Attempt to remove suffixes.
// TODO: This logic can't be right for all cases; really, we need to discover
// all the possible cuts of the word.
if (word.endsWith("ig") || word.endsWith("iĝ")) {
word = word.slice(0, -2);
}
return word;
}
// Remove a parenthesis pair.
// A search for "paragraph" should return "paragraph (text)".
function remove_parentheses(text) {
var paren = text.indexOf(' (');
if (paren === -1) {
return text;
}
if (paren === 0) {
return text.substr(text.indexOf(')') + 1);
}
return text.substr(0, paren);
}
function is_exact_match(entry, search) {
var v = normalize_english(entry);
v = remove_parentheses(v);
return (v === search);
}
// Returns MatchResults into a dictionary.
function search_exact(word, dict, dict_lower) {
var inexactmatches = [];
var exactmatches = [];
var i, j;
var entry;
var match = false;
var exactmatch = false;
var lowerWord = word.toLowerCase();
// Find all potentially matching candidates quickly.
// Does not care whether the entry was in English or Esperanto.
for (i = 0; i < dict.length; ++i) {
entry = dict_lower[i];
match = false;
exactmatch = false;
for (j = 0; j < entry.length; ++j) {
// FirefoxOS 2.2 doesn't support ES6 includes().
if (entry[j].includes(lowerWord)) {
match = true;
exactmatch = is_exact_match(entry[j], lowerWord);
if (exactmatch === true) {
break;
}
}
}
// Sort each entry into either exactmatches or inexactmatches,
// preferring exactmatches.
if (exactmatch === true) {
exactmatches.push(i);
} else if (match === true) {
inexactmatches.push(i);
}
}
return new MatchResults(dict, exactmatches, inexactmatches);
}
// Given an esperanto word, look up an etymology.
function find_etymology(word) {
// The etymology dictionary is entirely in lowercase.
var lowerWord = word.toLowerCase();
// Look for the word directly.
for (var i = 0; i < etymology.length; ++i) {
if (etymology[i][0] === lowerWord) {
return etymology[i][1];
}
}
// If it wasn't found, try to compare roots.
// We can't do this in the first place because of exceptions.
// For example, "tri" and "tro" have distinct etymologies.
var root = getroot(lowerWord);
for (var i = 0; i < etymology.length; ++i) {
if (etymology[i][0].slice(0, -1) === root) {
return etymology[i][1];
}
}
return "";
}
// @license-end