Module: Ultrasphinx::Spell

Defined in:
lib/ultrasphinx/spell.rb

Overview

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.

Class Method Summary collapse

Class Method Details

.correct(string) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ultrasphinx/spell.rb', line 42

def self.correct string
  return nil unless SP
  correction = string.gsub(/[\w\']+/) do |word| 
    unless SP.check(word)
      SP.suggest(word).first
    else
      word
    end
  end
  
  correction if correction != string
end