Class: NaiveBayes

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

Overview

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(path = YAML_PATH) ⇒ NaiveBayes

Returns a new instance of NaiveBayes.



12
13
14
15
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 12

def initialize(path = YAML_PATH)
  load_from_yaml(path) if File.exist?(path)
  @key = {}
end

Instance Method Details

#key?(string) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

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

#save_to_yaml(path = YAML_PATH) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/spellr/key_tuner/naive_bayes.rb', line 32

def save_to_yaml(path = YAML_PATH)
  write_yaml(path,
             feature_set: feature_set,
             num_classes: num_classes,
             classes: classes,
             features: features)
end