-
Notifications
You must be signed in to change notification settings - Fork 0
/
camxes_postproc.js
executable file
·266 lines (251 loc) · 7.55 KB
/
camxes_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
/*
* 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;
function camxes_postprocessing(text, mode) {
if (!is_string(text)) return "ERROR: Wrong input type.";
if (mode === 0) return text;
if (text.charAt(0) != '[') return text;
/** Condensation **/
text = text.replace(/ +/gm, "");
text = text.replace(/\r\n|\n|\r/gm, "");
if (mode == 1) return text;
/** Prettified forms **/
var with_selmaho = (mode >= 3 && mode != 5);
var with_nodes_labels = (mode == 4 || mode == 7);
var without_terminator = (mode >= 5);
text = text.replace(/\"/gm, ""); // Delete every quote marks
/* Save selmaho and brivla types */
text = text.replace(/(gismu|lujvo|fuhivla|cmene|cmevla),([A-Za-z']+)/g, "$1:$2");
text = text.replace(/([A-Za-z_]+),([A-Za-z']+)/g, "$1:$2");
/* Save a few nodes from deletion (optional) */
if (with_nodes_labels) {
text = text.replace(/prenex,/g, "PN");
text = text.replace(/sentence,\[\[terms,/g, "sentence,[BH[terms,");
text = text.replace(/bridi_tail_2,/g, "BT");
}
/* Delete every remaining node names and commas */
text = text.replace(/([A-Za-z0-9_]+,)+\[/g, "[");
text = text.replace(/,/gm, "");
/* If requested, erase every elided terminators from the output */
if (without_terminator)
text = erase_elided_terminators(text);
/* Cleanup */
text = delete_superfluous_brackets(text);
text = text.replace(/\[ +/g, "[");
text = text.replace(/ +\]/g, "]");
text = text.replace(/([A-Z] )\[/g, "$1[");
/* Abbreviations */
text = text.replace(/(cmene|cmevla):/g, "C:");
text = text.replace(/gismu:/g, "G:");
text = text.replace(/lujvo:/g, "L:");
text = text.replace(/fuhivla/g, "F");
text = text.replace(/BRIVLA/g, "Z");
/* Making things easier to read */
if (!with_selmaho) text = text.replace(/[A-Za-z_]+:/g, "");
text = text.replace(/\]\[/g, "] [");
text = prettify_brackets(text);
text = text.replace(/ +/gm, " ");
text = text.replace(/^ +/, "");
return text;
}
function erase_elided_terminators(str) {
var i, j;
var parachute = 400;
str = str.replace(/([a-z']+)`/g, function(v) { return v.toUpperCase(); });
str = str.replace(/([A-Z]+)'([A-Z]+)`/g,"$1h$2`");
str = str.replace(/([A-Z]+)H([A-Z]+)`/g,"$1h$2`");
str = str.replace(/([A-Zh]+)`/g,"$1");
do {
i = str.search(/\[[A-Zh`]+\]/);
if (i < 0) break;
j = i + str.substr(i).indexOf("]");
while (i-- > 0 && ++j < str.length)
if (str[i] != '[' || str[j] != ']') break;
i++;
j--;
//alert("DEL "+str.substr(i,j-i+1));
str = str_replace(str, i, j-i+1, "");
} while (parachute--);
return str;
}
function delete_superfluous_brackets(string) {
/* RATIONALE:
* Here is done the dirty work of removing all those superfluous brackets
* left after our earlier intense session of regexp replacements.
* Admittedly not quite easy to read, but it does the job. :)
*/
str = string.split("");
var i = 0;
var parachute = 4000; // Against evil infinite loops.
if (str.length < 1) {
alert("ERROR: (str.length < 1) @delete_superfluous_brackets");
return str;
}
// We reach the first word
while (str[i] == "[") {
if (i >= str.length) return; // No word found.
i++;
}
/**
### FIRST CLEANUP: BRACKETS SURROUNDING SINGLE WORD ###
**/
do {
/** erase_surrounding_brackets **/
var j = i;
while (str[i].search(/[A-Za-z'_:~]/) === 0 && i < str.length) i++;
while (str[i] == ']') {
if (j <= 0) {
alert("ERROR: right bracket found without left counterpart.");
break;
}
j--;
while (str[j] == ' ') j--;
if (str[j] == '[') {
str[j] = ' ';
str[i++] = ' ';
} else break;
}
/** reach_next_word **/
while (str[i] == '[' || str[i] == ']' || str[i] == ' ' && i < str.length)
i++;
} while (i < str.length && --parachute > 0);
if (parachute <= 0)
alert("@delete_superfluous_brackets #1: INFINITE LOOP\ni = " + i
+ "\nstr from i:\n" + str.slice(i));
/**
### SECOND CLEANUP: BRACKETS SURROUNDING GROUPS OF WORDS ETC. ###
**/
i = 0;
parachute = 4000;
while (i < str.length && parachute-- > 0) {
var so, eo, jj;
// FIRST STEP: reaching the next '['.
while (i < str.length && str[i] != '[') i++;
so = i;
while (i < str.length && str[i] == '[') i++;
eo = i;
if (i >= str.length) break;
if (i - so < 2) continue;
jj = i;
i -= 2;
do {
var floor;
// Now we'll reach the ']' closing the aformentioned '['.
floor = 1;
while (jj < str.length && floor > 0) {
if (str[jj] == '[') floor++;
else if (str[jj] == ']') floor--;
jj++;
}
// We now erase superfluous brackets
while (str[jj] == ']' && i >= so) {
str[i--] = ' ';
str[jj++] = ' ';
}
} while (i >= so && jj < str.length && parachute-- > 0);
i = eo;
}
if (parachute <= 0)
alert("@delete_superfluous_brackets #2: INFINITE LOOP\ni = " + i
+ "\nstr from i:\n" + str.slice(i));
return str.join("");
}
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 nn = floor % brackets_number;
var numm = (floor && !nn) ?
str_print_uint(floor / brackets_number, numset) : "";
str = str_replace(str, i, 1, numm + close_brackets[nn]);
}
i++;
}
return str;
}
/* ================== */
/* === 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;