-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_worker.js
332 lines (269 loc) · 7.85 KB
/
parser_worker.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
"use strict";
const CONTAINERS = ['selbri', 'sumti', 'term', 'free', 'tag', 'bu_clause', 'si_clause', 'ZOI_clause', 'any_word'];
const ZANTUFA_ELEMENTS = ['BRIVLA'];
const ILMENTUFA_ELEMENTS = ['zohoi_word'];
const ELEMENTS = ['gismu', 'lujvo', 'fuhivla', 'cmevla', 'zoi_word'].concat(ZANTUFA_ELEMENTS).concat(ILMENTUFA_ELEMENTS);
const HEAD = ['LU', 'LIhU', 'TO', 'TOI'];
const DELETE = ['spaces', 'initial_spaces'];
/* This function returns the string resulting from the recursive concatenation
* of all the leaf elements of the parse tree argument (except node names). */
// "join_leaves" or "flatten_tree" might be better names.
function join_expr(n) {
if (n.length < 1) {
return "";
}
var s = "";
var i = Array.isArray(n[0]) ? 0 : 1;
while (i < n.length) {
s += (typeof n[i] == "string") ? n[i] : join_expr(n[i]);
i++;
}
return s;
}
function is_selmaho(v) {
if (typeof v !== "string") {
return false;
}
return (0 == v.search(/^[IUBCDFGJKLMNPRSTVXZ]?([AEIOUY]|(AI|EI|OI|AU))(h([AEIOUY]|(AI|EI|OI|AU)))*$/g));
}
// fi'e la .ilmen.
function process_parse_tree(parse_tree, value_substitution_map, name_substitution_map, node_action_for, must_prefix_leaf_labels) {
if (parse_tree.length == 0) {
return null;
}
var action = node_action_for(parse_tree);
if (action == 'DEL') {
return null; // Deleting the current branch.
}
var has_name = typeof parse_tree[0] == "string";
// Getting the value replacement for this node, if any.
var substitution_value = (has_name) ? value_substitution_map[parse_tree[0]] : undefined;
if (has_name) {
if (action == 'TRIM') {
/* If there's a value replacement for this node, we return it instead of the node's content. */
if (typeof substitution_value !== 'undefined') {
return substitution_value;
}
/* Otherwise the first step of a trim action is to remove the node name. */
parse_tree.splice(0, 1);
has_name = false;
} else {
/* No trimming, so let's see if the node name is in the renaming list. If so, let's rename it accordingly. */
var v = name_substitution_map[parse_tree[0]];
if (typeof v !== 'undefined') {
parse_tree[0] = v;
}
/* If there's a value replacement for this node, it becomes the unique value for the node. */
if (typeof substitution_value !== 'undefined') {
return [parse_tree[0], substitution_value];
}
}
}
if (action == 'FLAT') {
/*
* Flattening action. All the terminal nodes of the branch are concatenated,
* and the concatenation result replaces the branch's content, alongside the node name if any.
* If the concatenation result is empty, the branch becomes empty.
*/
var r = join_expr(parse_tree);
if (has_name && r != "") {
if (must_prefix_leaf_labels) {
return parse_tree[0] + ':' + r;
} else {
return [parse_tree[0], r];
}
} else if (has_name) {
return parse_tree[0];
} else {
return r;
}
} else if (action == 'TRIMFLAT') {
return join_expr(parse_tree);
}
/* Now we'll iterate over all the other elements of the current node. */
var i = has_name ? 1 : 0;
while (i < parse_tree.length) {
if (Array.isArray(parse_tree[i])) {
/* Recursion */
parse_tree[i] = process_parse_tree(
parse_tree[i],
value_substitution_map,
name_substitution_map,
node_action_for,
must_prefix_leaf_labels
);
}
/* The recursion call on the current element might have set it to null as a request for deletion. */
if (parse_tree[i] === null) {
parse_tree.splice(i, 1);
} else {
i += 1; // No deletion, so let's go to the next element.
}
}
/*
* Now we've finished iterating over the node elements. Let's proceed to the final steps.
* If 'must_prefix_leaf_labels' is set and the node has a name and contains at least one other element, we append ':' to its name.
* If the node is empty, we return null as a signal for deletion.
* If the node contains only one element and we want to trim the node, it gets replaced by its content.
*/
if (i == 0) {
return null;
} else if (i == 1 && action != 'PASS') {
return parse_tree[0];
} else if (must_prefix_leaf_labels && i == 2 && has_name && typeof parse_tree[1] == "string") {
if (!parse_tree[1].includes(":")) {
return parse_tree[0] + ':' + parse_tree[1];
} else {
parse_tree[0] += ":";
}
}
return parse_tree;
}
function node_action_for(tree) {
const name = tree[0];
const has_name = typeof name == "string";
if (has_name) {
if (CONTAINERS.indexOf(name) > -1) { // save these from being trimmed/flattened/deleted
return "PASS";
}
if (tree.length == 1 || DELETE.indexOf(name) > -1) { // eliminates empty 'initial_spaces'
return "DEL";
}
if (is_selmaho(name) || ELEMENTS.indexOf(name) > -1) {
return "FLAT";
}
}
return 'TRIM';
}
function simplify(tree) {
console.assert(Array.isArray(tree) && tree[0] == "text");
return process_parse_tree(tree, {}, {}, node_action_for, false) || ['text', ['initial_spaces', '']];
}
// used to remove the {i} which is prepended to all parsed sentences
function shiftFirstWord(tree) {
while (typeof tree[1] != "string") {
if (Array.isArray(tree[0])) {
tree = tree[0];
} else {
tree = tree[1];
}
}
return tree.splice(0, 2);
}
const build = tree => {
if (tree.length == 0) {
return tree;
}
if (Array.isArray(tree[0])) {
return tree.map(build).reduce((a, b) => a.concat(b));
}
if (typeof tree[1] == 'string') {
if (HEAD.indexOf(tree[0]) > -1) {
return [
{
outerHTML: "<link-head-temp>"
},
{
outerHTML: tree[1],
hasTextContent: true
},
{
outerHTML: "</link-head-temp>",
isRightTag: true
}
];
}
return [
{
outerHTML: "<word data-type='" + tree[0] + "'>"
},
{
outerHTML: tree[1],
hasTextContent: true,
},
{
outerHTML: "</word>",
isRightTag: true
}
];
}
return [
{
outerHTML: "<" + tree[0] + ">"
},
...tree.slice(1).map(build).reduce((a, b) => a.concat(b)),
{
outerHTML: "</" + tree[0] + ">",
isRightTag: true
}
];
};
const convert = tree => {
simplify(tree);
shiftFirstWord(tree);
const linearXML = build(tree);
return linearXML;
};
function isLexingEquivalent(converted, tokens) {
const convertedWords = converted
.filter(x => x.hasTextContent)
.map(x => x.outerHTML);
const tokensWords = tokens.filter(x => x.search(/\w/) > -1);
if (convertedWords.length !== tokensWords.length) {
console.log(converted);
console.log(convertedWords, tokensWords);
return false;
}
for (let i = 0; i < tokensWords.length; i++) {
if (tokensWords[i] !== convertedWords[i]) {
return false;
}
}
return true;
}
const makeSpacesHTML = str => "<spaces>" + str + "</spaces>";
const interleave = (converted, tokens) => {
let lastRightTag = {outerHTML: ''};
const rtn = [lastRightTag];
for (let item of converted) {
rtn.push(item);
if (item.isRightTag) {
lastRightTag = item;
} else if (item.hasTextContent) {
while (tokens[0] != item.outerHTML) {
lastRightTag.outerHTML += makeSpacesHTML(tokens.shift()); // will be WS
}
tokens.shift();
}
}
while (tokens.length > 0) {
lastRightTag.outerHTML += makeSpacesHTML(tokens.shift());
}
return rtn;
};
// easier to convert into XML as soon as possible?
self.onmessage = function(event) {
const [parserName, tokens] = event.data;
if (eval("typeof " + parserName) === "undefined") {
importScripts("/parsers/" + parserName + ".js");
}
try {
const unshiftedTokens = ["i"].concat(tokens); // will fix inputs like "babo klama"
const rawOutput = eval(parserName + ".parse(unshiftedTokens.join(''))");
const converted = convert(rawOutput);
if (!isLexingEquivalent(converted, tokens)) {
throw new Error("vlakorfa'i srera");
}
const interleaved = interleave(converted, tokens);
postMessage({
hasError: false,
xml: interleaved.map(x => x.outerHTML).join('')
});
} catch(exception) {
postMessage({
hasError: true,
errorMessage: exception.message,
errorOffset: exception.offset
});
}
};