Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "typo-js", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "A Hunspell-style spellchecker.", | ||
@@ -5,0 +5,0 @@ "main": "typo.js", |
152
typo.js
@@ -265,11 +265,3 @@ /* globals chrome: false */ | ||
if (fs.existsSync(path)) { | ||
var stats = fs.statSync(path); | ||
var fileDescriptor = fs.openSync(path, 'r'); | ||
var buffer = new Buffer(stats.size); | ||
fs.readSync(fileDescriptor, buffer, 0, buffer.length, null); | ||
return buffer.toString(charset, 0, buffer.length); | ||
return fs.readFileSync(path, charset); | ||
} | ||
@@ -302,3 +294,3 @@ else { | ||
var lines = data.split("\n"); | ||
var lines = data.split(/\r?\n/); | ||
@@ -435,3 +427,3 @@ for (i = 0, _len = lines.length; i < _len; i++) { | ||
var lines = data.split("\n"); | ||
var lines = data.split(/\r?\n/); | ||
var dictionaryTable = {}; | ||
@@ -458,2 +450,7 @@ | ||
if (!line) { | ||
// Ignore empty lines. | ||
continue; | ||
} | ||
var parts = line.split("/", 2); | ||
@@ -804,10 +801,21 @@ | ||
function edits1(words) { | ||
var rv = []; | ||
/** | ||
* Returns a hash keyed by all of the strings that can be made by making a single edit to the word (or words in) `words` | ||
* The value of each entry is the number of unique ways that the resulting word can be made. | ||
* | ||
* @arg mixed words Either a hash keyed by words or a string word to operate on. | ||
* @arg bool known_only Whether this function should ignore strings that are not in the dictionary. | ||
*/ | ||
function edits1(words, known_only) { | ||
var rv = {}; | ||
var ii, i, j, _iilen, _len, _jlen; | ||
var i, j, _iilen, _len, _jlen, _edit; | ||
for (ii = 0, _iilen = words.length; ii < _iilen; ii++) { | ||
var word = words[ii]; | ||
if (typeof words == 'string') { | ||
var word = words; | ||
words = {}; | ||
words[word] = true; | ||
} | ||
for (var word in words) { | ||
for (i = 0, _len = word.length + 1; i < _len; i++) { | ||
@@ -817,3 +825,12 @@ var s = [ word.substring(0, i), word.substring(i) ]; | ||
if (s[1]) { | ||
rv.push(s[0] + s[1].substring(1)); | ||
_edit = s[0] + s[1].substring(1); | ||
if (!known_only || self.check(_edit)) { | ||
if (!(_edit in rv)) { | ||
rv[_edit] = 1; | ||
} | ||
else { | ||
rv[_edit] += 1; | ||
} | ||
} | ||
} | ||
@@ -823,3 +840,12 @@ | ||
if (s[1].length > 1 && s[1][1] !== s[1][0]) { | ||
rv.push(s[0] + s[1][1] + s[1][0] + s[1].substring(2)); | ||
_edit = s[0] + s[1][1] + s[1][0] + s[1].substring(2); | ||
if (!known_only || self.check(_edit)) { | ||
if (!(_edit in rv)) { | ||
rv[_edit] = 1; | ||
} | ||
else { | ||
rv[_edit] += 1; | ||
} | ||
} | ||
} | ||
@@ -831,3 +857,12 @@ | ||
if (self.alphabet[j] != s[1].substring(0,1)){ | ||
rv.push(s[0] + self.alphabet[j] + s[1].substring(1)); | ||
_edit = s[0] + self.alphabet[j] + s[1].substring(1); | ||
if (!known_only || self.check(_edit)) { | ||
if (!(_edit in rv)) { | ||
rv[_edit] = 1; | ||
} | ||
else { | ||
rv[_edit] += 1; | ||
} | ||
} | ||
} | ||
@@ -839,3 +874,12 @@ } | ||
for (j = 0, _jlen = self.alphabet.length; j < _jlen; j++) { | ||
rv.push(s[0] + self.alphabet[j] + s[1]); | ||
_edit = s[0] + self.alphabet[j] + s[1]; | ||
if (!known_only || self.check(_edit)) { | ||
if (!(_edit in rv)) { | ||
rv[_edit] = 1; | ||
} | ||
else { | ||
rv[_edit] += 1; | ||
} | ||
} | ||
} | ||
@@ -848,36 +892,26 @@ } | ||
} | ||
function known(words) { | ||
var rv = []; | ||
for (var i = 0, _len = words.length; i < _len; i++) { | ||
if (self.check(words[i])) { | ||
rv.push(words[i]); | ||
} | ||
} | ||
return rv; | ||
} | ||
function correct(word) { | ||
// Get the edit-distance-1 and edit-distance-2 forms of this word. | ||
var ed1 = edits1([word]); | ||
var ed2 = edits1(ed1); | ||
var ed1 = edits1(word); | ||
var ed2 = edits1(ed1, true); | ||
var corrections = known(ed1.concat(ed2)); | ||
var i, _len; | ||
// Sort the edits based on how many different ways they were created. | ||
var weighted_corrections = {}; | ||
var weighted_corrections = ed2; | ||
for (i = 0, _len = corrections.length; i < _len; i++) { | ||
if (!(corrections[i] in weighted_corrections)) { | ||
weighted_corrections[corrections[i]] = 1; | ||
for (var ed1word in ed1) { | ||
if (!self.check(ed1word)) { | ||
continue; | ||
} | ||
if (ed1word in weighted_corrections) { | ||
weighted_corrections[ed1word] += ed1[ed1word]; | ||
} | ||
else { | ||
weighted_corrections[corrections[i]] += 1; | ||
weighted_corrections[ed1word] = ed1[ed1word]; | ||
} | ||
} | ||
var i, _len; | ||
var sorted_corrections = []; | ||
@@ -890,13 +924,17 @@ | ||
} | ||
function sorter(a, b) { | ||
if (a[1] < b[1]) { | ||
var a_val = a[1]; | ||
var b_val = b[1]; | ||
if (a_val < b_val) { | ||
return -1; | ||
} else if (a_val > b_val) { | ||
return 1; | ||
} | ||
return 1; | ||
// @todo If a and b are equally weighted, add our own weight based on something like the key locations on this language's default keyboard. | ||
return b[0].localeCompare(a[0]); | ||
} | ||
sorted_corrections.sort(sorter).reverse(); | ||
var rv = []; | ||
@@ -913,3 +951,5 @@ | ||
for (i = 0, _len = Math.min(limit, sorted_corrections.length); i < _len; i++) { | ||
var working_limit = limit; | ||
for (i = 0; i < Math.min(working_limit, sorted_corrections.length); i++) { | ||
if ("uppercase" === capitalization_scheme) { | ||
@@ -922,7 +962,11 @@ sorted_corrections[i][0] = sorted_corrections[i][0].toUpperCase(); | ||
if (!self.hasFlag(sorted_corrections[i][0], "NOSUGGEST")) { | ||
if (!self.hasFlag(sorted_corrections[i][0], "NOSUGGEST") && rv.indexOf(sorted_corrections[i][0]) == -1) { | ||
rv.push(sorted_corrections[i][0]); | ||
} | ||
else { | ||
// If one of the corrections is not eligible as a suggestion , make sure we still return the right number of suggestions. | ||
working_limit++; | ||
} | ||
} | ||
return rv; | ||
@@ -944,2 +988,2 @@ } | ||
module.exports = Typo; | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
727061
778