A good search engine should be smart enough to detect spelling errors and provide suggestions. For example if you misspelled a word when using Google search, it will ask Do you mean:.... It will be cool to have such feature for our own search engine. So I started to explore possible solutions what works will rails and came upon this blog post by Evan Weaver. He has implemented a ruby gem raspell to interface with Aspell. That sounds like a good solution and I decided to check it out.
1. Installing Aspell
As mentioned in Evan’s post to install Aspell just type the following command from terminal:
sudo port install aspell aspell-dict-en
This will install both the aspell library and aspell English dictionary.
2. Install raspell
This is straight forward.
sudo gem install raspell --with-opt-dir=/opt/local
3. Setting up your own dictionary
For my case I needed aspell to check against my own custom dictionary. I create a text file containing the list of words with each word on a new line save it as wordlist. e.g.
John
Simon
Mae
Tim
Louis
Then run it with the following command.
aspell --lang=en create master ./en_SG-name.rws < wordlist
Note that for the language option I’m using ‘en’ which is the standard code for English. If you are generating a word list in other languages you will have to change lang option accordingly. The name of the output file is important because Aspell only pick up dictionary file in a certain naming format. The dictionary file should start with the language code then follow by a country code which is optional. For my case I have chosen SG because I do not want to override the default en dictionaries from Aspell. The ‘name’ after the ’-’ is actually the jargon. So if you are generating a word list of street names in US you might want to name you dictionary as en_US-street.rws. Once you have generated the dictionary you will have to place it in Aspell default dictionary directory.
sudo mv en_SG-name.rws /opt/local/share/aspell/
Because Aspell will only look for dictionaries with .multi file extension in the default directory. I’ll need to create a en_SG-name.multi file for my new dictionary
sudo vi /opt/local/share/aspell/en_SG-name.multi
and add the following line.
add en_SG-name.rws
You can add multiple dictionary to this file. If you needed the common English dictionary together with your custom one. Just add it accordingly.
4. Using your own dictionary in ruby.
Finally to use the dictionary I have just created.
require 'rubygems'
require 'raspell'
sp = Aspell.new('en_SG', 'name')
sp.suggest('Mea')
=> ["Mae"]
The first parameter is the language part of the file name and the second is the jargon. So if you have named your dictionary as en_US-street.multi then you will need to initialize it with
sp = Aspell.new('en_US', 'street')
