Module: Ultrasphinx::Spell

Defined in:
lib/ultrasphinx/spell.rb

Overview

Spelling support

In order to spellcheck your user’s query, Ultrasphinx bundles a small spelling module. First, make sure Aspell 0.6, an appropriate Aspell dictionary, and the Rubygem ‘raspell’ are all installed.

Then, copy examples/app.multi into your Aspell dictionary folder. It allows you to use Sphinx to generate a custom wordlist for your app. Modify it if you don’t want to also use the default American English dictionary.

Then, to build the custom wordlist, run:

rake ultrasphinx:spelling:build

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. Otherwise, the query was already correct.

Constant Summary collapse

SP =
Aspell.new("app")

Class Method Summary collapse

Class Method Details

.correct(string) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ultrasphinx/spell.rb', line 27

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