Class: EvalRuby::RSpecMatchers::HavePrecisionAtK

Inherits:
Object
  • Object
show all
Defined in:
lib/eval_ruby/rspec.rb

Overview

RSpec matcher that checks precision@k for retrieval results.

Examples:

expect(retrieval_result).to have_precision_at_k(5).above(0.8)

Instance Method Summary collapse

Constructor Details

#initialize(k) ⇒ HavePrecisionAtK

Returns a new instance of HavePrecisionAtK.



52
53
54
55
# File 'lib/eval_ruby/rspec.rb', line 52

def initialize(k)
  @k = k
  @threshold = 0.5
end

Instance Method Details

#above(threshold) ⇒ self

Parameters:

  • threshold (Float)

    minimum precision score (0.0 - 1.0)

Returns:

  • (self)


59
60
61
62
# File 'lib/eval_ruby/rspec.rb', line 59

def above(threshold)
  @threshold = threshold
  self
end

#failure_messageString

Returns:

  • (String)


77
78
79
# File 'lib/eval_ruby/rspec.rb', line 77

def failure_message
  "expected precision@#{@k} >= #{@threshold}, but got #{@score.round(4)}"
end

#matches?(results) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
# File 'lib/eval_ruby/rspec.rb', line 66

def matches?(results)
  @results = results
  if results.is_a?(EvalRuby::RetrievalResult)
    @score = results.precision_at_k(@k)
  else
    raise ArgumentError, "Expected EvalRuby::RetrievalResult or use assert_precision_at_k"
  end
  @score >= @threshold
end