Class: InciScore::Recognizer

Inherits:
Object
  • Object
show all
Defined in:
lib/inci_score/recognizer.rb,
lib/inci_score/recognizer_rules.rb

Defined Under Namespace

Modules: Rules

Constant Summary collapse

DEFAULT_RULES =
[Rules::Key, Rules::Levenshtein, Rules::Hazard, Rules::Prefix, Rules::Tokens].freeze
PRECISION_BASE =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ingredient, rules = DEFAULT_RULES) ⇒ Recognizer

Returns a new instance of Recognizer.



11
12
13
14
15
16
# File 'lib/inci_score/recognizer.rb', line 11

def initialize(ingredient, rules = DEFAULT_RULES)
  @ingredient = Ingredient.new(ingredient)
  @rules = rules
  @applied = []
  @found = false
end

Instance Attribute Details

#appliedObject (readonly)

Returns the value of attribute applied.



8
9
10
# File 'lib/inci_score/recognizer.rb', line 8

def applied
  @applied
end

#foundObject

Returns the value of attribute found.



9
10
11
# File 'lib/inci_score/recognizer.rb', line 9

def found
  @found
end

#ingredientObject (readonly)

Returns the value of attribute ingredient.



8
9
10
# File 'lib/inci_score/recognizer.rb', line 8

def ingredient
  @ingredient
end

#rulesObject (readonly)

Returns the value of attribute rules.



8
9
10
# File 'lib/inci_score/recognizer.rb', line 8

def rules
  @rules
end

Instance Method Details

#callObject



18
19
20
21
22
23
# File 'lib/inci_score/recognizer.rb', line 18

def call
  return if ingredient.to_s.empty?
  find_component.tap do |c|
    self.found = true if c
  end
end

#precisionObject



25
26
27
28
29
30
31
# File 'lib/inci_score/recognizer.rb', line 25

def precision
  return 0.0 unless found
  rule = applied.last
  index = rules.index(rule) + PRECISION_BASE
  ratio = Math.log(index, PRECISION_BASE)
  (100 / ratio).round(2)
end