Class: Retreval::RankedQueryResult

Inherits:
QueryResult show all
Defined in:
lib/retreval/query_result.rb

Overview

A RankedQueryResult is a QueryResult with special functions for ranked retrieval evaluation.

Instance Attribute Summary

Attributes inherited from QueryResult

#documents, #gold_standard, #query

Instance Method Summary collapse

Methods inherited from QueryResult

#<<, #add_document, #cleanup, #f_measure, #load_from_yaml_file, #print_contingency_table

Constructor Details

#initialize(args) ⇒ RankedQueryResult

Creates a new RankedQueryResult. One has to specify the query string and can optionally pass a Document array too. The rank of the Document will be defined by its position in the array only. Called by:

RankedQueryResult.new :documents => array_of_document_ids, :query => "my query"
RankedQueryResult.new :query => "my query"


319
320
321
# File 'lib/retreval/query_result.rb', line 319

def initialize(args)
  super(args)
end

Instance Method Details

#average_precisionObject

Returns the average precision. It is the average of precisions computed at the point of each of the relevant documents in the ranked sequence.



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/retreval/query_result.rb', line 417

def average_precision
  begin
    # Calculate the results first if we haven't done this before
    statistics unless @calculated
    
    total_relevant_documents = @gold_standard.documents.values.count { |doc| @gold_standard.relevant? :document => doc.id, :query => @query.querystring  }
    
    if total_relevant_documents > 0
      # The sum is calculated by adding the precision for a relevant document, or 0 for a nonrelevant document
      return @average_precision = @results.inject(0.0) { |sum, document| document[:relevant] ? document[:precision] + sum : sum } / total_relevant_documents
    else
      return @average_precision = 0
    end
  rescue Exception => e
    raise "Error while calculating average precision: " + e.message
  end
  
end

#eleven_point_precisionObject

Calculates the 11-point precision and the average interpolated precision. See: nlp.stanford.edu/IR-book/html/htmledition/evaluation-of-ranked-retrieval-results-1.html



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/retreval/query_result.rb', line 326

def eleven_point_precision
  
  statistics unless @calculated

  @recall_levels = Hash.new

  # Find out what recall/precision pairs we already know
  @results.each_with_index do |row, index|
    precision = row[:precision]
    recall = row[:recall]
    @recall_levels[recall] = precision
  end

  begin
    @eleven_point_precision = Hash.new

    # Calculate the 11 points
    # This outer loop effectively iterates from 0.0 to 1.0
    (0..10).each_with_index do |recall_level, index|
      recall_level = recall_level.to_f / 10
      @eleven_point_precision[recall_level] = 0

      # Look in our known recall levels (stored as the keys)
      @recall_levels.keys.each do |key|
        
        # If we find a known recall equal or higher to the one from the 11-point
        # scale, return the precision at that level
        # E.g. if our level is 0.3 and we have a known precision at 0.5, this
        # one will be used as our interpolated precision
        if key >= recall_level
          precision_for_level = @recall_levels[key]
          # Store the interpolated precision at the current level, e.g. 0.3
          @eleven_point_precision[recall_level] = precision_for_level
          break
        end
      end
    end

    # Now calculate an average precision for this statistic
    # That's a neat line of ruby code, is it?
    @eleven_point_average = @eleven_point_precision.values.inject(0.0) { |sum, precision| sum + precision } / 11
    
  rescue
    raise "Error while calculating the 11-point precision map!"
  end

  @eleven_point_precision
  
end

Prints a pretty table for 11-point interpolated precision



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/retreval/query_result.rb', line 438

def print_eleven_point_precision_table
  
  # Calculate the results first if we haven't done this before
  statistics unless @calculated
  
  data = eleven_point_precision
  print "Recall\tInterpolated Precision\n"
  data.each_pair do |recall, precision|
    print recall.to_s + "\t" + "%.3f" % precision + "\n"
  end
  print "--------------------------------------\n"
  print "Avg.\t" + "%.3f" % @eleven_point_average + "\n"
  print "\n"
  
end

Prints a pretty table for ranked results



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/retreval/query_result.rb', line 456

def print_ranked_table
  
  # Calculate the results first if we haven't done this before
  statistics unless @calculated
        
  # Use the results to print a table
  print "Query: #{@query.querystring}\n"
  print "Index\tRelevant\tPrecision\tRecall\tScore\t\tDocument ID\n"
  @results.each_with_index do |row, index|
    precision = "%.3f" % row[:precision]
    document = @documents.values[index].id
    recall = "%.3f" %  row[:recall]
    relevant = row[:relevant] ? "[X]" : "[ ]"
    print "#{index+1}\t" + relevant + "\t\t" + precision + "\t\t" + recall + "\t" + @documents.values[index].score.to_s + "\t" + document + "\n"
  end
  print "\n"
  
end

#statistics(max = 0) ⇒ Object

Calculates the precision and recall for each rank and returns it in a Hash of results



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/retreval/query_result.rb', line 379

def statistics(max = 0)
  
  return @results if @calculated
  
  begin
  
    # If no maximum parameter is given, all documents are evalutated
    # This should be the default for normal evaluations
    max = @documents.length if max == 0 or max > @documents.length
  
    # Calculate precision and recall for the top i documents only
    @results = Array.new
    for i in (1..max)
      subset = OpenStruct.new
      subset.documents = Array.new
      subset.query = @query
      @documents.values.each_with_index do |doc, index|
        # Only get the subset of documents
        subset.documents << doc
        break if index == i - 1
      end
      results = calculate(subset)
      @results << results
    end
  
    # Now mark everything as calculated and return it
    @calculated = true
    @results
  
  rescue Exception => e
    raise "Error while calculating results: " + e.message
  end
  
end