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
|
class MetranslatorInitializationError extends Error {
constructor(message) {
super(message);
}
}
switch (process.argv[2]) {
case "debug":
debug = true
break;
case "release":
debug = false
break;
default:
throw new MetranslatorInitializationError("Debugging level not defined or invalid")
}
switch (process.argv[3]) {
case "en":
toMetroz = false
break;
case "mt":
toMetroz = true
break;
default:
throw new MetranslatorInitializationError("Target language not defined or invalid")
}
if (typeof process.argv[4] !== "string") {
throw new MetranslatorInitializationError("String to translate not defined")
}
if (debug) console.log("Loading database");
const db = require('./database.json');
let output = {
system: {
name: db._name,
version: db._version,
length: db.phrases.length
},
facts: [],
output: null
}
let query = " " + process.argv[4].toLowerCase().replaceAll("!", " !").replaceAll("?", " ?").replaceAll(",", " ,").replaceAll(".", " .") + " ";
if (toMetroz) {
if (debug) console.log("Target language is Metroz, source MUST be English");
for (phrase of db.phrases) {
if (debug) console.log("\nTrying to match '" + phrase.en.trim() + "'...");
matches = (query.match(new RegExp(phrase.en, "gmi")) || []).length;
if (debug) console.log(matches + " match(es)")
if (matches > 0 && typeof phrase.fact === "string" && phrase.fact.trim() !== "") {
output.facts.push(phrase.fact)
}
query = query.replaceAll(phrase.en, phrase.mt);
}
} else {
if (debug) console.log("Target language is English, source MUST be Metroz");
for (phrase of db.phrases) {
if (debug) console.log("\nTrying to match '" + phrase.mt.trim() + "'...");
matches = (query.match(new RegExp(phrase.mt, "gmi")) || []).length;
if (debug) console.log(matches + " match(es)")
if (matches > 0 && typeof phrase.fact === "string" && phrase.fact.trim() !== "") {
output.facts.push(phrase.fact)
}
query = query.replaceAll(phrase.mt, phrase.en);
}
}
console.log("")
output.output = query.trim().replaceAll(" !", "!").replaceAll(" ?", "?").replaceAll(" ,", ",").replaceAll(" .", ".").replaceAll("[{[", "").replaceAll("]}]", "");
if (debug) console.dir(output); else console.log(JSON.stringify(output))
|