diff options
author | Patrick Simianer <p@simianer.de> | 2016-04-08 23:19:28 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2016-04-08 23:19:28 +0200 |
commit | 34d621606e58adeb35aa33f8503e105189a7c0d5 (patch) | |
tree | 18931029e5ceed1e2c2d77bfdf2b87a216633366 /voted_rerank.rb | |
parent | 81f6b20d1c41d8906900c57ab71ec08a007ea02c (diff) |
Diffstat (limited to 'voted_rerank.rb')
-rwxr-xr-x | voted_rerank.rb | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/voted_rerank.rb b/voted_rerank.rb new file mode 100755 index 0000000..684825d --- /dev/null +++ b/voted_rerank.rb @@ -0,0 +1,67 @@ +#!/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 + +ws = [] +cs = [] +ReadFile.readlines_strip(ARGV[0]).each { |l| + c, s = l.split "\t" + cs << c.to_i + next if !s||s.strip=="" + ws << SparseVector.from_kv(s, "=", " ") +} + +def sign(x) + if x <= 0 + return -1.0 + else + return 1.0 + end +end + +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 = 0 + ws.each_with_index{ |w,j| + item.rr += sign(w.dot(x))*cs[j] + } + if item.rank == 0 && cur.size > 0 + o cur + cur = [] + j += 1 + end + cur << item +end +o cur + |