Class: GreenMidget::Examples

Inherits:
Countable show all
Defined in:
lib/green_midget/models/examples.rb

Constant Summary collapse

GENERAL_FEATURE_NAME =
'any'

Instance Attribute Summary

Attributes inherited from Countable

#key

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Countable

#[], #initialize, #log_ratio, #record_key

Constructor Details

This class inherits a constructor from GreenMidget::Countable

Class Method Details

.[](feature) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/green_midget/models/examples.rb', line 19

def self.[](feature)
  object = super(feature)

  if object.no_examples? && (feature == GENERAL_FEATURE_NAME)
    raise NoExamplesGiven
  elsif object.no_examples?
    super GENERAL_FEATURE_NAME
  else
    object
  end
end

.log_ratioObject



36
37
38
# File 'lib/green_midget/models/examples.rb', line 36

def self.log_ratio
  self[GENERAL_FEATURE_NAME].log_ratio
end

.objects(features, with_general = false) ⇒ Object



31
32
33
34
# File 'lib/green_midget/models/examples.rb', line 31

def self.objects(features, with_general = false)
  features += with_general ? [ GENERAL_FEATURE_NAME ] : []
  super(features)
end

.totalObject



40
41
42
# File 'lib/green_midget/models/examples.rb', line 40

def self.total
  @@total ||= self[GENERAL_FEATURE_NAME].total
end

Instance Method Details

#no_examples?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/green_midget/models/examples.rb', line 52

def no_examples?
  CATEGORIES.inject(1) { |memo, category| memo *= self[category] } == 0
end

#probability_for(category) ⇒ Object



44
45
46
# File 'lib/green_midget/models/examples.rb', line 44

def probability_for(category)
  self[category] / total
end

#totalObject



48
49
50
# File 'lib/green_midget/models/examples.rb', line 48

def total
  CATEGORIES.inject(0) { |memo, category| memo += self[category] }
end