-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunParser.js
117 lines (104 loc) · 3.12 KB
/
runParser.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
const tslamyu = require('./tslamyu');
//const util = require('util');
const fetch = require('node-fetch');
let verbose = false;
main();
async function main() {
let input = process.argv[2];
if (input === "-v" || input === "--verbose") {
verbose = true;
input = process.argv[3];
}
let responses = await getResponsesFor(input);
if (verbose) {
console.log('\x1b[1m\x1b[34mInput:\x1b[0m');
}
results = tslamyu.doParse(responses, verbose);
if (results['lexingErrors'].length) {
for (let e of results['lexingErrors']) {
error(e);
}
console.log('');
}
results = results['results'];
if (verbose) {
console.log('\x1b[1m\x1b[34mParse results:\x1b[0m ' + results.length + ' possible parse tree(s) found');
}
let correct = results[0].getErrors().length === 0;
if (verbose) {
for (let i = 0; i < results.length; i++) {
let result = results[i];
console.log('───────────────────────────────────────────────────');
outputTree(result);
console.log("(penalty: " + result.getPenalty() + ")");
for (let j = 0; j < result.getErrors().length; j++) {
error(result.getErrors()[j]);
}
console.log(" -> \"" + result.translate() + "\"");
}
console.log('───────────────────────────────────────────────────');
} else {
let lastTranslation = null;
for (let i = 0; i < results.length; i++) {
let result = results[i];
if (i > 0 && result.getPenalty() > results[0].getPenalty()) {
break;
}
for (let j = 0; j < result.getErrors().length; j++) {
error(result.getErrors()[j]);
}
let translation = result.translate();
if (correct && translation !== lastTranslation) {
console.log(" -> \"" + translation + "\"");
lastTranslation = translation;
}
}
}
}
async function getResponsesFor(query) {
const response = await fetch('http://localhost:3000/api/fwew?tìpawm=' + query)
.then(response => response.json())
.catch(error => {
throw error;
});
return response;
}
function outputTree(tree, prefix1 = '', prefix2 = '', role = null) {
let mainText = '';
if (role) {
mainText += '\x1b[33m' + role + ': \x1b[0m';
}
mainText += '\x1b[1m' + tree['word'] + '\x1b[0m';
if (tree['translation']) {
mainText += ' -> ' + tree['translation'];
}
console.log(prefix1 + mainText);
if (tree['children']) {
let prefixLength = 1;
if (role) {
prefixLength = role.length + 3;
}
for (let i = 0; i < tree['children'].length; i++) {
if (i === tree['children'].length - 1) {
outputTree(tree['children'][i],
prefix2 + spaces(prefixLength) + '└─ ',
prefix2 + spaces(prefixLength) + ' ',
tree['roles'][i]);
} else {
outputTree(tree['children'][i],
prefix2 + spaces(prefixLength) + '├─ ',
prefix2 + spaces(prefixLength) + '│ ',
tree['roles'][i]);
}
}
}
}
function spaces(n) {
return Array(n + 1).join(' ');
}
function warning(msg) {
console.log('Warning: ' + msg);
}
function error(msg) {
console.log('\x1b[31m\x1b[1mError:\x1b[0m ' + msg);
}