Class: Coopy::Viterbi
- Inherits:
-
Object
- Object
- Coopy::Viterbi
- Defined in:
- lib/lib/coopy/viterbi.rb
Instance Method Summary collapse
- #add_transition(s0, s1, c) ⇒ Object
- #assert_mode(_next) ⇒ Object
- #begin_transitions ⇒ Object
- #calculate_path ⇒ Object
- #end_transitions ⇒ Object
- #get(i) ⇒ Object
- #get_cost ⇒ Object
-
#initialize ⇒ Viterbi
constructor
A new instance of Viterbi.
- #length ⇒ Object
- #reset ⇒ Object
- #set_size(states, sequence_length) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Viterbi
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
49 50 51 52 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 |
# File 'lib/lib/coopy/viterbi.rb', line 49 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 |
#assert_mode(_next) ⇒ Object
44 45 46 47 |
# File 'lib/lib/coopy/viterbi.rb', line 44 def assert_mode(_next) @index+=1 if _next == 0 && @mode == 1 @mode = _next end |
#begin_transitions ⇒ Object
92 93 94 95 |
# File 'lib/lib/coopy/viterbi.rb', line 92 def begin_transitions @path_valid = false self.assert_mode(1) end |
#calculate_path ⇒ Object
97 98 99 100 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 |
# File 'lib/lib/coopy/viterbi.rb', line 97 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) ::Haxe::Log._trace.call("Problem in Viterbi",{ file_name: "Viterbi.hx", line_number: 119, class_name: "coopy.Viterbi", method_name: "calculatePath"}) if !(bestj != -1 && (bestj >= 0 && bestj < @k)) bestj = @src.get(bestj,i) end end @path_valid = true end |
#end_transitions ⇒ Object
87 88 89 90 |
# File 'lib/lib/coopy/viterbi.rb', line 87 def end_transitions @path_valid = false self.assert_mode(0) end |
#get(i) ⇒ Object
160 161 162 163 |
# File 'lib/lib/coopy/viterbi.rb', line 160 def get(i) self.calculate_path return @path.get(0,i) end |
#get_cost ⇒ Object
165 166 167 168 |
# File 'lib/lib/coopy/viterbi.rb', line 165 def get_cost self.calculate_path return @best_cost end |
#length ⇒ Object
155 156 157 158 |
# File 'lib/lib/coopy/viterbi.rb', line 155 def length self.calculate_path if @index > 0 return @index end |
#reset ⇒ Object
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_s ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/lib/coopy/viterbi.rb', line 134 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) return txt end |