I came across Peter Norvig’s article How to Write a Spelling Corrector the other day and felt that packaging it as a ruby gem would be a nice way to learn how to create gems. The gem gives you a SpellingBee class that can suggest corrections for mis-spelled words based on the frequency of words in a text file specified by you.
Usage
Install the gem first.
gem install spellingbee
Now you can use the SpellingBee class like this:
require 'spellingbee' s = SpellingBee.new s.correct "speling" #=> ["spelling"] s.correct "asdfghjk" #=> ["asdfghjk"] // no similar words so same word is returned
Here, the correction is suggested because the word “spelling” occurs in the default example file. If you tried correcting a word that wasn’t in the file, the same word will be returned. To use your own dictionary file:
s = SpellingBee.new :source_text => 'my_file.txt' s.correct "speling" #=> ["spelling"]
Here, assuming that my_file.txt contains the word spelling, SpellingBee will return the correction for “speling”.
There are a few methods in the SpellinBee class that you can use to list all words that have edit distance 1, such as words that have one character missing, or one character inserted or two consecutive characters swapped. Take a look at the source code to see how to use them. (There are comments explaining each method.)
Credits – Brian Adkins’ article How to Write a Spelling Corrector in Ruby helped a lot when I was trying to translate the python code.
I created this gem just so that I could learn how to create gems, and it doesn’t have any tests (yet). I’ll hopefully be able to improve it over time. If you have any ideas to improve the gem, please fork spellingbee on github, or leave suggestions in the comments here. Please do point out mistakes.