Permalink
Browse files

[spell] 0.3.0 added support for custom alphabets

* Added test case
* Added information to readme in the #advanced portion
* Implementation includes `spell.suggest` and `spell.lucky`
  • Loading branch information...
1 parent 4e570ad commit 1b83dee9c282480f8fcd3ddaf9f3599375ab2d4f @dscape committed Jan 19, 2012
Showing with 44 additions and 12 deletions.
  1. +13 −0 README.md
  2. +1 −1 package.json
  3. +20 −11 spell.js
  4. +10 −0 test/spell.js
View
13 README.md
@@ -103,6 +103,19 @@ finally when adding words you can optionally give it a score:
dict.add_word('beer', {score: 100});
```
+if you are working with words that include punctuation in your dictionary you might need to override the alphabet that is being used. e.g. you might want to add `.` & `@` if you have a dictionary of email address. you can do that by:
+
+``` js
+// instantiate a new dictionary
+var dict = spell();
+// load text into dictionary so we can train the dictionary to know
+// which words exists and which ones are more frequent than others
+dict.load("nuno@dino.saur pedro@space.ship");
+console.log(dict.suggest('nuno@dino.sau',
+ "abcdefghijklmnopqrstuvwxyz.@".split("")
+));
+```
+
## storage
by default `dict` is stored in process (memory) for each dictionary instance you create. however if you feel like storing the dictionary externally, or use a shared dictionary, you can:
View
2 package.json
@@ -2,7 +2,7 @@
, "description" : "javascript dictionary module for node.js, and the browser"
, "homepage" : "http://github.com/dscape/spell"
, "repository" : "git://github.com/dscape/spell"
-, "version" : "0.2.0"
+, "version" : "0.3.0"
, "author" : "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)"
, "contributors" :
[ "Pedro Teixeira <pedro.teixeira@gmail.com> (http://metaduck.com)" ]
View
31 spell.js
@@ -56,8 +56,9 @@ function spell_train(corpus,regex) {
}
}
-function spell_edits(word) {
- var edits = []
+function spell_edits(word, alphabetOverride) {
+ var edits = []
+ , thisAlphabet = alphabetOverride ? alphabetOverride : alphabet
, i
, j
;
@@ -69,13 +70,13 @@ function spell_edits(word) {
word.slice(i, i+1) + word.slice(i+2));
}
for (i=0; i < word.length; i++) { // alteration
- for(j in alphabet) {
- edits.push(word.slice(0, i) + alphabet[j] + word.slice(i+1));
+ 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 alphabet) {
- edits.push(word.slice(0, i) + alphabet[j] + word.slice(i));
+ for(j in thisAlphabet) {
+ edits.push(word.slice(0, i) + thisAlphabet[j] + word.slice(i));
}
}
return edits;
@@ -230,15 +231,19 @@ function spell_remove_word(word,opts) {
*
* @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) {
+function spell_suggest(word, alphabet) {
if (dict.hasOwnProperty(word)) {
return [{"word":word, "score": dict[word]}];
}
- var edits1 = spell_edits(word)
+ var edits1 = spell_edits(word, alphabet)
, candidates = {}
, min
, max
@@ -260,7 +265,7 @@ function spell_suggest(word) {
edits1.forEach(get_candidates);
if(!is_empty(candidates)) { return spell_order(candidates,min,max); }
edits1.forEach(function(edit1){
- spell_edits(edit1).forEach(get_candidates);
+ spell_edits(edit1, alphabet).forEach(get_candidates);
});
if(!is_empty(candidates)) { return spell_order(candidates,min,max); }
return []; // no suggestions
@@ -276,11 +281,15 @@ function spell_suggest(word) {
*
* @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) {
- var suggest = spell_suggest(word)[0];
+function spell_lucky(word, alphabet) {
+ var suggest = spell_suggest(word, alphabet)[0];
if(suggest && suggest.hasOwnProperty("word")) {
return suggest.word;
}
View
10 test/spell.js
@@ -220,6 +220,16 @@ describe('spell', function(){
;
assert(suggest[0].word === readme.lucky("the"));
});
+ it('[npm] should be able to suggest w/ customer alphabets', function () {
+ var npm = spell()
+ , suggest
+ ;
+ npm.load({corpus: {"uglify": 1, "uglify-js": 1}});
+ suggest = npm.suggest('uglifyjs',
+ "abcdefghijklmnopqrstuvwxyz-".split(""));
+ assert(suggest[0]. word === 'uglify-js');
+ assert(suggest[0].score === 1);
+ });
});
describe('#lucky()', function(){

0 comments on commit 1b83dee

Please sign in to comment.