//const corenlp = require("corenlp-request-wrapper");
const axios = require("axios")
const request = require('request');
const portNumber = 80

// const mailServices = require("../helpers/emailServer");

//var pattern = /([?,;.!])(\w)/g;

var dicoStanfordPos = {};
// english
// penn part of speech : http://repository.upenn.edu/cgi/viewcontent.cgi?article=1603&context=cis_reports
// see more here : http://gromoteur.ilpga.fr/manuals/tagset.pdf
dicoStanfordPos["CC"] = "conj"; // 1.	CC	Coordinating conjunction
dicoStanfordPos["CD"] = "num"; // 2.	CD	Cardinal number
dicoStanfordPos["DT"] = "det"; // 3.	DT	Determiner
dicoStanfordPos["EX"] = ""; // 4.	EX	Existential there
dicoStanfordPos["FW"] = ""; // 5.	FW	Foreign word
dicoStanfordPos["IN"] = "conj,prep"; // 6.	IN	Preposition or subordinating conjunction
dicoStanfordPos["JJ"] = "adj"; // 7.	JJ	Adjective
dicoStanfordPos["JJR"] = "adj"; // 8.	JJR	Adjective, comparative
dicoStanfordPos["JJS"] = "adj"; // 9.	JJS	Adjective, superlative
dicoStanfordPos["LS"] = ""; // 10.	LS	List item marker
dicoStanfordPos["MD"] = "v"; // 11.	MD	Modal
dicoStanfordPos["NN"] = "n"; // 12.	NN	Noun, singular or mass
dicoStanfordPos["NNS"] = "n"; // 13.	NNS	Noun, plural
dicoStanfordPos["NNP"] = "pn"; // 14.	NNP	Proper noun, singular
dicoStanfordPos["NNPS"] = "pn"; // 15.	NNPS	Proper noun, plural
dicoStanfordPos["PDT"] = ""; // 16.	PDT	Predeterminer
dicoStanfordPos["POS"] = ""; // 17.	POS	Possessive ending
dicoStanfordPos["PRP"] = "pron"; // 18.	PRP	Personal pronoun
dicoStanfordPos["PRP$"] = "pron"; // 19.	PRP$	Possessive pronoun
dicoStanfordPos["RB"] = "adv"; // 20.	RB	Adverb
dicoStanfordPos["RBR"] = "adv"; // 21.	RBR	Adverb, comparative
dicoStanfordPos["RBS"] = "adv"; // 22.	RBS	Adverb, superlative
dicoStanfordPos["RP"] = "par"; // 23.	RP	Particle
dicoStanfordPos["SYM"] = ""; // 24.	SYM	Symbol
dicoStanfordPos["TO"] = ""; // 25.	TO	to
dicoStanfordPos["UH"] = "interj"; // 26.	UH	Interjection
dicoStanfordPos["VB"] = "v"; // 27.	VB	Verb, base form
dicoStanfordPos["VBD"] = "v"; // 28.	VBD	Verb, past tense
dicoStanfordPos["VBG"] = "v"; // 29.	VBG	Verb, gerund or present participle
dicoStanfordPos["VBN"] = "v"; // 30.	VBN	Verb, past participle
dicoStanfordPos["VBP"] = "v"; // 31.	VBP	Verb, non-3rd person singular present
dicoStanfordPos["VBZ"] = "v"; // 32.	VBZ	Verb, 3rd person singular present
dicoStanfordPos["WDT"] = "det"; // 33.	WDT	Wh-determiner
dicoStanfordPos["WP"] = "pron"; // 34.	WP	Wh-pronoun
dicoStanfordPos["WP$"] = "pron"; // 35.	WP$	Possessive wh-pronoun
dicoStanfordPos["WRB"] = "adv"; // 36.	WRB	Wh-adverb
//
// french
// see : http://www.llf.cnrs.fr/Gens/Abeille/French-Treebank-fr.php
// http://www.lattice.cnrs.fr/sites/itellier/SEM.html
// A ADJ ADJWH ADV ADVWH C CC CL CLO CLR CLS CS DET DETWH ET I N NC NPP P PREF PRO PROREL PROWH PUNC V VIMP VINF VPP VPR VS
dicoStanfordPos["ADV"] = "adv";
dicoStanfordPos["A"] = "adj";
dicoStanfordPos["ADJ"] = "adj"; // ADJ (adjective)
dicoStanfordPos["ADJWH"] = "det"; // ADJWH
dicoStanfordPos["ADV"] = "adv"; // ADV (adverb)
dicoStanfordPos["ADVWH"] = "adv"; // ADV (adverb)
dicoStanfordPos["C"] = "conj"; // C (conjunction)
dicoStanfordPos["CC"] = "conj"; // CC (coordinating conjunction)
dicoStanfordPos["CS"] = "conj"; // CS (subordinating conjunction)
dicoStanfordPos["DET"] = "det"; // DET (determiner)
dicoStanfordPos["DETWH"] = "det"; // DETWH (determiner)
dicoStanfordPos["ET"] = ""; // ET (foreign word)
dicoStanfordPos["I"] = "interj"; // I (interjection)
dicoStanfordPos["N"] = "n"; // N ( noun)
dicoStanfordPos["NC"] = "n"; // NC (common noun)
dicoStanfordPos["NP"] = "pn"; // NP (proper noun)
dicoStanfordPos["P"] = "prep"; // P (preposition)
dicoStanfordPos["PREF"] = ""; // PREF (prefix)
dicoStanfordPos["PRO"] = "pron"; // PRO (strong pronoun)
dicoStanfordPos["PROREL"] = "pron"; // PROREL (relative pronoun)
dicoStanfordPos["PROWH"] = "pron"; // PROREL (relative pronoun)
dicoStanfordPos["PUNC"] = ""; // PUNC (punctuation mark)
dicoStanfordPos["V"] = "v"; // V (verb)
dicoStanfordPos["VIMP"] = "v"; // VIMP (verb)
dicoStanfordPos["VINF"] = "v"; // VINF (verb)
dicoStanfordPos["VPP"] = "v"; // VPP (verb)
dicoStanfordPos["VPR"] = "v"; // VPR (verb)
dicoStanfordPos["VS"] = "v"; // VS (verb)
dicoStanfordPos["CL"] = "pron"; // CL (weak clitic pronoun)
dicoStanfordPos["CLR"] = "pron"; // CLR (pronom clitique réfléchi)
dicoStanfordPos["CLS"] = "pron"; // CLS (personal pronoun) (pronom clitique sujet)
dicoStanfordPos["CLO"] = "pron"; // CLO (pronom clitique objet)
dicoStanfordPos["NPP"] = "pn"; // NPP (proper pronoun)
//
// CoreNLP Server was lunched here with the french props file on port 9000 

