Index: gc_lang/fr/modules-js/conj.js ================================================================== --- gc_lang/fr/modules-js/conj.js +++ gc_lang/fr/modules-js/conj.js @@ -479,11 +479,11 @@ // Initialization if (typeof(browser) !== 'undefined') { // WebExtension conj.init(helpers.loadFile(browser.extension.getURL("grammalecte/fr/conj_data.json"))); -} else if (typeof(exports) !== 'undefined') { +} 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). Index: gc_lang/fr/modules-js/mfsp.js ================================================================== --- gc_lang/fr/modules-js/mfsp.js +++ gc_lang/fr/modules-js/mfsp.js @@ -1,58 +1,66 @@ // Grammalecte "use strict"; -if (typeof(exports) !== 'undefined') { +if (typeof(require) !== 'undefined') { var helpers = require("resource://grammalecte/helpers.js"); } - -const _oMfspData = JSON.parse(helpers.loadFile("resource://grammalecte/fr/mfsp_data.json")); - -// list of affix codes -const _lTagMiscPlur = _oMfspData.lTagMiscPlur; -const _lTagMasForm = _oMfspData.lTagMasForm; - -// dictionary of words with uncommon plurals (-x, -ux, english, latin and italian plurals) and tags to generate them -const _dMiscPlur = helpers.objectToMap(_oMfspData.dMiscPlur); - -// dictionary of feminine forms and tags to generate masculine forms (singular and plural) -const _dMasForm = helpers.objectToMap(_oMfspData.dMasForm); - const mfsp = { + // list of affix codes + _lTagMiscPlur: null, + _lTagMasForm: null, + // 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); + this._dMasForm = helpers.objectToMap(_oData.dMasForm); + } + catch (e) { + console.error(e); + } + }, + isFemForm: function (sWord) { - // returns True if sWord exists in _dMasForm - return _dMasForm.has(sWord); + // returns True if sWord exists in this._dMasForm + return this._dMasForm.has(sWord); }, getMasForm: function (sWord, bPlur) { // returns masculine form with feminine form - if (_dMasForm.has(sWord)) { + if (this._dMasForm.has(sWord)) { return [ for (sTag of this._whatSuffixCode(sWord, bPlur)) this._modifyStringWithSuffixCode(sWord, sTag) ]; } return []; }, hasMiscPlural: function (sWord) { // returns True if sWord exists in dMiscPlur - return _dMiscPlur.has(sWord); + return this._dMiscPlur.has(sWord); }, getMiscPlural: function (sWord) { // returns plural form with singular form - if (_dMiscPlur.has(sWord)) { - return [ for (sTag of _lTagMiscPlur[_dMiscPlur.get(sWord)].split("|")) this._modifyStringWithSuffixCode(sWord, sTag) ]; + if (this._dMiscPlur.has(sWord)) { + return [ for (sTag of this._lTagMiscPlur[this._dMiscPlur.get(sWord)].split("|")) this._modifyStringWithSuffixCode(sWord, sTag) ]; } return []; }, _whatSuffixCode: function (sWord, bPlur) { // necessary only for dMasFW - let sSfx = _lTagMasForm[_dMasForm.get(sWord)]; + let sSfx = this._lTagMasForm[this._dMasForm.get(sWord)]; if (sSfx.includes("/")) { if (bPlur) { return sSfx.slice(sSfx.indexOf("/")+1).split("|"); } return sSfx.slice(0, sSfx.indexOf("/")).split("|"); @@ -80,14 +88,26 @@ return "## erreur, code : " + sSfx + " ##"; } } } + +// Initialization +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.isFemForm = mfsp.isFemForm; exports.getMasForm = mfsp.getMasForm; exports.hasMiscPlural = mfsp.hasMiscPlural; exports.getMiscPlural = mfsp.getMiscPlural; exports._whatSuffixCode = mfsp._whatSuffixCode; exports._modifyStringWithSuffixCode = mfsp._modifyStringWithSuffixCode; } Index: gc_lang/fr/webext/gce_worker.js ================================================================== --- gc_lang/fr/webext/gce_worker.js +++ gc_lang/fr/webext/gce_worker.js @@ -23,10 +23,12 @@ helpers.echo("START"); helpers.echo(conj.getConj("devenir", ":E", ":2s")); +helpers.echo(mfsp.getMasForm("emmerdeuse", true)); +helpers.echo(mfsp.getMasForm("pointilleuse", false)); let oTokenizer = null; let oDict = null; let oLxg = null; Index: gc_lang/fr/webext/manifest.json ================================================================== --- gc_lang/fr/webext/manifest.json +++ gc_lang/fr/webext/manifest.json @@ -33,10 +33,11 @@ "scripts": [ "grammalecte/helpers.js", "grammalecte/text.js", "grammalecte/tokenizer.js", "grammalecte/fr/conj.js", + "grammalecte/fr/mfsp.js", "gce_worker.js" ] }, "web_accessible_resources": [ "grammalecte/fr/conj_data.json",