Class: IndexedSearch::Match::Stem

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

Overview

Performs a word stemming match, using the Porter word stemming algorithm, see: tartarus.org/martin/PorterStemmer/ Note the Porter algorithm is designed for English. Requires the text gem

Uses a stem column to store a stem 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?, #term_matches, #term_non_matches

Constructor Details

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

Class Method Details

.implementation=(what) ⇒ Object

TODO: remove this!



24
25
26
# File 'lib/indexed_search/match/stem.rb', line 24

def self.implementation=(what)
  ActiveSupport::Deprecation.warn "IndexedSearch::Match::Stem.implementation no longer does anything and will be removed from future releases.", caller
end

.make_index_value(term) ⇒ Object

stem routine, enforces set length too



53
54
55
56
# File 'lib/indexed_search/match/stem.rb', line 53

def self.make_index_value(term)
  # TODO figure out how to normalize these to ascii... (they've only been normalized by case)
  Text::PorterStemming.stem(term)[0..max_length]
end

Instance Method Details

#results(do_all = false) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/indexed_search/match/stem.rb', line 58

def results(do_all=false)
  [].tap do |res|
    if do_all || term_maps[1].present?
      res << IndexedSearch::Match::Result.new(self, term_maps[1], rank_multiplier[0], term_multiplier[0], limit_reduction_factor, type_reduction_factor)
    end
    if do_all || term_maps[2].present?
      res << IndexedSearch::Match::Result.new(self, term_maps[2], rank_multiplier[1], term_multiplier[1], limit_reduction_factor, type_reduction_factor)
    end
  end
end

#scopeObject



28
29
30
# File 'lib/indexed_search/match/stem.rb', line 28

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

#term_mapObject



33
34
35
# File 'lib/indexed_search/match/stem.rb', line 33

def term_map
  term_maps[0]
end

#term_mapsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/indexed_search/match/stem.rb', line 36

def term_maps
  @term_maps ||= [].tap do |maps|
    # 3 maps: both, words with stem equal to this term's stem, words with stem equal to this term
    # TODO: these still don't actually work right.. grr.. (the select in result.find weeds some unicode matches out
    # (which should be fixed by better normalization) and also weeds out the 3rd map term matches oops!)
    3.times { maps << Hash.new { |hash,key| hash[key] = [] } }
    term_matches.each do |term|
      stem = self.class.make_index_value(term)
      maps[0][stem] << term
      maps[0][term] << term if term != stem
      maps[1][stem] << term
      maps[2][term] << term
    end
  end
end