Class: NaiveBayes

Inherits:
Object
  • Object
show all
Defined in:
lib/spellr/key_tuner/naive_bayes.rb

Overview

Constant Summary collapse

YAML_PATH =

:nocov:

File.join(__dir__, 'data.yml')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = YAML_PATH) ⇒ NaiveBayes

Returns a new instance of NaiveBayes.



18
19
20
21
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 18

def initialize(path = YAML_PATH)
  load_from_yaml(path)
  @key = {}
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



16
17
18
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 16

def classes
  @classes
end

#feature_setObject (readonly)

Returns the value of attribute feature_set.



16
17
18
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 16

def feature_set
  @feature_set
end

#featuresObject (readonly)

Returns the value of attribute features.



16
17
18
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 16

def features
  @features
end

#num_classesObject (readonly)

Returns the value of attribute num_classes.



16
17
18
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 16

def num_classes
  @num_classes
end

Instance Method Details

#class_probability(features, class_name) ⇒ Object

this is where we compute the final naive Bayesian probability for a given set of features being a part of a given class.



61
62
63
64
65
66
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 61

def class_probability(features, class_name)
  class_fraction = 1.0 / num_classes
  feature_bayes = feature_multiplication(features, class_name)
  feature_bayes *= heuristic_weight if class_name.start_with?('key_')
  feature_bayes * class_fraction
end

#classify(features) ⇒ Object



68
69
70
71
72
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 68

def classify(features)
  classes.max_by do |class_name|
    class_probability(features, class_name)
  end
end

#feature_multiplication(features, class_name) ⇒ Object

multiply together the feature probabilities for all of the features in a class for given values



49
50
51
52
53
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 49

def feature_multiplication(features, class_name)
  features.reduce(1.0) do |result, (key, value)|
    result * feature_probability(key, value, class_name)
  end
end

#feature_probability(feature, value, class_name) ⇒ Object

given a class, this method determines the probability of a certain value occurring for a given feature feature: name of the feature in consideration in the training data value: the value of the feature for which we are finding the probability class_name: name of the class in consideration



43
44
45
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 43

def feature_probability(feature, value, class_name)
  Stats.gaussian_probability(value, **feature_set[class_name][feature])
end

#heuristic_weightObject



55
56
57
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 55

def heuristic_weight
  @heuristic_weight ||= 10**Spellr.config.key_heuristic_weight
end

#key?(string) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 23

def key?(string)
  @key.fetch(string) do
    @key[string] = classify(PossibleKey.new(string).features).start_with?('key')
  end
end

#load_from_yaml(path = YAML_PATH) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 29

def load_from_yaml(path = YAML_PATH)
  data = YAML.safe_load(::File.read(path), permitted_classes: [Symbol])

  @feature_set = data[:feature_set]
  @num_classes = data[:num_classes]
  @classes = data[:classes]
  @features = data[:features]
end