Skip to content

Commit

Permalink
Bad word caching
Browse files Browse the repository at this point in the history
hss-dev committed Sep 20, 2017

Verified

This commit was signed with the committer’s verified signature.
jloleysens Jean-Louis Leysens
1 parent 27f14b2 commit 274e5b9
Showing 2 changed files with 93 additions and 69 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-spell-check-provider",
"version": "v1.0.0-10",
"version": "v1.0.0-11",
"description": "Because Electron's spell-check APIs are just a little too low-level.",
"main": "src/index.js",
"scripts": {
160 changes: 92 additions & 68 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -13,84 +13,108 @@ var util = require('util');
* @return {SpellCheckProvider}
*/
var SpellCheckProvider = function() {
EventEmitter.call(this);
init();
EventEmitter.call(this);
init();
};

function init() {
dictionary(function(err, dict) {
if (err) {
throw err;
}
dictionary(function(err, dict) {
if (err) {
throw err;
}

this._nspellInstance = nspell(dict);

console.log("processing " + wordlist.length + " words");
// initialise missing words from our wordlist
for (i = 0; i < wordlist.length; i += 2) {
var ukTerm = wordlist[i];
var usEquiv = wordlist[i + 1];
this._nspellInstance = nspell(dict);

if (ukTerm !== usEquiv) {
if (this._nspellInstance.spell(usEquiv).correct) {
this._nspellInstance.remove(usEquiv);
}
}
if (!this._nspellInstance.spell(ukTerm).correct) {
this._nspellInstance.add(ukTerm);
}
}
});
console.log("processing " + wordlist.length + " words");
// initialise missing words from our wordlist
for (i = 0; i < wordlist.length; i += 2) {
var ukTerm = wordlist[i];
var usEquiv = wordlist[i + 1];

if (ukTerm !== usEquiv) {
if (this._nspellInstance.spell(usEquiv).correct) {
this._nspellInstance.remove(usEquiv);
}
}
if (!this._nspellInstance.spell(ukTerm).correct) {
this._nspellInstance.add(ukTerm);
}
}

const timeThis = "HELLLO";
console.time("Time suggestion list for " + timeThis);
console.log(_nspellInstance.suggest(timeThis));
console.timeEnd("Time suggestion list for " + timeThis);

});
};

util.inherits(SpellCheckProvider, EventEmitter);

let last = new Map();

_.extend(SpellCheckProvider.prototype, {
spellCheck : function(text) {
if (text !== '' && !this.correct(text)) {
this.emit('misspelling', _nspellInstance.suggest(text));
return false;
} else {
this.emit('clear');
return true;
}
},
suggest : function(text) {
if (text !== '') {
return _nspellInstance.suggest(text);
}
return [];
},
correct : function(text) {
if (text !== '') {
return _nspellInstance.correct(text);
}
return true;
},
spell : function(text) {
if (text !== '') {
return _nspellInstance.spell(text);
}
},
add : function(text) {
this.emit('add', text, add => {
_nspellInstance.add(text);
});
},
addInternal : function(text) {
_nspellInstance.add(text);
},
remove : function(text) {
this.emit('remove', text, remove => {
_nspellInstance.remove(text);
});
},
reinitialise : function() {
init();
},
getWordList : function() {
return wordlist;
}
spellCheck: function(text, event) {
if (text !== '' && !this.correct(text)) {
if (last.has(text)) {
this.emit('misspelling', last.get(text));
return false;
}
const suggestions = _nspellInstance.suggest(text);
// console.log("'" + text + "' is wrong we suggest");
// suggestions.forEach((s, i) => console.log(i + 1 + ". " + s));

this.emit('misspelling', suggestions);
if (last.size > 50) {
// get rid of first 50....
const mapIter = myMap.keys();
for (let i = 0; i < 25; i++) {
last.delete(mapIter.next().value);
}
}
last.set(text, suggestions);
return false;
} else {
this.emit('clear');
return true;
}
},
suggest: function(text) {
if (text !== '') {
return _nspellInstance.suggest(text);
}
return [];
},
correct: function(text) {
if (text !== '') {
return _nspellInstance.correct(text);
}
return true;
},
spell: function(text) {
if (text !== '') {
return _nspellInstance.spell(text);
}
},
add: function(text) {
this.emit('add', text, add => {
_nspellInstance.add(text);
});
},
addInternal: function(text) {
_nspellInstance.add(text);
},
remove: function(text) {
this.emit('remove', text, remove => {
_nspellInstance.remove(text);
});
},
reinitialise: function() {
init();
},
getWordList: function() {
return wordlist;
}
});

module.exports = SpellCheckProvider;

0 comments on commit 274e5b9

Please sign in to comment.