Grammalecte  Diff

Differences From Artifact [29bf30d9e3]:

To Artifact [79840617ea]:


    12     12   /* global require, exports, console, IBDAWG, Tokenizer */
    13     13   
    14     14   "use strict";
    15     15   
    16     16   if(typeof(process) !== 'undefined') {
    17     17       var ibdawg = require("./ibdawg.js");
    18     18       var tokenizer = require("./tokenizer.js");
           19  +    var suggest = require("./suggest.js");
    19     20   } else if (typeof(require) !== 'undefined') {
    20     21       var ibdawg = require("resource://grammalecte/graphspell/ibdawg.js");
    21     22       var tokenizer = require("resource://grammalecte/graphspell/tokenizer.js");
           23  +    var suggest = require("resource://grammalecte/graphspell/suggest.js");
    22     24   }
    23     25   
    24     26   ${map}
    25     27   
    26     28   
    27     29   const dDefaultDictionaries = new Map([
    28     30       ["fr", "fr-allvars.json"],
................................................................................
    40     42           }
    41     43           this.oMainDic = this._loadDictionary(mainDic, sPath, true);
    42     44           this.oCommunityDic = this._loadDictionary(communityDic, sPath);
    43     45           this.oPersonalDic = this._loadDictionary(personalDic, sPath);
    44     46           this.bCommunityDic = Boolean(this.oCommunityDic);
    45     47           this.bPersonalDic = Boolean(this.oPersonalDic);
    46     48           this.oTokenizer = null;
           49  +        // Default suggestions
           50  +        this.oDefaultSugg = null;
           51  +        this.loadSuggestions(sLangCode)
    47     52           // storage
    48     53           this.bStorage = false;
    49     54           this._dMorphologies = new Map();            // key: flexion, value: list of morphologies
    50     55           this._dLemmas = new Map();                  // key: flexion, value: list of lemmas
    51     56       }
    52     57   
    53     58       _loadDictionary (dictionary, sPath="", bNecessary=false) {
................................................................................
   120    125           this.bCommunityDic = false;
   121    126       }
   122    127   
   123    128       deactivatePersonalDictionary () {
   124    129           this.bPersonalDic = false;
   125    130       }
   126    131   
          132  +
          133  +    // Default suggestions
          134  +
          135  +    loadSuggestions (sLangCode) {
          136  +        // load default suggestion module for <sLangCode>
          137  +        // When “import” works everywhere, do like with Python
          138  +        if (suggest && suggest.hasOwnProperty(sLangCode)) {
          139  +            this.oDefaultSugg = suggest[sLangCode];
          140  +        }
          141  +    }
          142  +
   127    143   
   128    144       // Storage
   129    145   
   130    146       activateStorage () {
   131    147           this.bStorage = true;
   132    148       }
   133    149   
................................................................................
   229    245               return this._dLemmas.get(sWord);
   230    246           }
   231    247           return Array.from(new Set(this.getMorph(sWord).map((sMorph) => { return sMorph.slice(1, sMorph.indexOf("/")); })));
   232    248       }
   233    249   
   234    250       * suggest (sWord, nSuggLimit=10) {
   235    251           // generator: returns 1, 2 or 3 lists of suggestions
   236         -        yield this.oMainDic.suggest(sWord, nSuggLimit, true);
          252  +        if (this.oDefaultSugg) {
          253  +            if (this.oDefaultSugg.hasOwnProperty(sWord)) {
          254  +                yield this.oDefaultSugg[sWord].split("|");
          255  +            } else if (sWord.gl_isTitle() && this.oDefaultSugg.hasOwnProperty(sWord.lower())) {
          256  +                let lRes = this.oDefaultSugg[sWord.toLowerCase()].split("|");
          257  +                yield lRes.map((sSugg) => { return sSugg.slice(0,1).toUpperCase() + sSugg.slice(1); });
          258  +            } else {
          259  +                yield this.oMainDic.suggest(sWord, nSuggLimit, true);
          260  +            }
          261  +        } else {
          262  +            yield this.oMainDic.suggest(sWord, nSuggLimit, true);
          263  +        }
   237    264           if (this.bCommunityDic) {
   238    265               yield this.oCommunityDic.suggest(sWord, Math.floor(nSuggLimit/2)+1);
   239    266           }
   240    267           if (this.bPersonalDic) {
   241    268               yield this.oPersonalDic.suggest(sWord, Math.floor(nSuggLimit/2)+1);
   242    269           }
   243    270       }