Index: compile_rules.py ================================================================== --- compile_rules.py +++ compile_rules.py @@ -54,11 +54,11 @@ s = re.sub(r"after0_chk1\(\s*", 'look_chk1(dDA, sx[m.end():], m.end(), ', s) # after0_chk1(s) s = re.sub(r"textarea0_chk1\(\s*", 'look_chk1(dDA, sx, 0, ', s) # textarea0_chk1(s) s = re.sub(r"isEndOfNG\(\s*\)", 'isEndOfNG(dDA, s[m.end():], m.end())', s) # isEndOfNG(s) s = re.sub(r"isNextNotCOD\(\s*\)", 'isNextNotCOD(dDA, s[m.end():], m.end())', s) # isNextNotCOD(s) s = re.sub(r"isNextVerb\(\s*\)", 'isNextVerb(dDA, s[m.end():], m.end())', s) # isNextVerb(s) - s = re.sub(r"\bspell *[(]", '_oDict.isValid(', s) + s = re.sub(r"\bspell *[(]", '_oSpellChecker.isValid(', s) s = re.sub(r"[\\](\d+)", 'm.group(\\1)', s) return s def uppercase (s, sLang): Index: gc_core/js/lang_core/gc_engine.js ================================================================== --- gc_core/js/lang_core/gc_engine.js +++ gc_core/js/lang_core/gc_engine.js @@ -36,11 +36,11 @@ // data let _sAppContext = ""; // what software is running let _dOptions = null; let _aIgnoredRules = new Set(); -let _oDict = null; +let _oSpellChecker = null; let _dAnalyses = new Map(); // cache for data from dictionary var gc_engine = { @@ -320,14 +320,14 @@ //// Initialization load: function (sContext="JavaScript", sPath="") { try { if (typeof(require) !== 'undefined') { - var ibdawg = require("resource://grammalecte/graphspell/ibdawg.js"); - _oDict = new ibdawg.IBDAWG("${dic_filename}.json"); + var spellchecker = require("resource://grammalecte/graphspell/spellchecker.js"); + _oSpellChecker = new spellchecker.SpellChecker("${lang}", "", "${dic_filename}.json"); } else { - _oDict = new IBDAWG("${dic_filename}.json", sPath); + _oSpellChecker = new SpellChecker("${lang}", sPath, "${dic_filename}.json"); } _sAppContext = sContext; _dOptions = gc_options.getOptions(sContext).gl_shallowCopy(); // duplication necessary, to be able to reset to default } catch (e) { @@ -334,11 +334,11 @@ helpers.logerror(e); } }, getDictionary: function () { - return _oDict; + return _oSpellChecker; }, //// Options setOption: function (sOpt, bVal) { @@ -388,13 +388,13 @@ helpers.echo("FSA: " + _dAnalyses.get(aWord[1])); return true; } function _storeMorphFromFSA (sWord) { - // retrieves morphologies list from _oDict -> _dAnalyses - //helpers.echo("register: "+sWord + " " + _oDict.getMorph(sWord).toString()) - _dAnalyses.set(sWord, _oDict.getMorph(sWord)); + // retrieves morphologies list from _oSpellChecker -> _dAnalyses + //helpers.echo("register: "+sWord + " " + _oSpellChecker.getMorph(sWord).toString()) + _dAnalyses.set(sWord, _oSpellChecker.getMorph(sWord)); return !!_dAnalyses.get(sWord); } function morph (dDA, aWord, sPattern, bStrict=true, bNoWord=false) { // analyse a tuple (position, word), return true if sPattern in morphologies (disambiguation on) Index: gc_core/py/lang_core/gc_engine.py ================================================================== --- gc_core/py/lang_core/gc_engine.py +++ gc_core/py/lang_core/gc_engine.py @@ -6,11 +6,11 @@ import os import traceback #import unicodedata from itertools import chain -from ..graphspell.ibdawg import IBDAWG +from ..graphspell.spellchecker import SpellChecker from ..graphspell.echo import echo from . import gc_options __all__ = [ "lang", "locales", "pkg", "name", "version", "author", \ @@ -32,11 +32,11 @@ # data _sAppContext = "" # what software is running _dOptions = None _aIgnoredRules = set() -_oDict = None +_oSpellChecker = None _dAnalyses = {} # cache for data from dictionary #### Parsing @@ -286,15 +286,15 @@ except ImportError: _createError = _createDictError def load (sContext="Python"): - global _oDict + global _oSpellChecker global _sAppContext global _dOptions try: - _oDict = IBDAWG("${dic_filename}.bdic") + _oSpellChecker = SpellChecker("${lang}", "${dic_filename}.bdic") _sAppContext = sContext _dOptions = dict(gc_options.getOptions(sContext)) # duplication necessary, to be able to reset to default except: traceback.print_exc() @@ -332,11 +332,11 @@ global _dOptions _dOptions = dict(gc_options.getOptions(_sAppContext)) def getDictionary (): - return _oDict + return _oSpellChecker def _getRules (bParagraph): try: if not bParagraph: @@ -396,13 +396,13 @@ echo("FSA: " + str(_dAnalyses[tWord[1]])) return True def _storeMorphFromFSA (sWord): - "retrieves morphologies list from _oDict -> _dAnalyses" + "retrieves morphologies list from _oSpellChecker -> _dAnalyses" global _dAnalyses - _dAnalyses[sWord] = _oDict.getMorph(sWord) + _dAnalyses[sWord] = _oSpellChecker.getMorph(sWord) return True if _dAnalyses[sWord] else False def morph (dDA, tWord, sPattern, bStrict=True, bNoWord=False): "analyse a tuple (position, word), return True if sPattern in morphologies (disambiguation on)" Index: gc_lang/fr/modules-js/gce_suggestions.js ================================================================== --- gc_lang/fr/modules-js/gce_suggestions.js +++ gc_lang/fr/modules-js/gce_suggestions.js @@ -208,21 +208,21 @@ } } let aSugg = new Set(); if (!sFlex.includes("-")) { if (sFlex.endsWith("l")) { - if (sFlex.endsWith("al") && sFlex.length > 2 && _oDict.isValid(sFlex.slice(0,-1)+"ux")) { + if (sFlex.endsWith("al") && sFlex.length > 2 && _oSpellChecker.isValid(sFlex.slice(0,-1)+"ux")) { aSugg.add(sFlex.slice(0,-1)+"ux"); } - if (sFlex.endsWith("ail") && sFlex.length > 3 && _oDict.isValid(sFlex.slice(0,-2)+"ux")) { + if (sFlex.endsWith("ail") && sFlex.length > 3 && _oSpellChecker.isValid(sFlex.slice(0,-2)+"ux")) { aSugg.add(sFlex.slice(0,-2)+"ux"); } } - if (_oDict.isValid(sFlex+"s")) { + if (_oSpellChecker.isValid(sFlex+"s")) { aSugg.add(sFlex+"s"); } - if (_oDict.isValid(sFlex+"x")) { + if (_oSpellChecker.isValid(sFlex+"x")) { aSugg.add(sFlex+"x"); } } if (mfsp.hasMiscPlural(sFlex)) { mfsp.getMiscPlural(sFlex).forEach(function(x) { aSugg.add(x); }); @@ -238,18 +238,18 @@ if (sFlex.includes("-")) { return ""; } let aSugg = new Set(); if (sFlex.endsWith("ux")) { - if (_oDict.isValid(sFlex.slice(0,-2)+"l")) { + if (_oSpellChecker.isValid(sFlex.slice(0,-2)+"l")) { aSugg.add(sFlex.slice(0,-2)+"l"); } - if (_oDict.isValid(sFlex.slice(0,-2)+"il")) { + if (_oSpellChecker.isValid(sFlex.slice(0,-2)+"il")) { aSugg.add(sFlex.slice(0,-2)+"il"); } } - if (_oDict.isValid(sFlex.slice(0,-1))) { + if (_oSpellChecker.isValid(sFlex.slice(0,-1))) { aSugg.add(sFlex.slice(0,-1)); } if (aSugg.size > 0) { return Array.from(aSugg).join("|"); } Index: gc_lang/fr/modules/gce_suggestions.py ================================================================== --- gc_lang/fr/modules/gce_suggestions.py +++ gc_lang/fr/modules/gce_suggestions.py @@ -157,17 +157,17 @@ elif sGender == ":f": return suggFemPlur(sFlex) aSugg = set() if "-" not in sFlex: if sFlex.endswith("l"): - if sFlex.endswith("al") and len(sFlex) > 2 and _oDict.isValid(sFlex[:-1]+"ux"): + if sFlex.endswith("al") and len(sFlex) > 2 and _oSpellChecker.isValid(sFlex[:-1]+"ux"): aSugg.add(sFlex[:-1]+"ux") - if sFlex.endswith("ail") and len(sFlex) > 3 and _oDict.isValid(sFlex[:-2]+"ux"): + if sFlex.endswith("ail") and len(sFlex) > 3 and _oSpellChecker.isValid(sFlex[:-2]+"ux"): aSugg.add(sFlex[:-2]+"ux") - if _oDict.isValid(sFlex+"s"): + if _oSpellChecker.isValid(sFlex+"s"): aSugg.add(sFlex+"s") - if _oDict.isValid(sFlex+"x"): + if _oSpellChecker.isValid(sFlex+"x"): aSugg.add(sFlex+"x") if mfsp.hasMiscPlural(sFlex): aSugg.update(mfsp.getMiscPlural(sFlex)) if aSugg: return "|".join(aSugg) @@ -178,15 +178,15 @@ "returns singular forms assuming sFlex is plural" if "-" in sFlex: return "" aSugg = set() if sFlex.endswith("ux"): - if _oDict.isValid(sFlex[:-2]+"l"): + if _oSpellChecker.isValid(sFlex[:-2]+"l"): aSugg.add(sFlex[:-2]+"l") - if _oDict.isValid(sFlex[:-2]+"il"): + if _oSpellChecker.isValid(sFlex[:-2]+"il"): aSugg.add(sFlex[:-2]+"il") - if _oDict.isValid(sFlex[:-1]): + if _oSpellChecker.isValid(sFlex[:-1]): aSugg.add(sFlex[:-1]) if aSugg: return "|".join(aSugg) return "" Index: gc_lang/fr/webext/gce_worker.js ================================================================== --- gc_lang/fr/webext/gce_worker.js +++ gc_lang/fr/webext/gce_worker.js @@ -34,10 +34,11 @@ importScripts("grammalecte/graphspell/helpers.js"); importScripts("grammalecte/graphspell/str_transform.js"); importScripts("grammalecte/graphspell/char_player.js"); importScripts("grammalecte/graphspell/ibdawg.js"); +importScripts("grammalecte/graphspell/spellchecker.js"); importScripts("grammalecte/text.js"); importScripts("grammalecte/graphspell/tokenizer.js"); importScripts("grammalecte/fr/conj.js"); importScripts("grammalecte/fr/mfsp.js"); importScripts("grammalecte/fr/phonet.js"); Index: graphspell-js/ibdawg.js ================================================================== --- graphspell-js/ibdawg.js +++ graphspell-js/ibdawg.js @@ -87,12 +87,11 @@ constructor (param1, sPath="") { // param1 can be a filename or a object with all the necessary data. try { let oData = null; if (typeof(param1) == "string") { - let sDicName = param1; - let sURL = (sPath !== "") ? sPath + "/" + sDicName : "resource://grammalecte/graphspell/_dictionaries/"+sDicName; + let sURL = (sPath !== "") ? sPath + "/" + param1 : "resource://grammalecte/graphspell/_dictionaries/"+param1; oData = JSON.parse(helpers.loadFile(sURL)); } else { oData = param1; } Object.assign(this, oData); Index: graphspell-js/spellchecker.js ================================================================== --- graphspell-js/spellchecker.js +++ graphspell-js/spellchecker.js @@ -23,17 +23,15 @@ ["fr", "fr.json"], ["en", "en.json"] ]); -class Spellchecker { +class SpellChecker { - constructor (sLangCode, mainDic=null, extentedDic=null, personalDic=null, sPath="") { + constructor (sLangCode, sPath="", mainDic=null, extentedDic=null, personalDic=null) { // returns true if the main dictionary is loaded this.sLangCode = sLangCode; - console.log(sLangCode); - console.log(mainDic); if (mainDic === null) { mainDic = dDefaultDictionaries.gl_get(sLangCode, ""); } this.oMainDic = this._loadDictionary(mainDic, sPath, true); this.oExtendedDic = this._loadDictionary(extentedDic, sPath); @@ -44,23 +42,21 @@ // returns an IBDAWG object if (dictionary === null) { return null; } try { - if (typeof(require) !== 'undefined') { - console.log(">>>> "); + if (typeof(require) !== 'undefined') { return new ibdawg.IBDAWG(dictionary); // dictionary can be a filename or a JSON object } else { - console.log(">>>> no "); return new IBDAWG(dictionary, sPath); // dictionary can be a filename or a JSON object } } catch (e) { - if (bNecessary) { - throw e.message; - } console.log(e.message); + if (bNecessary) { + throw e.message; + } return null; } } setMainDictionary (dictionary) { @@ -97,44 +93,44 @@ return false; } isValid (sWord) { // checks if sWord is valid (different casing tested if the first letter is a capital) - if (this.oMainDic.isValid(sToken)) { + if (this.oMainDic.isValid(sWord)) { return true; } - if (this.oExtendedDic && this.oExtendedDic.isValid(sToken)) { + if (this.oExtendedDic && this.oExtendedDic.isValid(sWord)) { return true; } - if (this.oPersonalDic && this.oPersonalDic.isValid(sToken)) { + if (this.oPersonalDic && this.oPersonalDic.isValid(sWord)) { return true; } return false; } lookup (sWord) { // checks if sWord is in dictionary as is (strict verification) - if (this.oMainDic.lookup(sToken)) { + if (this.oMainDic.lookup(sWord)) { return true; } - if (this.oExtendedDic && this.oExtendedDic.lookup(sToken)) { + if (this.oExtendedDic && this.oExtendedDic.lookup(sWord)) { return true; } - if (this.oPersonalDic && this.oPersonalDic.lookup(sToken)) { + if (this.oPersonalDic && this.oPersonalDic.lookup(sWord)) { return true; } return false; } getMorph (sWord) { // retrieves morphologies list, different casing allowed - let lResult = this.oMainDic.getMorph(sToken); + let lResult = this.oMainDic.getMorph(sWord); if (this.oExtendedDic) { - lResult.extends(this.oExtendedDic.getMorph(sToken)); + lResult.extends(this.oExtendedDic.getMorph(sWord)); } if (this.oPersonalDic) { - lResult.extends(this.oPersonalDic.getMorph(sToken)); + lResult.extends(this.oPersonalDic.getMorph(sWord)); } return lResult; } * suggest (sWord, nSuggLimit=10) { Index: graphspell/spellchecker.py ================================================================== --- graphspell/spellchecker.py +++ graphspell/spellchecker.py @@ -17,21 +17,20 @@ "fr": "fr.bdic", "en": "en.bdic" } -class Spellchecker (): +class SpellChecker (): def __init__ (self, sLangCode, sfMainDic="", sfExtendedDic="", sfPersonalDic=""): "returns True if the main dictionary is loaded" self.sLangCode = sLangCode if not sfMainDic: sfMainDic = dDefaultDictionaries.get(sLangCode, "") self.oMainDic = self._loadDictionary(sfMainDic) self.oExtendedDic = self._loadDictionary(sfExtendedDic) self.oPersonalDic = self._loadDictionary(sfPersonalDic) - return bool(self.oMainDic) def _loadDictionary (self, sfDictionary): "returns an IBDAWG object" if not sfDictionary: return None @@ -70,35 +69,35 @@ return True return False def isValid (self, sWord): "checks if sWord is valid (different casing tested if the first letter is a capital)" - if self.oMainDic.isValid(sToken): + if self.oMainDic.isValid(sWord): return True - if self.oExtendedDic and self.oExtendedDic.isValid(sToken): + if self.oExtendedDic and self.oExtendedDic.isValid(sWord): return True - if self.oPersonalDic and self.oPersonalDic.isValid(sToken): + if self.oPersonalDic and self.oPersonalDic.isValid(sWord): return True return False def lookup (self, sWord): "checks if sWord is in dictionary as is (strict verification)" - if self.oMainDic.lookup(sToken): + if self.oMainDic.lookup(sWord): return True - if self.oExtendedDic and self.oExtendedDic.lookup(sToken): + if self.oExtendedDic and self.oExtendedDic.lookup(sWord): return True - if self.oPersonalDic and self.oPersonalDic.lookup(sToken): + if self.oPersonalDic and self.oPersonalDic.lookup(sWord): return True return False def getMorph (self, sWord): "retrieves morphologies list, different casing allowed" - lResult = self.oMainDic.getMorph(sToken) + lResult = self.oMainDic.getMorph(sWord) if self.oExtendedDic: - lResult.extends(self.oExtendedDic.getMorph(sToken)) + lResult.extends(self.oExtendedDic.getMorph(sWord)) if self.oPersonalDic: - lResult.extends(self.oPersonalDic.getMorph(sToken)) + lResult.extends(self.oPersonalDic.getMorph(sWord)) return lResult def suggest (self, sWord, nSuggLimit=10): "generator: returns 1,2 or 3 lists of suggestions" yield self.oMainDic.suggest(sWord, nSuggLimit)