Class: AnswerFactory::Machines::SelectBySummedRank

Inherits:
Machine
  • Object
show all
Defined in:
lib/machines/select_by_summed_rank.rb

Instance Attribute Summary

Attributes inherited from Machine

#options

Instance Method Summary collapse

Methods inherited from Machine

#initialize

Constructor Details

This class inherits a constructor from AnswerFactory::Machines::Machine

Instance Method Details

#all_goals(batch) ⇒ Object



42
43
44
# File 'lib/machines/select_by_summed_rank.rb', line 42

def all_goals(batch)
  (batch.collect {|a| a.scores.keys}).flatten.to_set.to_a
end

#screen(batch, overriden_options = {}) ⇒ Object Also known as: generate

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/machines/select_by_summed_rank.rb', line 9

def screen(batch, overriden_options={})
  raise ArgumentError, "SelectBySummedRank#screen cannot process class #{batch.class}" unless
    batch.kind_of?(Batch)
  all_options = @options.merge(overriden_options)
  
  criteria = all_options[:comparison_criteria] || shared_goals(batch)
  return batch if criteria.empty?
  
  scored = Hash.new(0)
  incomparable = Set.new
  
  criteria.each do |criterion|
    scorable, unscorable = batch.partition {|a| a.scores.include?(criterion)}
    incomparable += unscorable
    ranked = scorable.sort_by {|a| a.scores[criterion]}
    ranked.each_with_index do |a, index|
      scored[a] += index
    end
  end
  
  incomparable.each {|a| scored.delete(a)}
  
  lowest_sum = scored.values.min
  winners = batch.find_all {|a| scored[a] == lowest_sum }
  
  result = Batch.new
  incomparable.each {|a| result << a}
  
  winners.each {|a| result << a unless result.include?(a)}
  return result
end

#shared_goals(batch) ⇒ Object



47
48
49
# File 'lib/machines/select_by_summed_rank.rb', line 47

def shared_goals(batch)
  batch.inject(all_goals(batch)) {|intersection, answer| intersection &= answer.scores.keys}
end