Class: Deviner::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/deviner/match.rb

Instance Method Summary collapse

Constructor Details

#initialize(weight = nil, options = {}) ⇒ Match

Returns a new instance of Match.



8
9
10
11
# File 'lib/deviner/match.rb', line 8

def initialize(weight=nil, options={})
  @weights=weight
  self.options.merge(options)
end

Instance Method Details

#cacheObject



74
75
76
# File 'lib/deviner/match.rb', line 74

def cache
  @cache||={}
end

#clear_cacheObject



78
79
80
# File 'lib/deviner/match.rb', line 78

def clear_cache
  @cache={}
end

#constrains(key) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/deviner/match.rb', line 14

def constrains key
  if @weights.present?
    return {weight: Picky::Weights::Constant.new(@weights[key].to_i)}
  else
    return {weight: Weights::Constant.new()}
  end
end

#index(model, keys) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/deviner/match.rb', line 29

def index(model, keys)
  myoptions = options
  myconstrains = proc {|e| constrains(e) }
  index = Picky::Index.new :model do
    indexing stems_with: Lingua::Stemmer.new(language: myoptions[:language]),
             substitutes_characters_with: Picky::CharacterSubstituters::WestEuropean.new

    for key in keys
      category key, myconstrains.call(key)
    end
  end
  index.add model
  index
end

#match(categories) ⇒ Object

Return nil if no matches found



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/deviner/match.rb', line 52

def match(categories)
  result = []
  for category in categories
      score = score(category)
      result << {value: score,  category: category}
  end

  highest_value = result.map{|x| x[:value]}.sort.last
  selected_category = nil
  if highest_value > 0
    result.each do |hash|
      if hash[:value] == highest_value
        selected_category = hash[:category]
      end
    end
  end

  return selected_category

end

#model(model, keys) ⇒ Object



44
45
46
47
48
# File 'lib/deviner/match.rb', line 44

def model(model, keys)
  @index = index(model, keys)
  @keys = keys
  clear_cache
end

#optionsObject



22
23
24
25
26
27
# File 'lib/deviner/match.rb', line 22

def options
  {language: 'de',
   query_method: :name,
   parent_method: :category,
   has_parent: true}
end

#prepare_query(query) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/deviner/match.rb', line 102

def prepare_query query
  query.gsub(",", " ").
        gsub("&", " ").
        gsub("ü", "ue").
        gsub("ö", "oe").
        gsub("ä", "ae").
        gsub("Ü", "Ue").
        gsub("Ö", "Oe").
        gsub("Ä", "Ae").
        gsub("  ", " ")
end

#score(category) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/deviner/match.rb', line 82

def score(category)
  return 0 if category == nil
  query = category.send(options[:query_method])
  query = prepare_query(query)
  return  cache[query] if cache[query] != nil
  search = Picky::Search.new @index
  subqueries = query.split(" ")
  total = 0
  for subquery in subqueries
    result = search.search(query)
    total+=result.total
  end
  if options[:has_parent]
    parent = category.send(options[:parent_method])
  else
    parent = nil
  end
  cache[query] = total + score(parent)
end