-
Notifications
You must be signed in to change notification settings - Fork 0
/
Viterbi.cpp
100 lines (80 loc) · 2.83 KB
/
Viterbi.cpp
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
#include "Viterbi.h"
#include "Reconnaissance.h"
void viterbi(const vector<vector<double>>& neuralNetworkOutput, vector<int>& retour)
{
/// Initialisation
vector<vector<double> > delta(neuralNetworkOutput.size(),vector<double> (33,0));
vector<vector<int> > psi(neuralNetworkOutput.size(), vector<int> (33,0));
for (unsigned int i(0); i < neuralNetworkOutput[0].size();i++)
delta[0][i] = fichiers.initialisation[i] * neuralNetworkOutput[0][i];
/// Récurrence
for (unsigned int t(1) ; t < neuralNetworkOutput.size() ; t++)
{
for (unsigned int j(0) ; j < neuralNetworkOutput[t].size() ; j++)
{
if (neuralNetworkOutput[t][j])
{
for (unsigned int i(0) ; i < neuralNetworkOutput[t-1].size() ; i++)
{
if (delta[t-1][i] * fichiers.phonemTransitionMatrix[i][j] > delta[t][j])
{
delta[t][j] = delta[t-1][i] * fichiers.phonemTransitionMatrix[i][j];
psi[t][j] = i;
}
}
delta[t][j] *= neuralNetworkOutput[t][j];
}
}
}
/// Retropropagation
retour.clear();
retour.reserve(psi.size());
int it = 0;
double buf =0;
for (unsigned int i(0) ; i < delta[neuralNetworkOutput.size()-1].size() ; i++)
{
if (buf < delta[neuralNetworkOutput.size()-1][i])
{
buf = delta[neuralNetworkOutput.size()-1][i];
it = i;
}
}
retour.push_back(it);
for (int t(neuralNetworkOutput.size()-1) ; t >= 0 ; t--)
retour.insert(retour.begin(), psi[t][retour[0]]);
}
void reduction(vector<int>& listePhonemes)
{
vector<int> pos;
for (unsigned i(0) ; i < listePhonemes.size() ; i++)
{
if (listePhonemes[i] == 0)
{
pos.push_back(i);
}
else
{
int k = 0;
const int& longueurMin = fichiers.longueursPhonemes[listePhonemes[i]].first;
const int& longueurMax = fichiers.longueursPhonemes[listePhonemes[i]].second;
for (unsigned j( max((unsigned)0, i-longueurMin) ) ; j < min( i+longueurMin, listePhonemes.size() ) ; j++)
{
if (listePhonemes[j] == listePhonemes[i])
k++;
}
if (k < longueurMin)
pos.push_back(i);
else if (k > longueurMax)
pos.push_back(i);
}
}
for (unsigned i(0) ; i < pos.size() ; i++)
listePhonemes.erase( listePhonemes.begin() + pos[pos.size()-i-1] );
for (unsigned i(0) ; i+1 < listePhonemes.size() ; i++)
{
while (i+1 < listePhonemes.size() && listePhonemes[i] == listePhonemes[i+1])
{
listePhonemes.erase( listePhonemes.begin()+i+1 );
}
}
}