summaryrefslogtreecommitdiff
path: root/src/differenciate.js
blob: 61cfa17f561961f3495887afb7b6f560fa4dad8e (plain)
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
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;
}