// POST /?properties=%7B%22annotators%22%3A%20%22tokenize%2Cssplit%2Cpos%2Clemma%22%2C%20%22date%22%3A%20%222017-10-03T11%3A21%3A42%22%7D&pipelineLanguage=fr HTTP/1.1
// Host: ec2-34-241-189-245.eu-west-1.compute.amazonaws.com
// Connection: keep-alive
// Content-Length: 32
// Accept: application/json, text/javascript, */*; q=0.01
// Origin: http://ec2-34-241-189-245.eu-west-1.compute.amazonaws.com
// X-Requested-With: XMLHttpRequest
// User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
// Content-Type: application/x-www-form-urlencoded;charset=UTF-8
// Referer: http://ec2-34-241-189-245.eu-west-1.compute.amazonaws.com/
// Accept-Encoding: gzip, deflate
// Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4

// http://ec2-34-241-189-245.eu-west-1.compute.amazonaws.com/?properties={"annotators": "tokenize,ssplit,pos,lemma", "date": "2017-10-03T11:21:42"}&pipelineLanguage=fr

function testCoreNLP () {
    const url = `http://ec2-34-244-187-2.eu-west-1.compute.amazonaws.com/?properties=%7B%22annotators%22%3A%20%22tokenize%2Cssplit%2Cpos%22%2C%20%22date%22%3A%20%222024-01-16T16%3A44%3A47%22%7D&pipelineLanguage=en`
    axios.post(url, {
    }, {}).then((response) => {
        if (response.status === 200 || response.status === 202) {
            // mailServices.sendErrorEmail(response.data)
            // debugger
        } else {
            // mailServices.sendErrorEmail(response)
            console.error(response.status)
        }
    }).catch(e => {
        debugger;
        // mailServices.sendErrorEmail(e)
        console.error(e)
    });
}
testCoreNLP()
setInterval(()=> {
    testCoreNLP()
},1000*60*5)

