
const fs = require("fs");

 module.exports = {
    GenerateDictionaries : function()
    {
        var naPath = "./sentenceAnalysis/en/dictionaries/list_NN_Adj.csv";
        var nvnaPath = "./sentenceAnalysis/en/dictionaries/list_NN_VB_notAdj.csv";
        //
        var vbnPath = "./sentenceAnalysis/en/dictionaries/list_VBN_Lemma.csv";
        var vbnnPath = "./sentenceAnalysis/en/dictionaries/list_VBN_NN.csv";
        if (fs.existsSync(naPath))
        {
            fs.unlinkSync(naPath);
        }
        if (fs.existsSync(nvnaPath))
        {
            fs.unlinkSync(nvnaPath);
        }
        if (fs.existsSync(vbnPath))
        {
            fs.unlinkSync(vbnPath);
        }
        if (fs.existsSync(vbnnPath))
        {
            fs.unlinkSync(vbnnPath);
        }
        
        var nouns = {};
        var adjectives = {};
        var verbs = {};
        var exceptionAdjectives = {};
        var exceptionVerbs = {};
        var stream = fs.readFileSync("./wordServices/en/generated/2of12id.csv", 'utf8').split('\n');
        for (var i=0; i<stream.length; i++)
        {
        //  abandon,v,abandoned,abandoning,abandons
            var word = stream[i].split(",");
            if (word.length<2)
                continue;
            if (word[1]=="n")
            {
                nouns[word[0]] = true;
            }
            else if (word[1]=="v")
            {
                verbs[word[0]] = true;
            }
            if (word[1]=="adj")
            {
                adjectives[word[0]] = true;
            }
        }
        stream = fs.readFileSync("./sentenceAnalysis/en/misc/exceptionList_NN_Adj.csv", 'utf8').split('\n');
        for ( i=0; i<stream.length; i++)
        {
            exceptionAdjectives[stream[i]] = true;
        }
        stream = fs.readFileSync("./sentenceAnalysis/en/misc/exceptionList_NN_VB.csv", 'utf8').split('\n');
        for ( i=0; i<stream.length; i++)
        {
            exceptionVerbs[stream[i]] = true;
        }
        fs.appendFileSync(naPath,"key");
        fs.appendFileSync(nvnaPath,"key");
        for (var noun in nouns)
        {
            if (adjectives[noun])
            {
                if (!exceptionAdjectives[noun])
                {
                    fs.appendFileSync(naPath, "\n"+noun);
                }
                // nounAndAdjectives[] = true;                
            }
            else if (verbs[noun]){
                if (!exceptionVerbs[noun])
                {
                    fs.appendFileSync(nvnaPath, "\n"+noun);
                }
            }
        }
        fs.appendFileSync(vbnPath,"key,lemma");
        fs.appendFileSync(vbnnPath,"key");
        var vfStream = fs.readFileSync("./wordServices/en/Verbs Forms.csv", 'utf8').split('\n');
        for (var i = 1; i < vfStream.length; i++) {
            var verbs = vfStream[i].split(",");
            if (verbs.length >= 6) {
                if (verbs[1] == '')
                    continue;
                var vbns = verbs[3].split("/");
                var lemma = verbs[1];
                for (var vi=0; vi < vbns.length; vi++)
                {
                    var vbn = vbns[vi];
                    fs.appendFileSync(vbnPath, "\n"+vbn+","+lemma);
                    if (nouns[vbn])
                    {
                        fs.appendFileSync(vbnnPath, "\n"+vbn);
                    }
                }
            }
        }
    }
};