summaryrefslogtreecommitdiff
path: root/rerank_output_lists.rb
blob: 22bd9ca978b4939c3a5a02b4f76a2df69dbf8ff2 (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
#!/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}\t#{@rank}\t#{@rr}"
  end
end

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

def o kl, j
  f = WriteFile.new "lists/#{j}.model"
  g = WriteFile.new "lists/#{j}.rr"
  kl.each { |i|
    f.write "#{i.gold}\t#{i.model}\n"
    g.write "#{i.gold}\t#{i.rr}\n"
  }
end

`mkdir -p lists`
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, j
    cur = []
    j += 1
  end
  cur << item
end
o cur, j