forked from dscape/spell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell.js
366 lines (350 loc) · 9.97 KB
/
spell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* javascript spell checker based on
* http://norvig.com/spell-correct.html
*
* copyright 2011 nuno job <nunojob.com> (oO)--',--
* pedro teixeira <metaduck.com>
*
* licensed under the apache license, version 2.0 (the "license");
* you may not use this file except in compliance with the license.
* you may obtain a copy of the license at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* unless required by applicable law or agreed to in writing, software
* distributed under the license is distributed on an "as is" basis,
* without warranties or conditions of any kind, either express or implied.
* see the license for the specific language governing permissions and
* limitations under the license.
*/
(function () {
var root = this
, previous_spell = root.spell
;
/*
* dictionary
*
* creates a dictionary given a place to store
*
* @param {dict_store:object:required}
* object that implements two functions
* `get` to retrieve a stored dictionary from disk/memory
* `store` to store a dictionary from disk/memory
*
* @return {object} a dictionary module
*/
var spell = function dictionary(dict_store) {
var dict =
dict_store && typeof dict_store.get === 'function' ? dict_store.get() : {}
, noop = function(){}
, alphabet = "abcdefghijklmnopqrstuvwxyzáéíóúñäëïöüàèìòùâêîôûç".split("")
;
function spell_store(cb) {
if (dict_store && typeof dict_store.store === 'function') {
dict_store.store(dict, cb);
}
}
function spell_train(corpus,regex) {
var match, word;
regex = regex || /[a-záéíóúñäëïöüàèìòùâêîôûç]+/g;
corpus = corpus.toLowerCase();
while ((match = regex.exec(corpus))) {
word = match[0];
spell_add_word(word, 1);
}
}
function spell_edits(word, alphabetOverride) {
var edits = []
, thisAlphabet = alphabetOverride ? alphabetOverride : alphabet
, i
, j
;
for (i=0; i < word.length; i++) { // deletion
edits.push(word.slice(0, i) + word.slice(i+1));
}
for (i=0; i < word.length-1; i++) { // transposition
edits.push( word.slice(0, i) + word.slice(i+1, i+2) +
word.slice(i, i+1) + word.slice(i+2));
}
for (i=0; i < word.length; i++) { // alteration
for(j in thisAlphabet) {
edits.push(word.slice(0, i) + thisAlphabet[j] + word.slice(i+1));
}
}
for (i=0; i <= word.length; i++) { // insertion
for(j in thisAlphabet) {
edits.push(word.slice(0, i) + thisAlphabet[j] + word.slice(i));
}
}
return edits;
}
function is_empty(obj) {
for (var key in obj) { if (obj.hasOwnProperty(key)) return false; }
return true;
}
function spell_order(candidates, min, max) {
var ordered_candidates = []
, current
, i
, w
;
for(i=max; i>=min; i--) {
if(candidates.hasOwnProperty(i)) {
current = candidates[i];
for (w in current) {
if(current.hasOwnProperty(w)) {
ordered_candidates.push({"word": w, "score": i});
}
}
}
}
return ordered_candidates;
}
/*
* reset
*
* resets the dictionary.
*
* e.g.
* spell.reset();
*
* @return void
*/
function spell_reset() { return spell_load({reset: true}); }
/*
* load
*
* loads a free form corpus dictionary.
*
* e.g.
* spell.load({'dog': 1, 'cat': 2});
* spell.load('dog cat cat');
*
* @param {opts.corpus:string|object:optional}
* corpus string to initialize to
* @param {opts.reset:boolean:optional}
* whether you want to reset the existing dictionary or just append
* to what already exists
* @param {opts.store:boolean:optional}
* decide if you want to use storage
* @param {opts.after_store:function:optional}
* function to call back when store is done
*
* @return void
*/
function spell_load(corpus, opts) {
if ('object' === typeof corpus) { opts = corpus; }
if ('string' === typeof corpus) {
if('object' === typeof opts) {
opts.corpus = corpus;
} else {
opts = {corpus: corpus };
}
}
if ('string' === typeof opts) { opts = {corpus: opts }; }
opts = 'object' === typeof opts ? opts : {};
opts.reset = (opts.reset !== false);
opts.store = (opts.store !== false);
opts.after_store = opts.after_store || noop;
opts.corpus = opts.corpus || '';
if(opts.reset) { dict = {}; }
if('object' === typeof opts.corpus) {
for(var key in opts.corpus) {
spell_add_word(key, {score: opts.corpus[key]});
}
} else { spell_train(opts.corpus); }
if(opts.store) { spell_store(opts.after_store); }
}
/*
* add word
*
* loads a word into the dictionary
*
* e.g.
* spell.insert_word('dog', 5);
*
* @param {word:string:required}
* the word you want to add
* @param {opts.count:int:optional}
* the number of times the word appears in a text, defaults to one
* @param {opts.store:boolean:optional}
* decide if you want to use storage
* @param {opts.done:function:optional}
* function to call back when store is done
*
* @return void
*/
function spell_add_word(word, opts) {
if ('string' === typeof opts || 'number' === typeof opts) {
opts = { score: parseInt(opts, 10) };
}
opts = 'object' === typeof opts ? opts : {};
opts.score = opts.score || 1;
opts.store = opts.store || true;
opts.done = opts.done || noop;
word = word.toLowerCase();
dict[word] =
dict.hasOwnProperty(word) ? dict[word] + opts.score : opts.score;
if(opts.store) { spell_store(opts.done); }
}
/*
* remove word
*
* removes word from the dictionary
*
* e.g.
* spell.remove_word('dog');
*
* @param {word:string:required}
* the word you want to add
* @param {opts.store:boolean:optional}
* decide if you want to use storage
* @param {opts.done:function:optional}
* function to call back when store is done
*
* @return void
*/
function spell_remove_word(word,opts) {
opts = 'object' === typeof opts ? opts : {};
opts.store = (opts.store !== false);
opts.done = opts.done || noop;
if (dict.hasOwnProperty(word)) { delete dict[word]; }
if(opts.store) { spell_store(opts.done); }
}
/*
* suggest
*
* returns spelling sugestions for a given word
*
* e.g.
* spell.suggest('speling');
*
* @param {word:string:required}
* the word you want to spell check
* @param {alphabet:array:optional}
* if you need to override checking for just words you can set this
* and it will enable you to make suggestions that include punctiation
* etc
*
* @return {array} ordered array containing json objects such as
* [{"word": "spelling", "score": 10}]
*/
function spell_suggest(word, alphabet) {
if (dict.hasOwnProperty(word)) {
return [{"word":word, "score": dict[word]}];
}
var edits1 = spell_edits(word, alphabet)
, candidates = {}
, min
, max
, current_count
;
function get_candidates(word) {
if(dict.hasOwnProperty(word)) {
current_count = dict[word];
if (candidates.hasOwnProperty(current_count)) {
candidates[current_count][word] = true;
} else {
candidates[current_count] = {};
candidates[current_count][word] = true;
}
max = max ? (max < current_count ? current_count : max) : current_count;
min = min ? (min > current_count ? current_count : min) : current_count;
}
}
edits1.forEach(get_candidates);
if(!is_empty(candidates)) { return spell_order(candidates,min,max); }
edits1.forEach(function(edit1){
spell_edits(edit1, alphabet).forEach(get_candidates);
});
if(!is_empty(candidates)) { return spell_order(candidates,min,max); }
return []; // no suggestions
}
/*
* feeling lucky
*
* returns the first spelling correction for a word
*
* e.g.
* spell.lucky('speling');
*
* @param {word:string:required}
* the word you want to spell check
* @param {alphabet:array:optional}
* if you need to override checking for just words you can set this
* and it will enable you to make suggestions that include punctiation
* etc
*
* @return {string} the most likely match
*/
function spell_lucky(word, alphabet) {
var suggest = spell_suggest(word, alphabet)[0];
if(suggest && suggest.hasOwnProperty("word")) {
return suggest.word;
}
return;
}
/*
* export
*
* exports the dictionary
*
* e.g.
* spell.export();
*
* @return {json} dictionary
*/
function spell_export(word) {
return {corpus: dict};
}
/*
* check
*
* returns true or false for a given word if exists in dictionary
*
* e.g.
* spell.check('speling');
*
* @param {word:string:required}
* the word you want to spell check
*
* @return {boolean}
* false
*/
function spell_check(word) {
return dict.hasOwnProperty(word);
}
return { reset : spell_reset
, load : spell_load
, "export" : spell_export
, save : spell_export // alias
, add_word : spell_add_word
, addWord : spell_add_word // alias
, remove_word : spell_remove_word
, removeWord : spell_remove_word // alias
, suggest : spell_suggest
, check : spell_check
, lucky : spell_lucky
};
};
spell._previous = previous_spell;
if (typeof exports !== 'undefined') { // nodejs
spell.platform = { name: "node.js", version: process.version };
spell.version = JSON.parse(
require('fs').readFileSync(__dirname + "/package.json")).version;
spell.path = __dirname;
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = spell;
}
exports.spell = spell;
} else { // browser
// browser detection is possible in the future
spell.platform = { name: "browser" };
spell.version = "0.0.3";
if (typeof define === 'function' && define.amd) {
define('spell', function() { return spell; });
}
else {
root.spell = spell;
}
}
})();