Grammalecte  Diff

Differences From Artifact [ea3f39a7e2]:

To Artifact [0a0381f0d1]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
    WORKER:
    https://developer.mozilla.org/en-US/docs/Web/API/Worker
    https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope


    JavaScript still sucks.
    No module available in WebExtension at the moment! :(
    No require, no import/export.

    In Worker, we have importScripts() which imports everything in this scope.

    In order to use the same base of code with XUL-addon for Thunderbird and SDK-addon for Firefox,
    all modules have been “objectified”. And while they are still imported via “require”






|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
    WORKER:
    https://developer.mozilla.org/en-US/docs/Web/API/Worker
    https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope


    JavaScript sucks.
    No module available in WebExtension at the moment! :(
    No require, no import/export.

    In Worker, we have importScripts() which imports everything in this scope.

    In order to use the same base of code with XUL-addon for Thunderbird and SDK-addon for Firefox,
    all modules have been “objectified”. And while they are still imported via “require”
38
39
40
41
42
43
44

45
46
47









48
49
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
importScripts("grammalecte/fr/conj.js");
importScripts("grammalecte/fr/mfsp.js");
importScripts("grammalecte/fr/phonet.js");
importScripts("grammalecte/fr/cregex.js");
importScripts("grammalecte/fr/gc_options.js");
importScripts("grammalecte/fr/gc_rules.js");
importScripts("grammalecte/fr/gc_engine.js");

importScripts("grammalecte/tests.js");












helpers.echo("helpers echo");



let oTokenizer = null;
let oLxg = null;


function loadGrammarChecker (sGCOptions="", sContext="JavaScript") {
    if (gc_engine === null) {
        try {

            gc_engine = 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(console.log);

            gc_engine.load(sContext);
            oDict = gc_engine.getDictionary();

            oLxg = new lxg.Lexicographe(oDict);
            if (sGCOptions !== "") {
                gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
            }














            // we always retrieve options from the gc_engine, for setOptions filters obsolete options
            return gc_engine.getOptions()._toString();
        }
        catch (e) {
            console.log("# Error: " + e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
        }
    }
}

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








>



>
>
>
>
>
>
>
>
>
|
<
|
>
>


>

|
<
|
>
|
|
|
|
>
|
|
|
>
|
|
>
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|







38
39
40
41
42
43
44
45
46
47
48
49
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
importScripts("grammalecte/fr/conj.js");
importScripts("grammalecte/fr/mfsp.js");
importScripts("grammalecte/fr/phonet.js");
importScripts("grammalecte/fr/cregex.js");
importScripts("grammalecte/fr/gc_options.js");
importScripts("grammalecte/fr/gc_rules.js");
importScripts("grammalecte/fr/gc_engine.js");
importScripts("grammalecte/fr/lexicographe.js");
importScripts("grammalecte/tests.js");


/*
    Warning.
    Initialization can’t be completed at startup, for we need the path of the path of the extension
    to load data stored in JSON files.
    This path is retrieved in background.js and passed with the following event.
*/
onmessage = function (e) {
    if (e.data[0] == "init") {
        loadGrammarChecker(e.data[1]);
    }

}

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

function loadGrammarChecker (sCGDataPath, sGCOptions="", sContext="JavaScript") {

    try {
        console.log("Loading… path: " + sCGDataPath);
        conj.init(helpers.loadFile(sCGDataPath + "/grammalecte/fr/conj_data.json"));
        phonet.init(helpers.loadFile(sCGDataPath + "/grammalecte/fr/phonet_data.json"));
        mfsp.init(helpers.loadFile(sCGDataPath + "/grammalecte/fr/mfsp_data.json"));
        oTest = new TestGrammarChecking(gc_engine, sCGDataPath+"/grammalecte/fr/tests_data.json");
        console.log("JSON loaded");

        oTokenizer = new Tokenizer("fr");
        console.log("Tokenizer loaded");

        gc_engine.load(sContext, sCGDataPath+"grammalecte/_dictionaries");
        oDict = gc_engine.getDictionary();

        oLxg = new Lexicographe(oDict);
        if (sGCOptions !== "") {
            gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
        }
        helpers.echo("START");

        helpers.echo(conj.getConj("devenir", ":E", ":2s"));

        helpers.echo(mfsp.getMasForm("emmerdeuse", true));
        helpers.echo(mfsp.getMasForm("pointilleuse", false));

        helpers.echo(phonet.getSimil("est"));

        let aRes = gc_engine.parse("Je suit...");
        for (let oErr of aRes) {
            helpers.echo(text.getReadableError(oErr));
        }
        fullTests();
        // we always retrieve options from the gc_engine, for setOptions filters obsolete options
        //return gce.getOptions().gl_toString();
    }
    catch (e) {
        console.error("\n" + e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
    }

}

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

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    if (!gc_engine || !oDict) {
        return "# Error: grammar checker or dictionary not loaded."
    }
    let dMemoOptions = gc_engine.getOptions();
    if (sGCOptions) {
        gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
    }
    let oTest = new TestGrammarChecking(gc_engine);
    for (let sRes of oTest.testParse()) {
        helpers.echo(sRes+"\n");
    }
    gc_engine.setOptions(dMemoOptions);
}









|







143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    if (!gc_engine || !oDict) {
        return "# Error: grammar checker or dictionary not loaded."
    }
    let dMemoOptions = gc_engine.getOptions();
    if (sGCOptions) {
        gc_engine.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
    }
    
    for (let sRes of oTest.testParse()) {
        helpers.echo(sRes+"\n");
    }
    gc_engine.setOptions(dMemoOptions);
}


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
        return JSON.stringify(aElem);
    }
    catch (e) {
        helpers.logerror(e);
    }
    return JSON.stringify([]);
}


helpers.echo("START");

helpers.echo(conj.getConj("devenir", ":E", ":2s"));

helpers.echo(mfsp.getMasForm("emmerdeuse", true));
helpers.echo(mfsp.getMasForm("pointilleuse", false));

helpers.echo(phonet.getSimil("est"));

let oDict = new IBDAWG("French.json");
helpers.echo(oDict.getMorph("merde"));

gc_engine.load("JavaScript");
let aRes = gc_engine.parse("Je suit...");
for (let oErr of aRes) {
    helpers.echo(text.getReadableError(oErr));
}


//fullTests();









<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
170
171
172
173
174
175
176
























        return JSON.stringify(aElem);
    }
    catch (e) {
        helpers.logerror(e);
    }
    return JSON.stringify([]);
}