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.

Setup

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.

Usage

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.

Methods

correct   correct  

Constants

SP = Aspell.new(Ultrasphinx::DICTIONARY)
SP = nil
SP = Aspell.new(Ultrasphinx::DICTIONARY)
SP = nil

Public Class methods

[Source]

    # File lib/ultrasphinx/spell.rb, line 42
42:     def self.correct string
43:       return nil unless SP
44:       correction = string.gsub(/[\w\']+/) do |word| 
45:         unless SP.check(word)
46:           SP.suggest(word).first
47:         else
48:           word
49:         end
50:       end
51:       
52:       correction if correction != string
53:     end

[Source]

    # File lib/ultrasphinx/spell.rb, line 42
42:     def self.correct string
43:       return nil unless SP
44:       correction = string.gsub(/[\w\']+/) do |word| 
45:         unless SP.check(word)
46:           SP.suggest(word).first
47:         else
48:           word
49:         end
50:       end
51:       
52:       correction if correction != string
53:     end

[Validate]