forked from Ntsekees/ilmentufa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathilmentufa_postproc.js
364 lines (334 loc) · 12.3 KB
/
ilmentufa_postproc.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* CAMXES.JS POSTPROCESSOR
* Created by Ilmen (ilmen.pokebip <at> gmail.com) on 2013-08-16.
* Last change: 2014-01-26.
*
* Entry point: camxes_postprocessing(text, mode)
* Arguments:
* -- text: [string] camxes' raw output
* -- mode: [uint] output mode flag
* 0 = Raw output (no change)
* 1 = Condensed
* 2 = Prettified
* 3 = Prettified + selma'o
* 4 = Prettified + selma'o + bridi parts
* 5 = Prettified - famyma'o
* 6 = Prettified - famyma'o + selma'o
* 7 = Prettified - famyma'o + selma'o + bridi parts
* Return value:
* [string] postprocessed version of camxes' output
*/
/*
* Function list:
* -- camxes_postprocessing(text, mode)
* -- erase_elided_terminators(str)
* -- delete_superfluous_brackets(str)
* -- prettify_brackets(str)
* -- is_string(v)
* -- str_print_uint(val, charset)
* -- str_replace(str, pos, len, sub)
* -- chr_check(chr, list)
* -- dbg_bracket_count(str)
*/
alert = console.log;
var glossfallback = {};
var xmlDoc;
function camxes_postprocessing(text, mode) {
if (mode.format == "rawstructure") return JSON.stringify(text);
if (mode.format == "raw") return JSON.stringify(remove_structure(text));
if (mode.format == "brackets") return prettify_brackets(bracket(text, mode));
if (mode.format == "text") return bracket(text, mode);
if (mode.format == "gloss") return prettify_brackets(gloss(text, glossfallback));
throw "invalid mode";
}
function prettify_brackets(str) {
var open_brackets = ["(", "[", "{", "<"];
var close_brackets = [")", "]", "}", ">"];
var brackets_number = 4;
// var numset = ['0','1','2','3','4','5','6','7','8','9'];
var numset = ['\u2070','\u00b9','\u00b2','\u00b3','\u2074',
'\u2075','\u2076','\u2077','\u2078','\u2079'];
var i = 0;
var floor = 0;
while (i < str.length) {
if (str[i] == '[') {
var n = floor % brackets_number;
var num = (floor && !n) ?
str_print_uint(floor / brackets_number, numset) : "";
str = str_replace(str, i, 1, open_brackets[n] + num);
floor++;
} else if (str[i] == ']') {
floor--;
var n = floor % brackets_number;
var num = (floor && !n) ?
str_print_uint(floor / brackets_number, numset) : "";
str = str_replace(str, i, 1, num + close_brackets[n]);
}
i++;
}
return str;
}
function remove_structure(obj) {
if (!obj) return obj;
if (obj.structure && Array.isArray(obj.structure)) obj.structure.forEach(remove_structure);
delete obj.structure;
if (typeof obj == "object")
Object.keys(obj).map(function (a) { return obj[a] }).forEach(remove_structure);
return obj;
}
function bracket(array, mode) {
function _bracket(array) {
var ret = "";
if (Array.isArray(array)) {
array = array.filter(function (a) { return a && (!Array.isArray(a) || a.length) && (mode.f || !a.elided) });
if (array.length == 1) return _bracket(array[0]);
else if (mode.format == "text") return array.map(_bracket).join(" ");
else return "[" + array.map(_bracket).join(" ") + "]";
}
if (typeof array == "object" && array.structure) {
if (mode.p && array.se_table)
ret += "[" + array.se_table + "]:";
ret += _bracket(array.structure);
if (mode.f) ret += _fuhe(array);
if (mode.format == "text") ret = _punctuate(array, ret);
return ret;
}
if (typeof array == "object" && array.word) {
ret = _bracket(array.elided ? array.word.toUpperCase() : array.word) + _fuhe(array);
return mode.s && array.selmaho && !array.elided ? array.selmaho + ":" + ret : ret;
}
if (typeof array == "string")
return array;
return "[???]";
}
function _fuhe(array) {
return array.fuhe ? "_" + array.fuhe : "";
}
function _punctuate(array, text) {
if (array.selmaho == "ZOhU") {
return text + ":";
} else if (array.clause == "free_sei") {
return "–" + text + "–";
} else if (array.clause == "free_to") {
return "(" + text + ")";
} else if (array.clause == "lu" || array.selmaho == "LOhU") {
return "«" + text + "»";
}
return text;
}
return _bracket(array);
}
function _empty(array) {
return array && (!Array.isArray(array) || array.length) && array;
}
function _normalize(string) {
if (string == null) return null;
return string.replace(/[,.:«»–()]/g, "")
.replace(/à/g, "a")
.replace(/è/g, "e")
.replace(/[ìĭ]/g, "i")
.replace(/ò/g, "o")
.replace(/[ùŭ]/g, "u")
;
}
function gloss(array, words) {
var remove = {
ku: 1, ke: 1, "ke'e": 1, vau: 1, "ku'o": 1, kei: 1, boi: 1,
};
var unconditional = {
lu: "«", "li'u": "»", "lo'u": "«", "le'u": "»",
to: "(", toi: ")", sei: "(", "se'u": ")",
};
var english = {
"lo": "a(n)", "le": "the", "la": "that-named", "nu": "event-of", "zo": "the-word:", "coi": "hello", "co'o": "goodbye", "ro": "each-of", "ma": "what", "na": "not", "na'e": "not", "nai": "-not", "nelci": "fond-of", "ka": "being", "tu'a": "about", "ie": "yeah", "e'u": "I-suggest", "e": "and", "a": "and/or", "je": "and", "ja": "and/or", "gi'e": ",-and", "gi'a": ",-and/or", "bu'u": "at", "ca": "at-present", "zo'u": ":", "za'a": "as-I-can-see", "za'adai": "as-you-can-see", "pu": "in-past", "ba": "in-future", "vau": "]", "doi": "oh", "uinai": "unfortunately", "u'u": "sorry", "ko": "do-it-so-that-you", "poi": "that", "noi": ", which", "me": "among", "pe'i": "in-my-opinion", "ui": "yay", "ju": "whether-or-not", "gu": "whether-or-not", "gi'u": "whether-or-not", "u": "whether-or-not", "xu": "is-it-true-that", "ka'e": "possibly-can", "re'u": "time", "roi": "times", "mi": "me",
};
function trygloss (string) {
var cnt = xmlDoc.get("/dictionary/direction[1]/valsi[translate(@word,\""+string.toUpperCase()+"\",\""+string+"\")=\""+string+"\"]/glossword[1]");
if (cnt) return cnt.attr("word").value();
}
function trykeyword (string, place) {
var cnt = xmlDoc.get("/dictionary/direction[1]/valsi[translate(@word,\""+string.toUpperCase()+"\",\""+string+"\")=\""+string+"\"]/keyword[@place=\"" + (place || 1) + "\"]");
if (cnt) return cnt.attr("word").value();
}
function _(string) { // translation shield
return string && {translated: string};
}
function _bridi_tail(tail, upper_tail_terms_pre) {
if (tail.left && tail.right) {
var upper_tail_terms_all = [tail.tail_terms].concat(upper_tail_terms_pre).filter(_empty);
var left = _bridi_tail(tail.left, upper_tail_terms_all);
var right = _bridi_tail(tail.right, upper_tail_terms_all);
return [left, tail.jek, tail.bo_tag, tail.bo, right].filter(_empty);
}
var preterms = _terms(tail.preterms, tail.selbri);
var tail_terms = _terms(tail.tail_terms, tail.selbri);
if (upper_tail_terms_pre)
var upper_tail_terms = upper_tail_terms_pre.map(function (a) { return _terms(a, tail.selbri) });
if (upper_tail_terms || tail.fa_after_tail && !(1 in tail.fa_after_tail.used))
return [preterms, _selbri_first(tail.selbri), tail_terms].concat(upper_tail_terms).filter(_empty);
else
return [preterms, tail_terms].concat(upper_tail_terms).filter(_empty);
}
function _terms(terms, selbri) {
if (!terms || !terms.terms) return null;
if (terms.terms.terms) terms = terms.terms; // XXX
return terms.terms.map(function (term) {
return _term(term, selbri);
});
}
function _selbri_word(selbri) {
// XXX HACK HACK
if (selbri && selbri._selbri_word)
return selbri._selbri_word;
while (selbri && selbri.tertau)
selbri = selbri.tertau;
if (!selbri) return null;
selbri._selbri_word = selbri.word || _empty(selbri.structure.filter(function (a) { return typeof a == "string" })) || "SELBRI-NOT-FOUND";
return selbri._selbri_word;
}
function _term(term, selbri) {
if (term.right) {
return _conjunction(term, _term, selbri);
} else if (term.terms) {
return _terms(term, selbri);
} else {
var fa = _empty(term.tag && term.tag.fa);
var selbri_fa = fa && selbri && selbri.se_table && selbri.se_table[fa[0] - 1];
var selbri_word = _selbri_word(selbri); // XXX don't run this for every term
var placetable = glossfallback[_normalize(selbri_word)];
var ret;
if (!fa || !selbri_fa || !placetable) {
ret = null;
} else if (selbri_fa == 1) {
//ret = term.sumti
ret = [term.sumti, _(placetable.bridi1post)].filter(_empty);
} else if (selbri_fa == 2) {
ret = [_(placetable.bridi2), term.sumti, _(placetable.bridi2post)].filter(_empty);
} else {
ret = [_(placetable.bridirest[selbri_fa - 3]), term.sumti].filter(_empty);
}
return _empty(ret) || (selbri_fa && selbri_word && [selbri_word + selbri_fa, term.sumti]) || term;
}
}
function _selbri_first(selbri) {
var selbri_word = _selbri_word(selbri);
var placetable = glossfallback[selbri_word];
if (placetable)
return placetable.bridi1post;
return selbri_word || selbri;
}
function _selbri_noun(selbri) {
if (!selbri) return selbri;
if (selbri.left && selbri.right) {
var left = _selbri_noun(selbri.left);
var right = _selbri_noun(selbri.right);
return [left, selbri.jek, selbri.bo_tag, selbri.bo, right].filter(_empty);
}
if (selbri.tertau) {
}
var selbri_fa = selbri.se_table[0];
var selbri_word = null;
}
function _conjunction(terjoma, fun, args) {
if (terjoma.right) {
if (terjoma.left) terjoma.left = fun.call(null, args ? [terjoma.left].concat(args) : terjoma.left);
terjoma.right = fun.call(null, args ? [terjoma.right].concat(args) : terjoma.right);
return terjoma;
}
return fun(terjoma);
}
function _transform(array) {
//if (array.word in remove)
//array.gloss_elided = true;
if (array.role == "bridi") {
//return JSON.stringify(remove_structure(array));
if (array.head) array.head = _terms(array.head, array.tail.selbri);
array.tail = _bridi_tail(array.tail);
//return JSON.stringify(remove_structure([array.head, array.tail].filter(_empty)));
return [array.head, array.tail].filter(_empty);
}
if (array.role + "DISABLE" == "relativized_selbri") {
if (!array.selbri) return array;
return [array.quantifier, _selbri_noun(array.selbri), array.relative].filter(_empty);
}
if (array.role == "precedence") {
return array.contents;
}
return array;
}
function _bracket(array) {
array = _transform(array);
if (Array.isArray(array)) {
array = array.filter(function (a) { return a && (!Array.isArray(a) || a.length) && !a.gloss_elided && !(a.word in remove) && !(a in remove); });
if (array.length == 1) return _bracket(array[0]);
else return "[" + array.map(_bracket).join(" ") + "]";
}
if (typeof array == "object" && array.translated) {
return array.translated;
}
if (typeof array == "object" && array.structure) {
return _bracket(array.structure);
}
if (typeof array == "object" && array.word) {
return _bracket(array.word);
}
if (typeof array == "string") {
array = _normalize(array);
return unconditional[array] || english[array] || trygloss(array) || array;
}
console.log(JSON.stringify(array));
return "[???]";
}
return _bracket(array);
}
/* ================== */
/* === Routines === */
/* ================== */
function is_string(v) {
return typeof v.valueOf() === 'string';
}
function str_print_uint(val, charset) {
// 'charset' must be a character array.
var radix = charset.length;
var str = "";
val -= val % 1; // No float allowed
while (val >= 1) {
str = charset[val % radix] + str;
val /= radix;
val -= val % 1;
}
return str;
}
function str_replace(str, pos, len, sub) {
if (pos < str.length) {
if (pos + len >= str.length) len -= pos + len - str.length;
return str.substring(0, pos) + sub + str.substring(pos + len);
} else return str;
}
function chr_check(chr, list) {
var i = 0;
if (!is_string(list)) return false;
do if (chr == list[i]) return true; while (i++ < list.length);
return false;
}
function dbg_bracket_count(str) {
var i = 0;
var x = 0;
var y = 0;
while (i < str.length) {
if (str[i] == '[') x++;
else if (str[i] == ']') y++;
i++;
}
alert("Bracket count: open = " + x + ", close = " + y);
}
module.exports.postprocessing = camxes_postprocessing;
module.exports.prettify = prettify_brackets;
module.exports.remove_structure = remove_structure;
module.exports.loadgloss = function (a, b) { glossfallback = a; xmlDoc = b; };
module.exports.normalize = _normalize;
if (typeof process !== 'undefined' && require !== 'undefined' && require.main === module) {
var fs = require('fs');
var input = process.argv.length > 2 ? process.argv[2] : fs.readFileSync(0, 'utf-8');
console.log(camxes_postprocessing(input, {format: 'text'}));
}