class Coopy::Viterbi

Attributes

best_cost[RW]
cost[RW]
index[RW]
k[RW]
mode[RW]
path[RW]
path_valid[RW]
src[RW]
t[RW]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

add_transition(s0,s1,c) click to toggle source
# 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_transitions() click to toggle source
# File lib/lib/coopy/viterbi.rb, line 96
def begin_transitions 
  @path_valid = false
  self.assert_mode(1)
end
calculate_path() click to toggle source
# 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_transitions() click to toggle source
# File lib/lib/coopy/viterbi.rb, line 91
def end_transitions 
  @path_valid = false
  self.assert_mode(0)
end
get(i) click to toggle source
# File lib/lib/coopy/viterbi.rb, line 164
def get(i)
  self.calculate_path
  @path.get(0,i)
end
get_cost() click to toggle source
# File lib/lib/coopy/viterbi.rb, line 169
def get_cost 
  self.calculate_path
  @best_cost
end
length() click to toggle source
# File lib/lib/coopy/viterbi.rb, line 159
def length 
  self.calculate_path if @index > 0
  @index
end
reset() click to toggle source
# 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) click to toggle source
# 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_s() click to toggle source
# 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

Protected Instance Methods

assert_mode(_next) click to toggle source
# File lib/lib/coopy/viterbi.rb, line 46
def assert_mode(_next)
  @index+=1 if _next == 0 && @mode == 1
  @mode = _next
end