Class: Phronomy::Eval::Scorer::ExactMatch

Inherits:
Base
  • Object
show all
Defined in:
lib/phronomy/eval/scorer/exact_match.rb

Overview

Scorer that returns 1.0 when the actual output exactly matches the expected output (after stripping leading/trailing whitespace). Comparison is case-sensitive by default.

Examples:

ExactMatch.new.score(actual: "Paris", expected: "Paris")  # => 1.0
ExactMatch.new.score(actual: "paris", expected: "Paris")  # => 0.0

Instance Method Summary collapse

Constructor Details

#initialize(case_sensitive: true) ⇒ ExactMatch

Returns a new instance of ExactMatch.

Parameters:

  • case_sensitive (Boolean) (defaults to: true)

    default true



15
16
17
# File 'lib/phronomy/eval/scorer/exact_match.rb', line 15

def initialize(case_sensitive: true)
  @case_sensitive = case_sensitive
end

Instance Method Details

#score(actual:, expected:, input: nil) ⇒ Float

Returns 1.0 on match, 0.0 otherwise.

Returns:

  • (Float)

    1.0 on match, 0.0 otherwise



20
21
22
23
24
25
# File 'lib/phronomy/eval/scorer/exact_match.rb', line 20

def score(actual:, expected:, input: nil)
  a = actual.to_s.strip
  e = expected.to_s.strip
  a = a.downcase and e = e.downcase unless @case_sensitive
  (a == e) ? 1.0 : 0.0
end