Grammalecte  Check-in [1f4195c966]

Overview
Comment:[fx][core] initialization in gce_worker.js
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fx | webext2
Files: files | file ages | folders
SHA3-256: 1f4195c966bbafe7f8ee351f98efd6d9740f91027825a526467d0cc8846c25a4
User & Date: olr on 2017-08-04 15:41:45
Other Links: branch diff | manifest | tags
Context
2017-08-04
16:09
[fx] these functions have a new name check-in: b4a1b21d88 user: olr tags: fx, webext2
15:41
[fx][core] initialization in gce_worker.js check-in: 1f4195c966 user: olr tags: fx, webext2
08:18
[core][fx] gc engine in a dedicated worker check-in: 51a209f202 user: olr tags: fx, webext2
Changes

Modified gc_core/js/ibdawg.js from [d79dcb6b99] to [10dd14ace9].

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
${string}



class IBDAWG {
    // INDEXABLE BINARY DIRECT ACYCLIC WORD GRAPH

    constructor (sDicName) {
        try {
            let sURL = (typeof(browser) !== 'undefined') ? browser.extension.getURL("grammalecte/_dictionaries/"+sDicName) : "resource://grammalecte/_dictionaries/"+sDicName;
            const dict = JSON.parse(helpers.loadFile(sURL));
            Object.assign(this, dict);
        }
        catch (e) {
            throw Error("# Error. File not found or not loadable.\n" + e.message + "\n");
        }
        /*







|

|







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
${string}



class IBDAWG {
    // INDEXABLE BINARY DIRECT ACYCLIC WORD GRAPH

    constructor (sDicName, sPath="") {
        try {
            let sURL = (sPath !== "") ? sPath + "/" + sDicName : "resource://grammalecte/_dictionaries/"+sDicName;
            const dict = JSON.parse(helpers.loadFile(sURL));
            Object.assign(this, dict);
        }
        catch (e) {
            throw Error("# Error. File not found or not loadable.\n" + e.message + "\n");
        }
        /*

Modified gc_core/js/lang_core/gc_engine.js from [64ba38ab05] to [2f4877bef2].

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
            return gc_rules.lSentenceRules;
        }
        return gc_rules.lParagraphRules;
    },

    //// Initialization

    load: function (sContext="JavaScript") {
        try {
            if (typeof(require) !== 'undefined') {
                var ibdawg = require("resource://grammalecte/ibdawg.js");
                _oDict = new ibdawg.IBDAWG("${dic_name}.json");
            } else {
                _oDict = new IBDAWG("${dic_name}.json");
            }
            _sAppContext = sContext;
            _dOptions = gc_options.getOptions(sContext).gl_shallowCopy();     // duplication necessary, to be able to reset to default
        }
        catch (e) {
            helpers.logerror(e);
        }







|





|







308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
            return gc_rules.lSentenceRules;
        }
        return gc_rules.lParagraphRules;
    },

    //// Initialization

    load: function (sContext="JavaScript", sPath="") {
        try {
            if (typeof(require) !== 'undefined') {
                var ibdawg = require("resource://grammalecte/ibdawg.js");
                _oDict = new ibdawg.IBDAWG("${dic_name}.json");
            } else {
                _oDict = new IBDAWG("${dic_name}.json", sPath);
            }
            _sAppContext = sContext;
            _dOptions = gc_options.getOptions(sContext).gl_shallowCopy();     // duplication necessary, to be able to reset to default
        }
        catch (e) {
            helpers.logerror(e);
        }

Modified gc_core/js/tests.js from [c744b064be] to [4a9fbec06f].

6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22
23
24
25
26
27
if (typeof(require) !== 'undefined') {
    var helpers = require("resource://grammalecte/helpers.js");
}


class TestGrammarChecking {

    constructor (gce) {
        this.gce = gce;

        this._aRuleTested = new Set();
    };

    * testParse (bDebug=false) {
        const t0 = Date.now();
        let sURL = (typeof(browser) !== 'undefined') ? browser.extension.getURL("grammalecte/"+this.gce.lang+"/tests_data.json") : "resource://grammalecte/"+this.gce.lang+"/tests_data.json";
        const aData = JSON.parse(helpers.loadFile(sURL)).aData;
        let nInvalid = 0
        let nTotal = 0
        let sErrorText;
        let sSugg;
        let sExpectedErrors;
        let sTextToCheck;







|

>





|







6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
if (typeof(require) !== 'undefined') {
    var helpers = require("resource://grammalecte/helpers.js");
}


class TestGrammarChecking {

    constructor (gce, spfTests="") {
        this.gce = gce;
        this.spfTests = spfTests
        this._aRuleTested = new Set();
    };

    * testParse (bDebug=false) {
        const t0 = Date.now();
        let sURL = (this.spfTests !== "") ? this.spfTests : "resource://grammalecte/"+this.gce.lang+"/tests_data.json";
        const aData = JSON.parse(helpers.loadFile(sURL)).aData;
        let nInvalid = 0
        let nTotal = 0
        let sErrorText;
        let sSugg;
        let sExpectedErrors;
        let sTextToCheck;

Modified gc_lang/fr/modules-js/conj.js from [5d3a577c36] to [526a57d214].

475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
        return (this.dConj.get(":PQ").get(":Q4")) ? this.dConj.get(":PQ").get(":Q4") : this.dConj.get(":PQ").get(":Q1");
    }
}


// Initialization
if (typeof(browser) !== 'undefined') {
    // WebExtension
    conj.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/conj_data.json")));
} else if (typeof(require) !== 'undefined') {
    // Add-on SDK and Thunderbird
    let helpers = require("resource://grammalecte/helpers.js");
    conj.init(helpers.loadFile("resource://grammalecte/fr/conj_data.json"));
} else if (typeof(self) !== 'undefined') {
    // used within Firefox content script (conjugation panel).
    // can’t load JSON from here, so we do it in ui.js and send it here.
    self.port.on("provideConjData", function (sJSONData) {
        conj.init(sJSONData);
    });
} else {
    console.log("Error: Impossible d’initialiser le module conj");
}


if (typeof(exports) !== 'undefined') {
    exports._lVtyp = conj._lVtyp;
    exports._lTags = conj._lTags;
    exports._dPatternConj = conj._dPatternConj;







|





|




|

|







475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
        return (this.dConj.get(":PQ").get(":Q4")) ? this.dConj.get(":PQ").get(":Q4") : this.dConj.get(":PQ").get(":Q1");
    }
}


// Initialization
if (typeof(browser) !== 'undefined') {
    // WebExtension (but not in Worker)
    conj.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/conj_data.json")));
} else if (typeof(require) !== 'undefined') {
    // Add-on SDK and Thunderbird
    let helpers = require("resource://grammalecte/helpers.js");
    conj.init(helpers.loadFile("resource://grammalecte/fr/conj_data.json"));
} else if (typeof(self) !== 'undefined' && typeof(self.port) !== 'undefined' && typeof(self.port.on) === "undefined") {
    // used within Firefox content script (conjugation panel).
    // can’t load JSON from here, so we do it in ui.js and send it here.
    self.port.on("provideConjData", function (sJSONData) {
        conj.init(sJSONData);
    });    
} else {
    console.log("Module conj non initialisé");
}


if (typeof(exports) !== 'undefined') {
    exports._lVtyp = conj._lVtyp;
    exports._lTags = conj._lTags;
    exports._dPatternConj = conj._dPatternConj;

Modified gc_lang/fr/modules-js/mfsp.js from [75f345b054] to [efd19b51da].

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
..
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109


var mfsp = {
    // list of affix codes
    _lTagMiscPlur: [],
    _lTagMasForm: [],
    // dictionary of words with uncommon plurals (-x, -ux, english, latin and italian plurals) and tags to generate them
    _dMiscPlur: {},
    // dictionary of feminine forms and tags to generate masculine forms (singular and plural)
    _dMasForm: {},

    init: function (sJSONData) {
        try {
            let _oData = JSON.parse(sJSONData);
            this._lTagMiscPlur = _oData.lTagMiscPlur;
            this._lTagMasForm = _oData.lTagMasForm;
            this._dMiscPlur = helpers.objectToMap(_oData.dMiscPlur);
................................................................................
if (typeof(browser) !== 'undefined') {
    // WebExtension
    mfsp.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/mfsp_data.json")));
} else if (typeof(require) !== 'undefined') {
    // Add-on SDK and Thunderbird
    mfsp.init(helpers.loadFile("resource://grammalecte/fr/mfsp_data.json"));
} else {
    console.log("Error: Impossible d’initialiser le module mfsp");
}


if (typeof(exports) !== 'undefined') {
    exports._lTagMiscPlur = mfsp._lTagMiscPlur;
    exports._lTagMasForm = mfsp._lTagMasForm;
    exports._dMiscPlur = mfsp._dMiscPlur;







|

|







 







|







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
..
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109


var mfsp = {
    // list of affix codes
    _lTagMiscPlur: [],
    _lTagMasForm: [],
    // dictionary of words with uncommon plurals (-x, -ux, english, latin and italian plurals) and tags to generate them
    _dMiscPlur: new Map(),
    // dictionary of feminine forms and tags to generate masculine forms (singular and plural)
    _dMasForm: new Map(),

    init: function (sJSONData) {
        try {
            let _oData = JSON.parse(sJSONData);
            this._lTagMiscPlur = _oData.lTagMiscPlur;
            this._lTagMasForm = _oData.lTagMasForm;
            this._dMiscPlur = helpers.objectToMap(_oData.dMiscPlur);
................................................................................
if (typeof(browser) !== 'undefined') {
    // WebExtension
    mfsp.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/mfsp_data.json")));
} else if (typeof(require) !== 'undefined') {
    // Add-on SDK and Thunderbird
    mfsp.init(helpers.loadFile("resource://grammalecte/fr/mfsp_data.json"));
} else {
    console.log("Module mfsp non initialisé");
}


if (typeof(exports) !== 'undefined') {
    exports._lTagMiscPlur = mfsp._lTagMiscPlur;
    exports._lTagMasForm = mfsp._lTagMasForm;
    exports._dMiscPlur = mfsp._dMiscPlur;

Modified gc_lang/fr/modules-js/phonet.js from [5cab447d24] to [8f4c2c4be2].

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

if (typeof(require) !== 'undefined') {
    var helpers = require("resource://grammalecte/helpers.js");
}


var phonet = {
    _dWord: {},
    _lSet: [],
    _dMorph: {},

    init: function (sJSONData) {
        try {
            let _oData = JSON.parse(sJSONData);
            this._dWord = helpers.objectToMap(_oData.dWord);
            this._lSet = _oData.lSet;
            this._dMorph = helpers.objectToMap(_oData.dMorph);
................................................................................
if (typeof(browser) !== 'undefined') {
    // WebExtension
    phonet.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/phonet_data.json")));
} else if (typeof(require) !== 'undefined') {
    // Add-on SDK and Thunderbird
    phonet.init(helpers.loadFile("resource://grammalecte/fr/phonet_data.json"));
} else {
    console.log("Error: Impossible d’initialiser le module phonet");
}


if (typeof(exports) !== 'undefined') {
    exports._dWord = phonet._dWord;
    exports._lSet = phonet._lSet;
    exports._dMorph = phonet._dMorph;
    exports.init = phonet.init;
    exports.hasSimil = phonet.hasSimil;
    exports.getSimil = phonet.getSimil;
    exports.selectSimil = phonet.selectSimil;
}







|

|







 







|












2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

if (typeof(require) !== 'undefined') {
    var helpers = require("resource://grammalecte/helpers.js");
}


var phonet = {
    _dWord: new Map(),
    _lSet: [],
    _dMorph: new Map(),

    init: function (sJSONData) {
        try {
            let _oData = JSON.parse(sJSONData);
            this._dWord = helpers.objectToMap(_oData.dWord);
            this._lSet = _oData.lSet;
            this._dMorph = helpers.objectToMap(_oData.dMorph);
................................................................................
if (typeof(browser) !== 'undefined') {
    // WebExtension
    phonet.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/phonet_data.json")));
} else if (typeof(require) !== 'undefined') {
    // Add-on SDK and Thunderbird
    phonet.init(helpers.loadFile("resource://grammalecte/fr/phonet_data.json"));
} else {
    console.log("Module phonet non initialisé");
}


if (typeof(exports) !== 'undefined') {
    exports._dWord = phonet._dWord;
    exports._lSet = phonet._lSet;
    exports._dMorph = phonet._dMorph;
    exports.init = phonet.init;
    exports.hasSimil = phonet.hasSimil;
    exports.getSimil = phonet.getSimil;
    exports.selectSimil = phonet.selectSimil;
}

Modified gc_lang/fr/webext/background.js from [67f47c2618] to [7ec98c3680].

1
2
3
4
5


6
7
8
9
10
11
12
// Background 

"use strict";

let xGCEWorker = new Worker("gce_worker.js");



function handleMessage (oRequest, xSender, sendResponse) {
  console.log(`[background] received: ${oRequest.content}`);
  sendResponse({response: "response from background script"});
}

browser.runtime.onMessage.addListener(handleMessage);





>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Background 

"use strict";

let xGCEWorker = new Worker("gce_worker.js");

xGCEWorker.postMessage(["init", browser.extension.getURL(".")]);

function handleMessage (oRequest, xSender, sendResponse) {
  console.log(`[background] received: ${oRequest.content}`);
  sendResponse({response: "response from background script"});
}

browser.runtime.onMessage.addListener(handleMessage);

Modified gc_lang/fr/webext/gce_worker.js from [ea3f39a7e2] to [0a0381f0d1].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
..
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
...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
...
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
/*
    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”
................................................................................
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);
}

................................................................................
    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);
}


................................................................................
        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();








|







 







>



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


>

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







 







|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
1
2
3
4
5
6
7
8
9
10
11
12
13
14
..
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
...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
...
170
171
172
173
174
175
176
























/*
    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”
................................................................................
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);
}

................................................................................
    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);
}


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