aboutsummaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'index.js')
-rw-r--r--index.js79
1 files changed, 79 insertions, 0 deletions
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..5950473
--- /dev/null
+++ b/index.js
@@ -0,0 +1,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)) \ No newline at end of file