diff options
Diffstat (limited to 'lib/nlp_ruby/ttable.rb')
-rw-r--r-- | lib/nlp_ruby/ttable.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/nlp_ruby/ttable.rb b/lib/nlp_ruby/ttable.rb index 20b1412..598e318 100644 --- a/lib/nlp_ruby/ttable.rb +++ b/lib/nlp_ruby/ttable.rb @@ -15,3 +15,62 @@ def read_phrase_table fn return table end +class Translation + attr_accessor :id, :s, :raw, :f, :score + + def initialize id=nil, raw=nil, s=nil, f=nil, score=nil + @id = id + @raw = raw + @s = s + @f = f + @score = score + end + + def from_s t, strip_alignment=true + id, raw, features, score = splitpipe(t, 3) + raw.strip! + @raw = raw + if strip_alignment # the way moses does it + @s = @raw.gsub(/\s*\|\d+-\d+\||\|-?\d+\|\s*/, ' ').gsub(/\s+/, ' ') + @s.strip! + else + @s = raw + end + @id = id.to_i + @f = read_feature_string features + @score = score.to_f + end + + def to_s + [id, s, f.to_kv, score].join ' ||| ' + end +end + +def read_kbest_lists fn, translation_type=Translation + kbest_lists = [] + cur = [] + f = ReadFile.new fn + prev = -1 + c = 0 + id = 0 + while line = f.gets + t = translation_type.new + t.from_s line + c = splitpipe(line)[0].to_i + if c != prev + if cur.size > 0 + kbest_lists << cur + cur = [] + end + prev = c + id = 0 + end + t.id = id + cur << t + id += 1 + end + kbest_lists << cur # last one + f.close + return kbest_lists +end + |