Module | Ultrasphinx::Spell |
In: |
lib/ultrasphinx/spell.rb
lib/ultrasphinx/spell.rb |
In order to spellcheck your user‘s query, Ultrasphinx bundles a small spelling module.
Make sure Aspell and the Rubygem raspell are installed. See blog.evanweaver.com/files/doc/fauna/raspell/ for detailed instructions.
Copy the examples/ap.multi file into your Aspell dictionary folder (/opt/local/share/aspell/ on Mac, /usr/lib/aspell-0.60/ on Linux). This file lets Aspell load a custom wordlist generated by Sphinx from your app data (you can configure its filename in the config/ultrasphinx/*.base files). Modify the file if you don‘t want to also use the default American English dictionary.
Finally, to build the custom wordlist, run:
sudo rake ultrasphinx:spelling:build
You need to use sudo because Ultrasphinx needs to write to the Aspell dictionary folder. Also note that Aspell, raspell, and the custom dictionary must be available on each application server, not on the Sphinx daemon server.
Now you can see if a query is correctly spelled as so:
@correction = Ultrasphinx::Spell.correct(@search.query)
If @correction is not nil, go ahead and suggest it to the user.
SP | = | Aspell.new(Ultrasphinx::DICTIONARY) |
SP | = | nil |
SP | = | Aspell.new(Ultrasphinx::DICTIONARY) |
SP | = | nil |
# File lib/ultrasphinx/spell.rb, line 41 41: def self.correct string 42: return nil unless SP 43: return nil if string =~ /\d+/ 44: 45: correction = string.gsub(/[\w\']+/) do |word| 46: unless SP.check(word) 47: SP.suggest(word).first 48: else 49: word 50: end 51: end 52: 53: correction if correction != string 54: end
# File lib/ultrasphinx/spell.rb, line 41 41: def self.correct string 42: return nil unless SP 43: return nil if string =~ /\d+/ 44: 45: correction = string.gsub(/[\w\']+/) do |word| 46: unless SP.check(word) 47: SP.suggest(word).first 48: else 49: word 50: end 51: end 52: 53: correction if correction != string 54: end