Class: Translation

Inherits:
Object
  • Object
show all
Defined in:
lib/zipf/Translation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, raw = nil, s = nil, f = nil, scores = {}, rank = nil) ⇒ Translation

Returns a new instance of Translation.



4
5
6
7
8
9
10
11
# File 'lib/zipf/Translation.rb', line 4

def initialize id=nil, raw=nil, s=nil, f=nil, scores={}, rank=nil
  @id = id
  @raw = raw
  @s = s
  @f = f
  @scores = scores
  @rank = rank
end

Instance Attribute Details

#fObject

Returns the value of attribute f.



2
3
4
# File 'lib/zipf/Translation.rb', line 2

def f
  @f
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/zipf/Translation.rb', line 2

def id
  @id
end

#rankObject

Returns the value of attribute rank.



2
3
4
# File 'lib/zipf/Translation.rb', line 2

def rank
  @rank
end

#rawObject

Returns the value of attribute raw.



2
3
4
# File 'lib/zipf/Translation.rb', line 2

def raw
  @raw
end

#sObject

Returns the value of attribute s.



2
3
4
# File 'lib/zipf/Translation.rb', line 2

def s
  @s
end

#scoresObject

Returns the value of attribute scores.



2
3
4
# File 'lib/zipf/Translation.rb', line 2

def scores
  @scores
end

Class Method Details

.from_s(s) ⇒ Object



29
30
31
32
33
# File 'lib/zipf/Translation.rb', line 29

def self.from_s s
  t = self.new
  t.from_s s
  return t
end

Instance Method Details

#from_s(t, strip_alignment = true, rank = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/zipf/Translation.rb', line 13

def from_s t, strip_alignment=true, rank=nil
  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 = SparseVector.from_kv features
  @scores[:decoder] = score.to_f
  @rank = rank
end

#to_s(include_features = true) ⇒ Object



35
36
37
38
# File 'lib/zipf/Translation.rb', line 35

def to_s include_features=true
  [@id, @s, @f.to_kv('=', ' '), @scores[:decoder]].join(' ||| ') if include_features
  [@id, @s, @scores[:decoder]].join(' ||| ') if !include_features
end

#to_s2Object



40
41
42
# File 'lib/zipf/Translation.rb', line 40

def to_s2
  [@rank, @s, @score, @scores.to_s].join ' ||| '
end