module.exports = {
    
    parse: function(language, stringToProcess, annotators, outputFormat, cb) {
      var data = {
        body: stringToProcess,
        qs: {properties: JSON.stringify({annotators, outputFormat})}
      };
      if (language=="fr")
      {
          data.qs.pipelineLanguage="fr";
      }
      
      request.post(`http://ec2-34-244-187-2.eu-west-1.compute.amazonaws.com/:${portNumber}/`,
      data,function(err, response, body) {
        cb(err, body, response);
      });
    },
    GetTextPoss: function(language, text)
    {
        return new Promise((resolve, reject) => 
        {
            var annotators = "pos";
            if (language=="en")
            {
                annotators += ",lemma";
            }
            //var cleanedText = text.replace(pattern, "$1"+" "+"$2");
            this.parse(
            language,
            text /*stringToProcess*/,
            annotators /*annotators*/,
            "json" /*outputFormat*/,
            (err, parsedText) => { /*Callback function*/

                const cleanedParsedText = parsedText.replace(/\n/gm,"")
    
                if (err)
                {
                    reject(err);
                }
                else if (cleanedParsedText.startsWith('{') && cleanedParsedText.endsWith('}'))
                {
                    var stanFordResult = JSON.parse(cleanedParsedText);
                    var result={
                        sentences: []
                    };
                    for (var si=0; si<stanFordResult.sentences.length; si++)
                    {
                        var stanfordSentence = stanFordResult.sentences[si];
                        var sentence = {tokens: []};
                        for (var ti=0; ti<stanfordSentence.tokens.length; ti++)
                        {
                            var stanfordToken = stanfordSentence.tokens[ti];
                            if (stanfordToken.originalText.startsWith('-'))
                            {
                                var token = {
                                    word: "-",
                                    stanPos: "-",
                                    pos: "",
                                    begin: stanfordToken.characterOffsetBegin,
                                    end: stanfordToken.characterOffsetBegin+1,
                                };
                                sentence.tokens.push(token);
                                //
                                stanfordToken.originalText = stanfordToken.originalText.substring(1);
                                stanfordToken.characterOffsetBegin ++;
                                ti--;
                            }
                            else if (stanfordToken.originalText.startsWith('t-'))
                            {                                
                                var token = {
                                    word: "t",
                                    stanPos: "",
                                    pos: "",
                                    begin: stanfordToken.characterOffsetBegin,
                                    end: stanfordToken.characterOffsetBegin+1,
                                };
                                sentence.tokens.push(token);   
                                var token = {
                                    word: "-",
                                    stanPos: "-",
                                    pos: "",
                                    begin: stanfordToken.characterOffsetBegin+1,
                                    end: stanfordToken.characterOffsetBegin+2,
                                };
                                sentence.tokens.push(token);
                                stanfordToken.originalText = stanfordToken.originalText.substring(2);
                                stanfordToken.characterOffsetBegin += 2;
                                ti--;
                            }
                            else{
                                var token = {
                                    word: stanfordToken.originalText,
                                    lemme: stanfordToken.lemma,
                                    stanPos: stanfordToken.pos,
                                    pos: dicoStanfordPos[stanfordToken.pos],
                                    begin: stanfordToken.characterOffsetBegin,
                                    end: stanfordToken.characterOffsetEnd,
                                };
                                sentence.tokens.push(token);
                            }
                        }
                        result.sentences.push(sentence);
                    }
                    resolve(result);
                }
                else{
                    debugger
                    reject(new Error(parsedText));
                }
            }
            );
        });
    },
    GetTextNLP: function(language, text, full, constituency)
    {
        return new Promise((resolve, reject) => 
        {
            var annotators = "pos";
            if (language=="en")
            {
                annotators += ",lemma";
            }
            //if (full)
            {
                annotators += ",depparse";
            }
            if (constituency)
            {
                annotators += ",parse";
            }
            //var cleanedText = text.replace(pattern, "$1"+" "+"$2");
            this.parse(
            language,
            text /*stringToProcess*/,
            annotators /*annotators*/,
            "json" /*outputFormat*/,
            (err, parsedText) => { /*Callback function*/
                const cleanedParsedText = parsedText.replace(/\n/gm,"")
    
                if (err)
                {
                    reject(err);
                }
                else if (cleanedParsedText.startsWith('{') && cleanedParsedText.endsWith('}'))
                {
                    var stanFordResult = JSON.parse(cleanedParsedText);
                    resolve(stanFordResult);
                   
                }
                else {
                    debugger
                    reject(new Error(parsedText));
                }
            }
            );
        });
    },
    ConvertOLToStanford: function(nlp)
    {
        var result={
            sentences: []
        };
        for (var si=0; si<nlp.sentences.length; si++)
        {
            var olSentence = nlp.sentences[si];
            var sentence = {tokens: []};
            for (var ti=0; ti<olSentence.tokens.length; ti++)
            {
                var olToken = olSentence.tokens[ti];
                var token = {
                    word: olToken.word,
                    pos: olToken.stanPos,
                    characterOffsetBegin: olToken.begin,
                    characterOffsetEnd: olToken.end,
                };
                if (olToken.lemme)
                {
                    token.lemma = olToken.lemme;
                }
                if (olToken.stanPos)
                {
                    token.pos = olToken.stanPos;
                }
                sentence.tokens.push(token);
            }
            result.sentences.push(sentence);
        }
        return result;
        // word: stanfordToken.originalText,
        // lemme: stanfordToken.lemma,
        // stanPos: stanfordToken.pos,
        // pos: dicoStanfordPos[stanfordToken.pos],
        // begin: stanfordToken.characterOffsetBegin,
        // end: stanfordToken.characterOffsetEnd,
    },
    GetOLPos: function(pos)
    {
        return dicoStanfordPos[pos];
    }
}