Index: graphspell-js/spellchecker.js ================================================================== --- graphspell-js/spellchecker.js +++ graphspell-js/spellchecker.js @@ -132,15 +132,47 @@ loadLexicographer (sLangCode) { // load default suggestion module for if (typeof(process) !== 'undefined') { this.lexicographer = require(`./lexgraph_${sLangCode}.js`); } - else if (typeof(require) !== 'undefined') { - this.lexicographer = require(`resource://grammalecte/graphspell/lexgraph_${sLangCode}.js`); + else if (self && self.hasOwnProperty("lexgraph_"+sLangCode)) { // self is the Worker + this.lexicographer = self["lexgraph_"+sLangCode]; + } + } + + analyze (sWord) { + // returns a list of words and their morphologies + if (!this.lexicographer) { + return []; + } + let lWordAndMorph = []; + for (let sElem of this.lexicographer.split(sWord)) { + if (sElem) { + let lMorph = this.getMorph(sElem); + let sLex = this.lexicographer.analyze(sElem) + let aRes = []; + if (sLex) { + aRes = [ [lMorph.join(" | "), sLex] ]; + } else { + for (let sMorph of lMorph) { + aRes.push([sMorph, this.lexicographer.formatTags(sMorph)]); + } + } + if (aRes.length > 0) { + lWordAndMorph.push([sElem, aRes]); + } + } } + return lWordAndMorph; } + readableMorph (sMorph) { + if (!this.lexicographer) { + return []; + } + return this.lexicographer.formatTags(sMorph); + } // Storage activateStorage () { this.bStorage = true; Index: graphspell/spellchecker.py ================================================================== --- graphspell/spellchecker.py +++ graphspell/spellchecker.py @@ -98,11 +98,11 @@ def deactivatePersonalDictionary (self): "deactivate personal dictionary" self.bPersonalDic = False - # Default suggestions + # Lexicographer def loadLexicographer (self, sLangCode): "load default suggestion module for " try: self.lexicographer = importlib.import_module(".lexgraph_"+sLangCode, "grammalecte.graphspell") @@ -125,10 +125,15 @@ aRes = [ (sMorph, self.lexicographer.formatTags(sMorph)) for sMorph in lMorph ] if aRes: lWordAndMorph.append((sElem, aRes)) return lWordAndMorph + def readableMorph (self, sMorph): + if not self.lexicographer: + return [] + return self.lexicographer.formatTags(sMorph) + # Storage def activateStorage (self): "store all lemmas and morphologies retrieved from the word graph" @@ -233,11 +238,11 @@ return self._dLemmas[sWord] return { s[1:s.find("/")] for s in self.getMorph(sWord) } def suggest (self, sWord, nSuggLimit=10): "generator: returns 1, 2 or 3 lists of suggestions" - if self.lexicographer.dSugg: + if self.lexicographer: if sWord in self.lexicographer.dSugg: yield self.lexicographer.dSugg[sWord].split("|") elif sWord.istitle() and sWord.lower() in self.lexicographer.dSugg: lRes = self.lexicographer.dSugg[sWord.lower()].split("|") yield list(map(lambda sSugg: sSugg[0:1].upper()+sSugg[1:], lRes))