Skip to content

Commit

Permalink
Replace typo.js with nspell- which is much much faster at long word s…
Browse files Browse the repository at this point in the history
…uggestions and is way more maintained

Typo js is very very slow at suggestions for long misspelled words! It's also not as popular atm.
See cfinke/Typo.js#56
  • Loading branch information
blurymind committed Dec 12, 2018
1 parent e9c4d59 commit 583edaf
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions spellcheck_ace.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// You also need to load in typo.js and jquery.js

// You also need to load in nspell.js and jquery.js
const nspell = require('nspell');
// You should configure these classes.
var editor = "editor"; // This should be the id of your editor element.
var lang = "en_US";
var dicPath = "dictionaries/en_US/en_US.dic";
var affPath = "dictionaries/en_US/en_US.aff";
const editor = "editor"; // This should be the id of your editor element.

const dicPath = "dictionaries/en_US/en_US.dic";
const affPath = "dictionaries/en_US/en_US.aff";

// Make red underline for gutter and words.
$("<style type='text/css'>.ace_marker-layer .misspelled { position: absolute; z-index: -2; border-bottom: 1px solid red; margin-bottom: -1px; }</style>").appendTo("head");
Expand All @@ -20,7 +20,7 @@ $.get(dicPath, function(data) {
affData = data;
}).done(function() {
console.log("Dictionary loaded");
dictionary = new Typo(lang, affData, dicData);
dictionary = new nspell(affData, dicData);
});
});

Expand All @@ -32,7 +32,7 @@ function misspelled(line) {
for (word in words) {
var x = words[word] + "";
var checkWord = x.replace(/[^a-zA-Z\-']/g, '');
if (!dictionary.check(checkWord)) {
if (!dictionary.correct(checkWord)) {
bads[bads.length] = [i, i + words[word].length];
}
i += words[word].length + 1;
Expand Down Expand Up @@ -121,10 +121,8 @@ function clear_spellcheck_markers() {
};
}

function suggest_word_for_misspelled(misspelledWord) {
var is_spelled_correctly = dictionary.check(misspelledWord);

function suggest_word_for_misspelled(misspelledWord) {
var array_of_suggestions = dictionary.suggest(misspelledWord);
if (is_spelled_correctly || array_of_suggestions.length === 0) { return false }
if (array_of_suggestions.length === 0) { return false }
return array_of_suggestions
}

0 comments on commit 583edaf

Please sign in to comment.