-
Notifications
You must be signed in to change notification settings - Fork 3
/
HMMTables.cpp
177 lines (164 loc) · 5.33 KB
/
HMMTables.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
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
/*
Copyright (C) 1998,1999,2000,2001 Franz Josef Och (RWTH Aachen - Lehrstuhl fuer Informatik VI)
This file is part of GIZA++ ( extension of GIZA ).
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
*/
#include "HMMTables.h"
#include <fstream>
#include "Globals.h"
#include "Parameter.h"
template<class CLS,class MAPPERCLASSTOSTRING>
void HMMTables<CLS,MAPPERCLASSTOSTRING>::writeJumps(ostream&out) const
{
double ssum=0.0;
for(typename map<AlDeps<CLS>,FlexArray<double> >::const_iterator i=alProb.begin();i!=alProb.end();++i)
{
double sum=0.0;
out << "\n\nDistribution for: ";
printAlDeps(out,i->first,*mapper1,*mapper2);
out << ' ';
for(int a=i->second.low();a<=i->second.high();++a)
if( i->second[a] )
{
out << a << ':' << i->second[a] << ';' << ' ';
sum+=i->second[a];
}
out << '\n' << '\n';
out << "SUM: " << sum << '\n';
ssum+=sum;
}
out << "FULL-SUM: " << ssum << '\n';
}
template<class CLS,class MAPPERCLASSTOSTRING>
void HMMTables<CLS,MAPPERCLASSTOSTRING>::readJumps(istream&)
{
}
template<class CLS,class MAPPERCLASSTOSTRING>
double HMMTables<CLS,MAPPERCLASSTOSTRING>::getAlProb(int istrich,int k,int sentLength,int J,CLS w1,CLS w2,int j,int iter) const
{
massert(k<sentLength&&k>=0);
massert(istrich<sentLength&&istrich>=-1);
int pos=istrich-k;
switch(PredictionInAlignments)
{
case 0: pos=istrich-k; break;
case 1: pos=k; break;
case 2:
pos=(k*J-j*sentLength);
if( pos>0 ) pos+=J/2; else pos-=J/2;
pos/=J;
break;
default:abort();
}
typename map<AlDeps<CLS>,FlexArray<double> >::const_iterator p=alProb.find(AlDeps<CLS>(sentLength,istrich,j,w1,w2));
if( p!=alProb.end() )
{
return (p->second)[pos];
}
else
{
if( iter>0&&iter<5000 )
cout << "WARNING: Not found: " << ' ' << J << ' ' << sentLength << '\n';;
return 1.0/(2*sentLength-1);
}
}
template<class CLS,class MAPPERCLASSTOSTRING>
void HMMTables<CLS,MAPPERCLASSTOSTRING>::addAlCount(int istrich,int k,int sentLength,int J,CLS w1,CLS w2,int j,double value,double valuePredicted)
{
int pos=istrich-k;
switch(PredictionInAlignments)
{
case 0: pos=istrich-k; break;
case 1: pos=k; break;
case 2:
pos=(k*J-j*sentLength);
if( pos>0 ) pos+=J/2; else pos-=J/2;
pos/=J;
break;
default:abort();
}
AlDeps<CLS> deps(AlDeps<CLS>(sentLength,istrich,j,w1,w2));
{
typename map<AlDeps<CLS>,FlexArray<double> >::iterator p=alProb.find(deps);
if( p==alProb.end() )
{
if( (CompareAlDeps&1)==0 )
p=alProb.insert(make_pair(deps,FlexArray<double> (-MAX_SENTENCE_LENGTH,MAX_SENTENCE_LENGTH,0.0))).first;
else
p=alProb.insert(make_pair(deps,FlexArray<double> (-sentLength,sentLength,0.0))).first;
}
p->second[pos]+=value;
}
if( valuePredicted )
{
typename map<AlDeps<CLS>,FlexArray<double> >::iterator p=alProbPredicted.find(deps);
if( p==alProbPredicted.end() )
{
if( (CompareAlDeps&1)==0 )
p=alProbPredicted.insert(make_pair(deps,FlexArray<double> (-MAX_SENTENCE_LENGTH,MAX_SENTENCE_LENGTH,0.0))).first;
else
p=alProbPredicted.insert(make_pair(deps,FlexArray<double> (-sentLength,sentLength,0.0))).first;
}
p->second[pos]+=valuePredicted;
}
}
template<class CLS,class MAPPERCLASSTOSTRING>
Array<double>&HMMTables<CLS,MAPPERCLASSTOSTRING>::doGetAlphaInit(int I)
{
if( !init_alpha.count(I) )
init_alpha[I]=Array<double>(I,0);
return init_alpha[I];
}
template<class CLS,class MAPPERCLASSTOSTRING>
Array<double>&HMMTables<CLS,MAPPERCLASSTOSTRING>::doGetBetaInit(int I)
{
if( !init_beta.count(I) )
init_beta[I]=Array<double>(I,0);
return init_beta[I];
}
template<class CLS,class MAPPERCLASSTOSTRING>
bool HMMTables<CLS,MAPPERCLASSTOSTRING>::getAlphaInit(int I,Array<double>&x)const
{
hash_map<int,Array<double> >::const_iterator i=init_alpha.find(I);
if( i==init_alpha.end() )
return 0;
else
{
x=i->second;
for(unsigned int j=x.size()/2+1;j<x.size();++j) // only first empty word can be chosen
x[j]=0;
return 1;
}
}
template<class CLS,class MAPPERCLASSTOSTRING>
bool HMMTables<CLS,MAPPERCLASSTOSTRING>::getBetaInit(int I,Array<double>&x)const
{
hash_map<int,Array<double> >::const_iterator i=init_beta.find(I);
if( i==init_beta.end() )
return 0;
else
{
x=i->second;
return 1;
}
}
template<class CLS,class MAPPERCLASSTOSTRING>
HMMTables<CLS,MAPPERCLASSTOSTRING>:: HMMTables(double _probForEmpty,const MAPPERCLASSTOSTRING&m1,const MAPPERCLASSTOSTRING&m2):
probabilityForEmpty(mfabs(_probForEmpty)),
updateProbabilityForEmpty(_probForEmpty<0.0),
mapper1(&m1),
mapper2(&m2)
{}
template<class CLS,class MAPPERCLASSTOSTRING>
HMMTables<CLS,MAPPERCLASSTOSTRING>::~HMMTables() {}