summaryrefslogtreecommitdiff
path: root/rerank.rb
blob: 9f7233c87f8d128fd2595f2ad0d8479675e61a48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby

require 'zipf'

class KbestItem
  attr_accessor :rank, :model, :rr, :gold, :f
  def initialize s
    a = s.split "\t"
    @rank = a[0].to_i
    @gold = a[1].to_f
    @model = a[2].to_f
    @rr    = -1.0
    @f = SparseVector.from_kv a[3], "=", " "
  end

  def to_s
    return "#{@model}\t#{@gold}"
  end
end

w = SparseVector.from_kv ReadFile.new(ARGV[0]).read, /\s/, "\n"

def o kl
  scores = []
  scores << kl.first.gold
  kl.sort! { |i,j| j.model <=> i.model }
  scores << kl.first.gold
  kl.sort! { |i,j| j.rr <=> i.rr }
  scores << kl.first.gold

  puts scores.join "\t"
end

STDERR.write "reranking..\n"
cur = []
k_sum = 0
j = 0
while line = STDIN.gets
  item = KbestItem.new line.strip
  item.rr = w.dot(item.f)
  if item.rank == 0 && cur.size > 0
    o cur
    cur = []
    j += 1
  end
  cur << item
end
o cur