Sha256: 4945d5bc5e3bb14e63021abea2dcc9a9e3289f6c72289243c3fb7e6f29dfca21

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents


module Ultrasphinx

=begin rdoc

In order to spellcheck your user's query, Ultrasphinx bundles a small spelling module. 

== Setup

Make sure Aspell and the Rubygem <tt>raspell</tt> are installed. See http://blog.evanweaver.com/files/doc/fauna/raspell/ for detailed instructions.
  
Copy the <tt>examples/app.multi</tt> file into your Aspell dictionary folder (<tt>/opt/local/share/aspell/</tt> on Mac, <tt>/usr/lib/aspell-0.60/</tt> on Linux). This file lets Aspell load a custom wordlist generated by Sphinx from your app data. 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 <tt>sudo</tt> because Ultrasphinx needs to write to the Aspell dictionary folder. Also note that Aspell, <tt>raspell</tt>, 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 <tt>@correction</tt> is not <tt>nil</tt>, go ahead and suggest it to the user. 

=end

  module Spell  
    SP = Aspell.new("app")   
    SP.suggestion_mode = Aspell::NORMAL
    SP.set_option("ignore-case", "true")
    
    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    
    
  end    
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ultrasphinx-1.5 lib/ultrasphinx/spell.rb
ultrasphinx-1.5.2 lib/ultrasphinx/spell.rb
ultrasphinx-1.5.1 lib/ultrasphinx/spell.rb