Class: Retreval::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/retreval/runner.rb

Overview

A simple class that performs the task of running this library when called from the commandline

Instance Method Summary (collapse)

Constructor Details

- (Runner) initialize(args)

Invokes a new Runner object by loading the options from the commandline



16
17
18
# File 'lib/retreval/runner.rb', line 16

def initialize(args)
  @options = Options.new(args)
end

Instance Method Details

- (Object) begin_calculations

Performs the default calculations and writes their output to the file specified



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/retreval/runner.rb', line 67

def begin_calculations
  @statistics = Hash.new
  @average_precision = Hash.new
  
  @query_result_set.query_results.each_with_index do |result, index|
    begin
      print "Cleaning up results and removing documents without judgements ... \n" if $verbose
      result.cleanup
      
      print "Calculating statistics for result #{index+1} of #{@query_result_set.query_results.count} ... "
      @statistics[result.query.querystring] = result.statistics
      @average_precision[result.query.querystring] = result.average_precision
      print "Done.\n"
      
      result.print_ranked_table if $verbose          
      
      write_to_yaml_file :data => @statistics, :filename => "statistics.yml"
      write_to_yaml_file :data => @average_precision, :filename => "avg_precision.yml"
      
    # rescue Exception => e
    #  raise "Error while calculating results: #{e}"
    end
  end
  
  print "Finished calculating all results. Exiting.\n" if $verbose
  print "The mean average precision was #{@query_result_set.mean_average_precision}\n" if $verbose
  exit
  
end

- (Object) load_gold_standard

Takes the passed options for a GoldStandard file and loads it according to the format specified



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/retreval/runner.rb', line 21

def load_gold_standard
  unless @options.gold_standard_file.nil?
    
      
    print "Loading gold standard file '#{@options.gold_standard_file}' ... " if $verbose
    @gold_standard = GoldStandard.new
    case @options.format
    when "yaml"
      @gold_standard.load_from_yaml_file @options.gold_standard_file
    when "plain"
      @gold_standard.load_from_plaintext_file @options.gold_standard_file
    else
      raise "I don't understand the format '#{@options.format}'"
    end
    
    print "done\n" if $verbose
    print "Gold standard loaded from #{@options.gold_standard_file} contains:
      - #{@gold_standard.queries.count} queries, 
      - #{@gold_standard.documents.count} documents,
      - #{@gold_standard.judgements.count} judgements, made by
      - #{@gold_standard.users.count} users\n\n" if $verbose        
  end
end

- (Object) load_query_result_set

Takes the passed options for a QueryResultSet file and loads it according to the format specified



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/retreval/runner.rb', line 46

def load_query_result_set
  unless @options.query_result_set_file.nil?
      
    print "Loading query result set from file '#{@options.query_result_set_file}' ... " if $verbose
    @query_result_set = QueryResultSet.new :gold_standard => @gold_standard
    case @options.format
    when "yaml"
      @query_result_set.load_from_yaml_file @options.query_result_set_file
    when "plain"
      @query_result_set.load_from_yaml_file @options.query_result_set_file
    else
      raise "I don't understand the format '#{@options.format}'"
    end
    
    print "done\n" if $verbose
    print "Query results loaded from #{@options.query_result_set_file} contain:
      - #{@query_result_set.query_results.count} query results\n\n" if $verbose
  end
end

- (Object) run

Called when the script is executed from the command line



113
114
115
116
117
118
119
# File 'lib/retreval/runner.rb', line 113

def run
  
  load_gold_standard
  load_query_result_set
  begin_calculations
  
end

- (Object) write_to_yaml_file(args)

Writes an object to a YAML file. Called by:

write_to_yaml_file :data => my_data, :filename => "my_data_file.yml"


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/retreval/runner.rb', line 100

def write_to_yaml_file(args)
  data = args[:data]
  filename = args[:filename]
  
  if data.nil? or filename.nil? 
    raise "Must pass filename and data in order to write to file!"
  end
  
  filename = @options.output + "_" + filename
  File.open(filename, "w") { |f| f.write data.to_yaml }
end