Method: Evoc::Evaluate.ap

Defined in:
lib/evoc/evaluate.rb

.ap(rec:, exp: nil) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/evoc/evaluate.rb', line 246

def self.ap(rec:,exp: nil)
  # AP is 0 for the empty list
  if rec.is_a?(Array) && rec.empty? # array and empty
    return nil
  end
  self.validateInput(rec)

  i = 0
  correct_i = 0
  ap = 0

  rec.each do |cluster|
    cluster.each do |item|
      i = i + 1
      correct_i = correct_i + item
      precision_i = correct_i/i
      ap = ap + (precision_i*item) 
    end
  end

  if exp.nil?
    exp = correct_i
  else
    if correct_i > exp
      raise ArgumentError, "Found more relevant items than the provided number of relevant items"
    end
  end
  return (exp == 0 ? 0 : (ap/exp).to_f)

end