Class: Coopy::Viterbi

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/coopy/viterbi.rb

Instance Method Summary collapse

Constructor Details

#initializeViterbi

Returns a new instance of Viterbi.



7
8
9
10
11
12
13
# File 'lib/lib/coopy/viterbi.rb', line 7

def initialize
  @k = @t = 0
  self.reset
  @cost = ::Coopy::SparseSheet.new
  @src = ::Coopy::SparseSheet.new
  @path = ::Coopy::SparseSheet.new
end

Instance Method Details

#add_transition(s0, s1, c) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/lib/coopy/viterbi.rb', line 53

def add_transition(s0,s1,c)
  resize = false
  if s0 >= @k 
    @k = s0 + 1
    resize = true
  end
  if s1 >= @k 
    @k = s1 + 1
    resize = true
  end
  if resize 
    @cost.non_destructive_resize(@k,@t,0)
    @src.non_destructive_resize(@k,@t,-1)
    @path.non_destructive_resize(1,@t,-1)
  end
  @path_valid = false
  self.assert_mode(1)
  if @index >= @t 
    @t = @index + 1
    @cost.non_destructive_resize(@k,@t,0)
    @src.non_destructive_resize(@k,@t,-1)
    @path.non_destructive_resize(1,@t,-1)
  end
  sourced = false
  if @index > 0 
    c += @cost.get(s0,@index - 1)
    sourced = @src.get(s0,@index - 1) != -1
  else 
    sourced = true
  end
  if sourced 
    if c < @cost.get(s1,@index) || @src.get(s1,@index) == -1 
      @cost.set(s1,@index,c)
      @src.set(s1,@index,s0)
    end
  end
end

#begin_transitionsObject



96
97
98
99
# File 'lib/lib/coopy/viterbi.rb', line 96

def begin_transitions 
  @path_valid = false
  self.assert_mode(1)
end

#calculate_pathObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/lib/coopy/viterbi.rb', line 101

def calculate_path 
  return if @path_valid
  self.end_transitions
  best = 0
  bestj = -1
  if @index <= 0 
    @path_valid = true
    return
  end
  begin
    _g1 = 0
    _g = @k
    while(_g1 < _g) 
      j = _g1
      _g1+=1
      if (@cost.get(j,@index - 1) < best || bestj == -1) && @src.get(j,@index - 1) != -1 
        best = @cost.get(j,@index - 1)
        bestj = j
      end
    end
  end
  @best_cost = best
  begin
    _g11 = 0
    _g2 = @index
    while(_g11 < _g2) 
      j1 = _g11
      _g11+=1
      i = @index - 1 - j1
      @path.set(0,i,bestj)
      puts "Problem in Viterbi" if !(bestj != -1 && (bestj >= 0 && bestj < @k))
      bestj = @src.get(bestj,i)
    end
  end
  @path_valid = true
end

#end_transitionsObject



91
92
93
94
# File 'lib/lib/coopy/viterbi.rb', line 91

def end_transitions 
  @path_valid = false
  self.assert_mode(0)
end

#get(i) ⇒ Object



164
165
166
167
# File 'lib/lib/coopy/viterbi.rb', line 164

def get(i)
  self.calculate_path
  @path.get(0,i)
end

#get_costObject



169
170
171
172
# File 'lib/lib/coopy/viterbi.rb', line 169

def get_cost 
  self.calculate_path
  @best_cost
end

#lengthObject



159
160
161
162
# File 'lib/lib/coopy/viterbi.rb', line 159

def length 
  self.calculate_path if @index > 0
  @index
end

#resetObject



29
30
31
32
33
34
# File 'lib/lib/coopy/viterbi.rb', line 29

def reset 
  @index = 0
  @mode = 0
  @path_valid = false
  @best_cost = 0
end

#set_size(states, sequence_length) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/lib/coopy/viterbi.rb', line 36

def set_size(states,sequence_length)
  @k = states
  @t = sequence_length
  @cost.resize(@k,@t,0)
  @src.resize(@k,@t,-1)
  @path.resize(1,@t,-1)
end

#to_sObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/lib/coopy/viterbi.rb', line 138

def to_s 
  self.calculate_path
  txt = ""
  begin
    _g1 = 0
    _g = @index
    while(_g1 < _g) 
      i = _g1
      _g1+=1
      if @path.get(0,i) == -1 
        txt += "*"
      else 
        txt += @path.get(0,i)
      end
      txt += " " if @k >= 10
    end
  end
  txt += " costs " + _hx_str(self.get_cost)
  txt
end