-
Notifications
You must be signed in to change notification settings - Fork 15
/
run_camxes.js
149 lines (135 loc) · 4.32 KB
/
run_camxes.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
/*
* USAGE:
* $ nodejs run_camxes -PARSER_ID -m MODE TEXT
* OR
* $ nodejs run_camxes -PARSER_ID -m MODE -t TEXT
* OR
* $ nodejs run_camxes -p PARSER_PATH -m MODE TEXT
* OR
* $ nodejs run_camxes -p PARSER_PATH -m MODE -t TEXT
*
* PARSER_PATH and TEXT being the path of the desired parser engine, and the
* Lojban text to be parsed, respectively.
*
* Possible values for PARSER_ID:
* "std", "beta", "cbm", "ckt", "exp", "morpho"
*
* MODE can be any letter string, each letter stands for a specific option.
* Here is the list of possible letters and their associated meaning:
* 'M' -> Keep morphology
* 'S' -> Show spaces
* 'T' -> Show terminators
* 'C' -> Show word classes (selmaho)
* 'R' -> Raw output, do not trim the parse tree. If this option isn't set,
* all the nodes (with the exception of those saved if the 'N' option
* is set) are pruned from the tree.
* 'N' -> Show main node labels
* 'L' -> Loop. Second 'L' for specifying the mode in the first word.
* Example:
* -m CTN
* This will show terminators, selmaho and main node labels.
*/
var camxes_preproc = require('./camxes_preproc.js');
var camxes_postproc = require('./camxes_postproc.js');
var engine_path = "./camxes.js";
var mode = "";
var text = "";
var target = '-t';
var p = [["-std","./camxes.js"],["-beta","./camxes-beta.js"],["-cbm","./camxes-beta-cbm.js"],
["-ckt","./camxes-beta-cbm-ckt.js"],["-exp","./camxes-exp.js"],["-morpho","./camxes-morpho.js"]];
for (var i = 2; i < process.argv.length; i++) {
if (process.argv[i].length > 0) {
var a = process.argv[i];
if (a[0] == '-') {
for (j = 0; j < p.length; j++) {
if (p[j][0] == a) {
engine_path = p[j][1];
target = '-t';
break;
}
}
if (j == p.length)
target = a;
} else {
switch (target) {
case '-t':
text = a;
break;
case '-p':
engine_path = a;
break;
case '-m':
mode = a;
}
target = '-t';
}
}
}
if (engine_path.length > 0 && !(engine_path[0] == '/' || engine_path.substring(0, 2) == './'))
engine_path = './' + engine_path;
try {
var engine = require(engine_path);
} catch (err) {
process.stdout.write(err.toString() + '\n');
process.exit();
}
var mode_loop = among_count('L', mode);
if (mode_loop) {
mode = mode.replace('L','');
if (text != "") {
process.stdout.write(run_camxes(text, mode, engine) + '\n');
}
run_camxes_loop(mode, engine);
} else {
process.stdout.write(run_camxes(text, mode, engine) + '\n');
process.exit();
}
// ================================ //
async function run_camxes_loop(mode, engine) {
const mode_reset = among_count('L', mode);
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> '
});
rl.prompt();
rl.on('line', (line) => {
if (mode_reset) {
mode = line.substr(0, line.indexOf(' '));
line = line.substr(line.indexOf(' ') + 1);
}
ret = run_camxes(line, mode, engine);
process.stdout.write(run_camxes(line, mode, engine)+ '\n');
rl.prompt();
}).on('close', () => {
process.stdout.write("\nco'o\n");
process.exit();
});
}
// ================================ //
function run_camxes(input, mode, engine) {
var result;
var syntax_error = false;
result = camxes_preproc.preprocessing(input);
try {
result = engine.parse(result);
} catch (e) {
var location_info = ' Location: [' + e.location.start.offset + ', ' + e.location.end.offset + ']';
location_info += ' …' + input.substring(e.location.start.offset, e.location.start.offset + 12) + '…';
result = e.toString() + location_info;
syntax_error = true;
} finally {
if (!syntax_error)
result = camxes_postproc.postprocessing(result, mode);
return result;
}
}
function among_count(v, s) {
var i = 0;
var ret = 0;
while (i < s.length) {
if (s[i++] == v) ret = ret + 1;
}
return ret;
}