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
|
const startTime = new Date();
class MetranslatorInitializationError extends Error {
constructor(message) {
super(message);
}
}
switch (process.argv[2]) {
case "debug":
debug = true
api = false
break;
case "release":
debug = false
api = false
break;
case "api":
debug = false
api = true
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: [],
duration: null,
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);
}
}
output.output = query.trim().replaceAll(" !", "!").replaceAll(" ?", "?").replaceAll(" ,", ",").replaceAll(" .", ".").replaceAll("[{[", "").replaceAll("]}]", "");
const endTime = new Date();
const diffTime = (endTime - startTime).toFixed(2);
output.duration = endTime - startTime;
if (debug) {
console.log("")
console.dir(output);
} else if (api) {
console.log(JSON.stringify(output).trim());
} else {
console.log("")
console.log("*")
console.log("| "+JSON.stringify(db._name).replaceAll('"',''))
console.log("| Database version: " + JSON.stringify(db._version).replaceAll('"',''))
console.log("| Made by Jamez and Minteck!")
console.log("| Source: https://minteck.ro.lt/git/minteck/metranslator-api")
console.log("*")
console.log("")
console.log("Done in " + diffTime + " ms");
console.log("Database contains " + db.phrases.length + " definitions (" + ((JSON.stringify(db.phrases).length)/1024).toFixed(2) + " KiB)");
console.log("");
console.log("Fun Facts:\n - " + output['facts'].join("\n - "))
console.log("")
console.log("Output: " + output['output'])
}
|