Class: InciScore::Recognizer
- Inherits:
-
Object
- Object
- InciScore::Recognizer
- 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
-
#applied ⇒ Object
readonly
Returns the value of attribute applied.
-
#found ⇒ Object
Returns the value of attribute found.
-
#ingredient ⇒ Object
readonly
Returns the value of attribute ingredient.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(ingredient, rules = DEFAULT_RULES) ⇒ Recognizer
constructor
A new instance of Recognizer.
- #precision ⇒ Object
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
#applied ⇒ Object (readonly)
Returns the value of attribute applied.
8 9 10 |
# File 'lib/inci_score/recognizer.rb', line 8 def applied @applied end |
#found ⇒ Object
Returns the value of attribute found.
9 10 11 |
# File 'lib/inci_score/recognizer.rb', line 9 def found @found end |
#ingredient ⇒ Object (readonly)
Returns the value of attribute ingredient.
8 9 10 |
# File 'lib/inci_score/recognizer.rb', line 8 def ingredient @ingredient end |
#rules ⇒ Object (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
#call ⇒ Object
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 |
#precision ⇒ Object
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 |