Grammalecte  Diff

Differences From Artifact [779e66492e]:

To Artifact [32ac3727dc]:


50
51
52
53
54
55
56
57

58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
























79
80
81
82
83
84



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

112
113
114
115

116
117
118
119
120

121
122
123
124
125

126
127
128
129
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

147
148
149
150
151
152
153
154
155
156
157
















50
51
52
53
54
55
56

57
58
59




















60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86



87
88
89





90
91
92

















93

94
95
96
97

98
99
100
101
102

103
104
105
106
107

108
109
110
111
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

129
130
131
132
133
134
135
136
137



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153







-
+

+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



-
-
-
+
+
+
-
-
-
-
-



-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

-
+



-
+




-
+




-
+




-
+















-
+








-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
let lxg = null; // module: lexicographer
let helpers = null;

let oTokenizer = null;
let oDict = null;
let oLxg = null;

function loadGrammarChecker (sGCOptions="") {
function loadGrammarChecker (sGCOptions="", sContext="JavaScript") {
    if (gce === null) {
        try {
        gce = require("resource://grammalecte/fr/gc_engine.js");
        helpers = require("resource://grammalecte/helpers.js");
        text = require("resource://grammalecte/text.js");
        tkz = require("resource://grammalecte/tokenizer.js");
        lxg = require("resource://grammalecte/fr/lexicographe.js");
        oTokenizer = new tkz.Tokenizer("fr");
        helpers.setLogOutput(worker.log);
        gce.load();
        oDict = gce.getDictionary();
        oLxg = new lxg.Lexicographe(oDict);
        if (sGCOptions !== "") {
            gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
        }
        // we always retrieve options from the gce, for setOptions filters obsolete options
        return gce.getOptions()._toString();
    }
}

function parse (sText, sLang, bDebug, bContext) {
    let aGrammErr = gce.parse(sText, sLang, bDebug, bContext);
            gce = require("resource://grammalecte/fr/gc_engine.js");
            helpers = require("resource://grammalecte/helpers.js");
            text = require("resource://grammalecte/text.js");
            tkz = require("resource://grammalecte/tokenizer.js");
            lxg = require("resource://grammalecte/fr/lexicographe.js");
            oTokenizer = new tkz.Tokenizer("fr");
            helpers.setLogOutput(worker.log);
            gce.load(sContext);
            oDict = gce.getDictionary();
            oLxg = new lxg.Lexicographe(oDict);
            if (sGCOptions !== "") {
                gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
            }
            // we always retrieve options from the gce, for setOptions filters obsolete options
            return gce.getOptions().gl_toString();
        }
        catch (e) {
            worker.log("# Error: " + e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
        }
    }
}

function parse (sText, sCountry, bDebug, bContext) {
    let aGrammErr = gce.parse(sText, sCountry, bDebug, bContext);
    return JSON.stringify(aGrammErr);
}

function parseAndSpellcheck (sText, sLang, bDebug, bContext) {
    let aGrammErr = gce.parse(sText, sLang, bDebug, bContext);
    let aSpellErr = [];
function parseAndSpellcheck (sText, sCountry, bDebug, bContext) {
    let aGrammErr = gce.parse(sText, sCountry, bDebug, bContext);
    let aSpellErr = oTokenizer.getSpellingErrors(sText, oDict);
    for (let oToken of oTokenizer.genTokens(sText)) {
        if (oToken.sType === 'WORD' && !oDict.isValidToken(oToken.sValue)) {
            aSpellErr.push(oToken);
        }
    }
    return JSON.stringify({ aGrammErr: aGrammErr, aSpellErr: aSpellErr });
}

function parseAndTag (sText, iParagraph, sLang, bDebug) {
    sText = text.addHtmlEntities(sText);
    let aSpellErr = [];
    for (let oToken of oTokenizer.genTokens(sText)) {
        if (oToken.sType === 'WORD' && !oDict.isValidToken(oToken.sValue)) {
            aSpellErr.push(oToken);
        }
    }
    let aGrammErr = gce.parse(sText, sLang, bDebug);
    let sHtml = text.tagParagraph(sText, iParagraph, aGrammErr, aSpellErr);
    return sHtml;
}

function parseAndGenerateParagraph (sText, iParagraph, sLang, bDebug) {
    return text.createHTMLBlock(parseAndTag(sText, iParagraph, sLang, bDebug), iParagraph);
}

function getOptions () {
    return gce.getOptions()._toString();
    return gce.getOptions().gl_toString();
}

function getDefaultOptions () {
    return gce.getDefaultOptions()._toString();
    return gce.getDefaultOptions().gl_toString();
}

function setOptions (sGCOptions) {
    gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
    return gce.getOptions()._toString();
    return gce.getOptions().gl_toString();
}

function setOption (sOptName, bValue) {
    gce.setOptions(new Map([ [sOptName, bValue] ]));
    return gce.getOptions()._toString();
    return gce.getOptions().gl_toString();
}

function resetOptions () {
    gce.resetOptions();
    return gce.getOptions()._toString();
    return gce.getOptions().gl_toString();
}

function fullTests (sGCOptions="") {
    if (!gce || !oDict) {
        return "# Error: grammar checker or dictionary not loaded."
    }
    let dMemoOptions = gce.getOptions();
    if (sGCOptions) {
        gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
    }
    let tests = require("resource://grammalecte/tests.js");
    let oTest = new tests.TestGrammarChecking(gce);
    let sAllRes = "";
    for (let sRes of oTest.testParse()) {
        dump(sRes+"\n");
        sAllRes += sRes+"<br/>";
        sAllRes += sRes+"\n";
    }
    gce.setOptions(dMemoOptions);
    return sAllRes;
}


// Lexicographer

function analyzeWords (sText) {
    return oLxg.analyzeText(sText);
}
function getListOfElements (sText) {
    try {
        let aElem = [];
        let aRes = null;
        for (let oToken of oTokenizer.genTokens(sText)) {
            aRes = oLxg.getInfoForToken(oToken);
            if (aRes) {
                aElem.push(aRes);
            }
        }
        return JSON.stringify(aElem);
    }
    catch (e) {
        helpers.logerror(e);
    }
}