Index: graphspell-js/char_player.js ================================================================== --- graphspell-js/char_player.js +++ graphspell-js/char_player.js @@ -1,8 +1,11 @@ // list of similar chars // useful for suggestion mechanism +/* jshint esversion:6 */ +/* jslint esversion:6 */ + ${map} var char_player = { @@ -23,11 +26,11 @@ ['â', 'a'], ['è', 'e'], ['ï', 'i'], ['ö', 'o'], ['ù', 'u'], ['ŷ', 'i'], ['ä', 'a'], ['ê', 'e'], ['í', 'i'], ['ó', 'o'], ['ü', 'u'], ['ý', 'i'], ['á', 'a'], ['ë', 'e'], ['ì', 'i'], ['ò', 'o'], ['ú', 'u'], ['ỳ', 'i'], ['ā', 'a'], ['ē', 'e'], ['ī', 'i'], ['ō', 'o'], ['ū', 'u'], ['ȳ', 'i'], ['ç', 'c'], ['ñ', 'n'], ['k', 'q'], ['w', 'v'], - ['œ', 'oe'], ['æ', 'ae'], + ['œ', 'oe'], ['æ', 'ae'], ['ſ', 's'], ['ffi', 'ffi'], ['ffl', 'ffl'], ['ff', 'ff'], ['ſt', 'ft'], ['fi', 'fi'], ['fl', 'fl'], ['st', 'st'] ]), simplifyWord: function (sWord) { // word simplication before calculating distance between words @@ -102,11 +105,11 @@ ["f", "fF"], ["F", "Ff"], ["g", "gGjJĵĴ"], ["G", "GgJjĴĵ"], - + ["h", "hH"], ["H", "Hh"], ["i", "iIîÎïÏyYíÍìÌīĪÿŸ"], ["I", "IiÎîÏïYyÍíÌìĪīŸÿ"], @@ -380,14 +383,14 @@ // Other functions filterSugg: function (aSugg) { return aSugg.filter((sSugg) => { return !sSugg.endsWith("è") && !sSugg.endsWith("È"); }); } -} +}; -if (typeof(exports) !== 'undefined') { +if (typeof exports !== 'undefined') { exports._xTransCharsForSpelling = char_player._xTransCharsForSpelling; exports.spellingNormalization = char_player.spellingNormalization; exports._xTransCharsForSimplification = char_player._xTransCharsForSimplification; exports.simplifyWord = char_player.simplifyWord; exports.aVowel = char_player.aVowel; Index: graphspell-js/dawg.js ================================================================== --- graphspell-js/dawg.js +++ graphspell-js/dawg.js @@ -6,17 +6,21 @@ // License: MPL 2 // // This tool encodes lexicon into an indexable binary dictionary // Input files MUST be encoded in UTF-8. +/* jshint esversion:6, -W097 */ +/* jslint esversion:6 */ +/* global require, exports, console, helpers */ + "use strict"; - -if (typeof(require) !== 'undefined') { - var str_transform = require("resource://grammalecte/graphspell/str_transform.js"); +if(typeof process !== 'undefined') { + var str_transform = require('./str_transform.js'); +} else if (typeof require !== 'undefined') { + var str_transform = require('resource://grammalecte/graphspell/str_transform.js'); } - ${map} class DAWG { @@ -98,11 +102,11 @@ let lTemp = []; for (let c of sFlex) { lTemp.push(dChar.get(c)); } lTemp.push(iAff+nChar); - lTemp.push(iTag+nChar+nAff) + lTemp.push(iTag+nChar+nAff); lWord.push(lTemp); } lEntry.length = 0; // clear the array // Dictionary of arc values occurrency, to sort arcs of each node @@ -428,11 +432,11 @@ }, reset: function () { this.nNextId = 0; } -} +}; class DawgNode { constructor () { @@ -542,11 +546,11 @@ let sHexVal = nVal.toString(16); // conversion to hexadecimal string //console.log(`value: ${nVal} in ${nByte} bytes`); if (sHexVal.length < (nByte*2)) { return "0".repeat((nByte*2) - sHexVal.length) + sHexVal; } else if (sHexVal.length == (nByte*2)) { - return sHexVal + return sHexVal; } else { throw "Conversion to byte string: value bigger than allowed."; } } } Index: graphspell-js/helpers.js ================================================================== --- graphspell-js/helpers.js +++ graphspell-js/helpers.js @@ -1,9 +1,10 @@ - // HELPERS -/*jslint esversion: 6*/ -/*global console,require,exports,XMLHttpRequest*/ + +/* jshint esversion:6, -W097 */ +/* jslint esversion:6 */ +/* global require, exports, console, XMLHttpRequest */ "use strict"; var helpers = { @@ -20,17 +21,23 @@ loadFile: function (spf) { // load ressources in workers (suggested by Mozilla extensions reviewers) // for more options have a look here: https://gist.github.com/Noitidart/ec1e6b9a593ec7e3efed // if not in workers, use sdk/data.load() instead try { - console.log("loadFile: " + spf); - let xRequest; - xRequest = new XMLHttpRequest(); - xRequest.open('GET', spf, false); // 3rd arg is false for synchronous, sync is acceptable in workers - xRequest.overrideMimeType('text/json'); - xRequest.send(); - return xRequest.responseText; + if(typeof process !== 'undefined' && typeof require !== 'undefined') { + //console.log('loadFile(disque): ' + spf); + let fs = require('fs'); + return fs.readFileSync(spf, 'utf8'); + } else { + //console.log('loadFile: ' + spf); + let xRequest; + xRequest = new XMLHttpRequest(); + xRequest.open('GET', spf, false); // 3rd arg is false for synchronous, sync is acceptable in workers + xRequest.overrideMimeType('text/json'); + xRequest.send(); + return xRequest.responseText; + } } catch (e) { console.error(e); return null; } @@ -59,11 +66,11 @@ return obj; } }; -if (typeof(exports) !== 'undefined') { +if (typeof exports !== 'undefined') { exports.inspect = helpers.inspect; exports.loadFile = helpers.loadFile; exports.objectToMap = helpers.objectToMap; exports.mapToObject = helpers.mapToObject; } Index: graphspell-js/ibdawg.js ================================================================== --- graphspell-js/ibdawg.js +++ graphspell-js/ibdawg.js @@ -1,18 +1,22 @@ -//// IBDAWG -/*jslint esversion: 6*/ -/*global console,require,exports*/ +// IBDAWG + +/* jshint esversion:6, -W097 */ +/* jslint esversion:6 */ +/* global require, exports, console*/ "use strict"; - -if (typeof(require) !== 'undefined') { - var str_transform = require("resource://grammalecte/graphspell/str_transform.js"); - var helpers = require("resource://grammalecte/graphspell/helpers.js"); - var char_player = require("resource://grammalecte/graphspell/char_player.js"); +if(typeof process !== 'undefined') { + var str_transform = require('./str_transform.js'); + var helpers = require('./helpers.js'); + var char_player = require('./char_player.js'); +} else if (typeof require !== 'undefined') { + var str_transform = require('resource://grammalecte/graphspell/str_transform.js'); + var helpers = require('resource://grammalecte/graphspell/helpers.js'); + var char_player = require('resource://grammalecte/graphspell/char_player.js'); } - // Don’t remove . Necessary in TB. ${string} ${map} ${set} @@ -221,11 +225,11 @@ return oJSON; } isValidToken (sToken) { // checks if sToken is valid (if there is hyphens in sToken, sToken is split, each part is checked) - sToken = char_player.spellingNormalization(sToken) + sToken = char_player.spellingNormalization(sToken); if (this.isValid(sToken)) { return true; } if (sToken.includes("-")) { if (sToken.gl_count("-") > 4) { @@ -298,11 +302,11 @@ return Boolean(this._convBytesToInteger(this.byDic.slice(iAddr, iAddr+this.nBytesArc)) & this._finalNodeMask); } getMorph (sWord) { // retrieves morphologies list, different casing allowed - sWord = char_player.spellingNormalization(sWord) + sWord = char_player.spellingNormalization(sWord); let l = this.morph(sWord); if (sWord[0].gl_isUpperCase()) { l.push(...this.morph(sWord.toLowerCase())); if (sWord.gl_isUpperCase() && sWord.length > 1) { l.push(...this.morph(sWord.gl_toCapitalize())); @@ -311,12 +315,12 @@ return l; } suggest (sWord, nSuggLimit=10) { // returns a array of suggestions for - //const t0 = Date.now(); - sWord = char_player.spellingNormalization(sWord) + //console.time("Suggestions for " + sWord + "); + sWord = char_player.spellingNormalization(sWord); let sPfx = ""; let sSfx = ""; [sPfx, sWord, sSfx] = char_player.cut(sWord); let nMaxSwitch = Math.max(Math.floor(sWord.length / 3), 1); let nMaxDel = Math.floor(sWord.length / 5); @@ -325,14 +329,13 @@ let oSuggResult = new SuggResult(sWord); this._suggest(oSuggResult, sWord, nMaxSwitch, nMaxDel, nMaxHardRepl, nMaxJump); let aSugg = oSuggResult.getSuggestions(nSuggLimit); if (sSfx || sPfx) { // we add what we removed - return aSugg.map( (sSugg) => { return sPfx + sSugg + sSfx } ); + return aSugg.map( (sSugg) => { return sPfx + sSugg + sSfx; } ); } - //const t1 = Date.now(); - //console.log("Suggestions for " + sWord + " in " + ((t1-t0)/1000).toString() + " s"); + //console.timeEnd("Suggestions for " + sWord + "); return aSugg; } _suggest (oSuggResult, sRemain, nMaxSwitch=0, nMaxDel=0, nMaxHardRepl=0, nMaxJump=0, nDist=0, nDeep=0, iAddr=0, sNewWord="", bAvoidLoop=false) { // returns a set of suggestions @@ -589,11 +592,11 @@ } } } * _getArcs1 (iAddr) { - "generator: return all arcs at as tuples of (nVal, iAddr)" + // generator: return all arcs at as tuples of (nVal, iAddr) while (true) { let iEndArcAddr = iAddr+this.nBytesArc; let nRawArc = this._convBytesToInteger(this.byDic.slice(iAddr, iEndArcAddr)); yield [nRawArc & this._arcMask, this._convBytesToInteger(this.byDic.slice(iEndArcAddr, iEndArcAddr+this.nBytesNodeAddress))]; if (nRawArc & this._lastArcMask) { @@ -630,8 +633,8 @@ // to do } } -if (typeof(exports) !== 'undefined') { +if (typeof exports !== 'undefined') { exports.IBDAWG = IBDAWG; } Index: graphspell-js/spellchecker.js ================================================================== --- graphspell-js/spellchecker.js +++ graphspell-js/spellchecker.js @@ -6,19 +6,23 @@ // - the main dictionary, bundled with the package // - the extended dictionary // - the community dictionary, added by an organization // - the personal dictionary, created by the user for its own convenience +/* jshint esversion:6, -W097 */ +/* jslint esversion:6 */ +/* global require, exports, console, IBDAWG, Tokenizer */ "use strict"; - -if (typeof(require) !== 'undefined') { - var ibdawg = require("resource://grammalecte/graphspell/ibdawg.js"); - var tokenizer = require("resource://grammalecte/graphspell/tokenizer.js"); +if(typeof process !== 'undefined') { + var ibdawg = require('./ibdawg.js'); + var tokenizer = require('./tokenizer.js'); +} else if (typeof require !== 'undefined') { + var ibdawg = require('resource://grammalecte/graphspell/ibdawg.js'); + var tokenizer = require('resource://grammalecte/graphspell/tokenizer.js'); } - ${map} const dDefaultDictionaries = new Map([ @@ -64,11 +68,11 @@ catch (e) { let sfDictionary = (typeof(dictionary) == "string") ? dictionary : dictionary.sLangName + "/" + dictionary.sFileName; if (bNecessary) { throw "Error: <" + sfDictionary + "> not loaded. " + e.message; } - console.log("Error: <" + sfDictionary + "> not loaded.") + console.log("Error: <" + sfDictionary + "> not loaded."); console.log(e.message); return null; } } @@ -195,11 +199,11 @@ return true; } if (this.bExtendedDic && this.oExtendedDic.isValid(sWord)) { return true; } - if (this.bCommunityDic && this.oCommunityDic.isValid(sToken)) { + if (this.bCommunityDic && this.oCommunityDic.isValid(sWord)) { return true; } if (this.bPersonalDic && this.oPersonalDic.isValid(sWord)) { return true; } @@ -212,11 +216,11 @@ return true; } if (this.bExtendedDic && this.oExtendedDic.lookup(sWord)) { return true; } - if (this.bCommunityDic && this.oCommunityDic.lookup(sToken)) { + if (this.bCommunityDic && this.oCommunityDic.lookup(sWord)) { return true; } if (this.bPersonalDic && this.oPersonalDic.lookup(sWord)) { return true; } @@ -271,11 +275,11 @@ } } * select (sFlexPattern="", sTagsPattern="") { // generator: returns all entries which flexion fits and morphology fits - yield* this.oMainDic.select(sFlexPattern, sTagsPattern) + yield* this.oMainDic.select(sFlexPattern, sTagsPattern); if (this.bExtendedDic) { yield* this.oExtendedDic.select(sFlexPattern, sTagsPattern); } if (this.bCommunityDic) { yield* this.oCommunityDic.select(sFlexPattern, sTagsPattern); @@ -299,8 +303,8 @@ } return lResult; } } -if (typeof(exports) !== 'undefined') { +if (typeof exports !== 'undefined') { exports.SpellChecker = SpellChecker; } Index: graphspell-js/str_transform.js ================================================================== --- graphspell-js/str_transform.js +++ graphspell-js/str_transform.js @@ -1,7 +1,10 @@ -//// STRING TRANSFORMATION -/*jslint esversion: 6*/ +// STRING TRANSFORMATION + +/* jshint esversion:6, -W097 */ +/* jslint esversion:6 */ +/* global exports, console */ "use strict"; // Note: 48 is the ASCII code for "0" @@ -218,15 +221,15 @@ return sSfxCode[0] == '0' ? sWord + sSfxCode.slice(1) : sWord.slice(0, -(sSfxCode.charCodeAt(0)-48)) + sSfxCode.slice(1); } }; -if (typeof(exports) !== 'undefined') { +if (typeof exports !== 'undefined') { exports.longestCommonSubstring = str_transform.longestCommonSubstring; exports.distanceDamerauLevenshtein = str_transform.distanceDamerauLevenshtein; exports.distanceDamerauLevenshtein2 = str_transform.distanceDamerauLevenshtein2; exports.showDistance = str_transform.showDistance; exports.changeWordWithSuffixCode = str_transform.changeWordWithSuffixCode; exports.changeWordWithAffixCode = str_transform.changeWordWithAffixCode; exports.defineAffixCode = str_transform.defineAffixCode; exports.defineSuffixCode = str_transform.defineSuffixCode; } Index: graphspell-js/tokenizer.js ================================================================== --- graphspell-js/tokenizer.js +++ graphspell-js/tokenizer.js @@ -1,9 +1,11 @@ // JavaScript // Very simple tokenizer -/*jslint esversion: 6*/ -/*global require,exports*/ + +/* jshint esversion:6, -W097 */ +/* jslint esversion:6 */ +/*global require, exports, console*/ "use strict"; const aTkzPatterns = { @@ -70,11 +72,11 @@ for (let [zRegex, sType] of this.aRules) { if (sType !== "SPACE" || bWithSpaces) { try { if ((m = zRegex.exec(sText)) !== null) { iToken += 1; - yield { "i": iToken, "sType": sType, "sValue": m[0], "nStart": iNext, "nEnd": iNext + m[0].length } + yield { "i": iToken, "sType": sType, "sValue": m[0], "nStart": iNext, "nEnd": iNext + m[0].length }; iCut = m[0].length; break; } } catch (e) { @@ -90,8 +92,8 @@ } } } -if (typeof(exports) !== 'undefined') { +if (typeof exports !== 'undefined') { exports.Tokenizer = Tokenizer; }