-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViterbi.cs
186 lines (173 loc) · 7.2 KB
/
Viterbi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
86
87
88
89
90
91
92
93
94
95
96
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Master.Algorithms;
namespace Master.Utility
{
/// <summary>
/// Class represents Viterbi decoder for finding most probable path through
/// states of hidden Markov model.
/// Author: Tomislav
/// </summary>
class Viterbi
{
private Mapper stateToIntMap;
private Mapper obsToIntMap;
private SortedDictionary<int, SortedDictionary<int, int>> stateToObservation;
private SortedDictionary<int, SortedDictionary<int, double>> stateObsGTProbs;
private HMM hmm;
private int order;
public Viterbi(Mapper stateToIntMap, Mapper obsToIntMap,
SortedDictionary<int, SortedDictionary<int, int>> stateToObservation, HMM hmm, int order) {
this.stateToIntMap = stateToIntMap;
this.obsToIntMap = obsToIntMap;
this.stateToObservation = stateToObservation;
this.hmm = hmm;
this.order = order;
this.stateObsGTProbs = new SortedDictionary<int, SortedDictionary<int, double>>();
foreach (KeyValuePair<int, SortedDictionary<int, int>> pair in stateToObservation) {
GoodTuring gt = new GoodTuring(CountsCalculator.calculateCoC(pair.Value), obsToIntMap.size());
gt.calculate();
stateObsGTProbs.Add(pair.Key, gt.getCountProb());
}
}
/// <summary>
/// Method processes (decodes) single sentence in one string using Viterbi algorithm
/// </summary>
/// <param name="sentence">Sentence with whitespace separated words</param>
/// <returns></returns>
public String[] process(String sentence) {
String[] sequence = sentence.Split(null);
return process(sequence);
}
/// <summary>
/// Method processes (decodes) single sentence with words in array using Viterbi algorithm
/// </summary>
/// <param name="sequence">Sentence with words in array</param>
/// <returns></returns>
public String[] process(String[] sequence) {
int[] obs = new int[sequence.Length];
int[] states = new int[sequence.Length];
String[] ret = new String[sequence.Length];
for (int i = 0; i < sequence.Length; i++) {
int obsValue = obsToIntMap.getValue(sequence[i]);
obs[i] = obsValue;
}
int noStates = stateToIntMap.size();
double [,] t1 = new double [noStates, sequence.Length];
int [,,] t2 = new int [noStates, sequence.Length, 2];
for (int i = 0; i < noStates && sequence.Length >= 1; i++) {
t1[i, 0] = hmm.getStateToStateProb(new List<int>() { -1, i }) + getStateToObservationSGT(i, obs[0]);
t2[i, 0, 0] = -1;
t2[i, 0, 1] = -1;
}
for (int i = 0; i < noStates && sequence.Length >= 2; i++) {
double max = Double.NegativeInfinity;
int index = 0;
for (int j = 0; j < noStates; j++) {
List<int> key;
if (order == 2) {
key = new List<int>() { j, i };
}
else {
key = new List<int>() { -1, j, i };
}
double tmp = t1[j, 0] + hmm.getStateToStateProb(key) + getStateToObservationSGT(i, obs[1]);
if (!Double.IsNaN(tmp) && tmp > max) {
max = tmp;
index = j;
}
}
t1[i, 1] = max;
t2[i, 1, 0] = -1;
t2[i, 1, 1] = index;
}
for (int i = 2; i < sequence.Length; i++) {
for (int j = 0; j < noStates; j++) {
double max = Double.NegativeInfinity;
int index0 = 0;
int index1 = 0;
for (int k = 0; k < noStates; k++) {
int tmpIndex = t2[k, i - 1, 1];
List<int> key;
if (order == 2) {
key = new List<int>() { k, j };
}
else {
key = new List<int>() { tmpIndex, k, j };
}
double tmp = t1[k, i - 1] + hmm.getStateToStateProb(key) + getStateToObservationSGT(j, obs[i]);
if (!Double.IsNaN(tmp) && tmp > max) {
max = tmp;
index0 = tmpIndex;
index1 = k;
}
}
t1[j, i] = max;
t2[j, i, 0] = index0;
t2[j, i, 1] = index1;
}
}
double _max = Double.NegativeInfinity;
int activeState = -1;
for (int i = 0; i < noStates; i++) {
if (t1[i, sequence.Length - 1] > _max) {
_max = t1[i, sequence.Length - 1];
activeState = i;
}
}
if (activeState == -1) {
return null;
}
states[sequence.Length - 1] = activeState;
for (int i = sequence.Length - 1; i >= 1; i--) {
states[i - 1] = t2[states[i],i, 1];
}
for (int i = 0; i < states.Length; i++) {
ret[i] = stateToIntMap.getKey(states[i]);
}
return ret;
}
/// <summary>
/// Method returns frequency observation probability
/// </summary>
/// <param name="state">State</param>
/// <param name="observation">State observation</param>
/// <returns></returns>
private double getStateToObservationFrequency(int state, int observation) {
if (!stateToObservation.ContainsKey(state)) {
return Math.Log(0,2);
}
SortedDictionary<int, int> map = stateToObservation[state];
if (!map.ContainsKey(observation)) {
return Math.Log(0,2);
}
int count = map[observation];
int sum = 0;
foreach (KeyValuePair<int, int> pair in map) {
sum += pair.Value;
}
return Math.Log(((double)count) / sum,2);
}
/// <summary>
/// Method returns Good-Turing observation probability
/// </summary>
/// <param name="state">State</param>
/// <param name="observation">State observation</param>
/// <returns></returns>
private double getStateToObservationSGT(int state, int observation) {
if (!stateToObservation.ContainsKey(state)) {
return Math.Log(0,2);
}
SortedDictionary<int, int> map = stateToObservation[state];
if (!map.ContainsKey(observation)) {
return Math.Log(stateObsGTProbs[state][0],2);
}
else {
int count = map[observation];
return Math.Log(stateObsGTProbs[state][count],2);
}
}
}
}