Class: IndexedSearch::Match::Metaphone

Inherits:
Base
  • Object
show all
Defined in:
lib/indexed_search/match/metaphone.rb

Overview

Does a metaphone algorithm comparison to find words that sound similar. This only works well for English.

Uses a metaphone column to store a metaphone value with each entry in the IndexedSearch::Word model.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#find, find_attributes, #initialize, match_against_term?, #results, #term_matches, #term_non_matches

Constructor Details

This class inherits a constructor from IndexedSearch::Match::Base

Class Method Details

.make_index_value(term) ⇒ Object



39
40
41
# File 'lib/indexed_search/match/metaphone.rb', line 39

def self.make_index_value(term)
  Text::Metaphone.metaphone(term)[0..max_length]
end

Instance Method Details

#scopeObject



29
30
31
# File 'lib/indexed_search/match/metaphone.rb', line 29

def scope
  @scope.where(self.class.matcher_attribute => term_map.keys)
end

#term_mapObject



33
34
35
36
37
# File 'lib/indexed_search/match/metaphone.rb', line 33

def term_map
  @term_map ||= Hash.new { |hash,key| hash[key] = [] }.tap do |map|
    term_matches.each { |term| map[self.class.make_index_value(term)] << term }
  end
end