summaryrefslogtreecommitdiff
path: root/src/differenciate.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2021-12-21 16:52:28 +0100
committerMinteck <contact@minteck.org>2021-12-21 16:52:28 +0100
commit46e43f4bde4a35785b4997b81e86cd19f046b69b (patch)
treec53c2f826f777f9d6b2d249dab556feb72a6c3a6 /src/differenciate.js
downloadlangdetect-46e43f4bde4a35785b4997b81e86cd19f046b69b.tar.gz
langdetect-46e43f4bde4a35785b4997b81e86cd19f046b69b.tar.bz2
langdetect-46e43f4bde4a35785b4997b81e86cd19f046b69b.zip
Commit
Diffstat (limited to 'src/differenciate.js')
-rw-r--r--src/differenciate.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/differenciate.js b/src/differenciate.js
new file mode 100644
index 0000000..61cfa17
--- /dev/null
+++ b/src/differenciate.js
@@ -0,0 +1,45 @@
+const fs = require('fs');
+const sortObject = obj => Object.keys(obj).sort().reduce((res, key) => (res[key] = obj[key], res), {});
+
+module.exports = (dpercs, dlangs) => {
+ matches = {};
+
+ keys = Object.keys(dpercs);
+ keys.forEach(key => {
+ if (key == "total") {
+ return;
+ }
+ matches[key] = {
+ langs: {},
+ best: {
+ lvl1: null,
+ lvl2: null,
+ lvl3: null,
+ lvl4: null,
+ lvl5: null,
+ }
+ }
+ dlgkeys = Object.keys(dlangs);
+ dlgkeys.forEach(lk => {
+ if (dlangs[lk]) {
+ inLang = JSON.parse(fs.readFileSync("./data/" + lk + ".dat").toString())[key].average;
+ inWord = dpercs[key];
+ diff = inLang - inWord;
+ if (diff < 0) {
+ diff = diff - (diff * 2);
+ }
+ // console.log("Lang: " + inLang + "\nWord: " + inWord + "\nDiff: " + diff);
+ matches[key].langs[diff] = lk;
+ matches[key].langs = sortObject(matches[key].langs);
+ okeys = Object.keys(matches[key].langs);
+ matches[key].best.lvl1 = matches[key].langs[okeys[0]];
+ matches[key].best.lvl2 = matches[key].langs[okeys[1]];
+ matches[key].best.lvl3 = matches[key].langs[okeys[2]];
+ matches[key].best.lvl4 = matches[key].langs[okeys[3]];
+ matches[key].best.lvl5 = matches[key].langs[okeys[4]];
+ }
+ })
+ })
+
+ return matches;
+} \ No newline at end of file