Class: See5::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/see5/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_classification:, rules:) ⇒ Model

Returns a new instance of Model.



9
10
11
12
# File 'lib/see5/model.rb', line 9

def initialize(default_classification:, rules:)
  @default_classification = default_classification
  @rules = rules
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



7
8
9
# File 'lib/see5/model.rb', line 7

def rules
  @rules
end

Class Method Details

.from_json(json) ⇒ Object



33
34
35
36
37
# File 'lib/see5/model.rb', line 33

def self.from_json(json)
  json_hash = JSON.parse(json, symbolize_names: true)
  new(default_classification: json_hash[:default_classification],
      rules: json_hash[:rules]&.map { |rule_hash| Rule.from_h(rule_hash) })
end

Instance Method Details

#classify(data) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/see5/model.rb', line 14

def classify(data)
  # See5 orders rules by confidence within each class (TODO verify),
  # so the first matching rule is the one with the highest confidence.
  first_matching_rule = rules.find { |rule| rule.match?(data) }

  return first_matching_rule.classification unless first_matching_rule.nil?

  @default_classification
end

#to_hObject



24
25
26
27
# File 'lib/see5/model.rb', line 24

def to_h
  { default_classification: @default_classification,
    rules: rules.map(&:to_h) }
end

#to_jsonObject



29
30
31
# File 'lib/see5/model.rb', line 29

def to_json
  to_h.to_json
end