-
Notifications
You must be signed in to change notification settings - Fork 3
/
TTables.cpp
405 lines (378 loc) · 12.7 KB
/
TTables.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
EGYPT Toolkit for Statistical Machine Translation
Written by Yaser Al-Onaizan, Jan Curin, Michael Jahr, Kevin Knight, John Lafferty, Dan Melamed, David Purdy, Franz Och, Noah Smith, and David Yarowsky.
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 "TTables.h"
#include "Parameter.h"
#include <math.h>
extern int interpolate;
extern int smooth_type;
extern double tamcutoff;
extern bool vb;
extern double vbalpha;
GLOBAL_PARAMETER(float,PROB_CUTOFF,"PROB CUTOFF","Probability cutoff threshold for lexicon probabilities",PARLEV_OPTHEUR,1e-7);
GLOBAL_PARAMETER2(float, COUNTINCREASE_CUTOFF,"COUNTINCREASE CUTOFF","countCutoff","Counts increment cutoff threshold",PARLEV_OPTHEUR,1e-6);
double digamma(double x) {
double result = 0, xx, xx2, xx4;
assert(x > 0);
for ( ; x < 7; ++x)
result -= 1/x;
x -= 1.0/2.0;
xx = 1.0/x;
xx2 = xx*xx;
xx4 = xx2*xx2;
result += log(x)+(1./24.)*xx2-(7.0/960.0)*xx4+(31.0/8064.0)*xx4*xx2-(127.0/30720.0)*xx4*xx4;
return result;
}
#ifdef BINARY_SEARCH_FOR_TTABLE
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::printCountTable(const char *,
const Vector<WordEntry>&,
const Vector<WordEntry>&,
const bool) const
{
}
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::printProbTable(const char *filename,
const Vector<WordEntry>& evlist,
const Vector<WordEntry>& fvlist,
const bool actual) const
{
ofstream of(filename);
/* for(unsigned int i=0;i<es.size()-1;++i)
for(unsigned int j=es[i];j<es[i+1];++j)
{
const CPPair&x=fs[j].second;
WordIndex e=i,f=fs[j].first;
if( actual )
of << evlist[e].word << ' ' << fvlist[f].word << ' ' << x.prob << '\n';
else
of << e << ' ' << f << ' ' << x.prob << '\n';
}*/
for(unsigned int i=0;i<lexmat.size();++i)
{
if( lexmat[i] )
for(unsigned int j=0;j<lexmat[i]->size();++j)
{
const CPPair&x=(*lexmat[i])[j].second;
WordIndex e=i,f=(*lexmat[i])[j].first;
//if( x.prob>PROB_SMOOTH )
if( actual )
of << evlist[e].word << ' ' << fvlist[f].word << ' ' << x.prob << '\n';
else
of << e << ' ' << f << ' ' << x.prob << '\n';
}
}
}
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::printProbTableInverse(const char *,
const Vector<WordEntry>&,
const Vector<WordEntry>&,
const double,
const double,
const bool ) const
{
}
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::normalizeTable(const vcbList&, const vcbList&, int)
{
if(vb){
for(unsigned int i=0;i<lexmat.size();++i){
double c=0.0;
if( lexmat[i] ){
unsigned int lSize=lexmat[i]->size();
for(unsigned int j=0;j<lSize;++j){
double count=(*lexmat[i])[j].second.count+vbalpha;
c+=count;
}
c=exp(digamma(c));
for(unsigned int j=0;j<lSize;++j){
if( c==0 )
(*lexmat[i])[j].second.prob=1.0/(lSize);
else{
double count=(*lexmat[i])[j].second.count+vbalpha;
count=exp(digamma(count));
(*lexmat[i])[j].second.prob=count/c;
}
(*lexmat[i])[j].second.count=0;
}
}
}
}
else{
for(unsigned int i=0;i<lexmat.size();++i){
double c=0.0;
if( lexmat[i] ){
unsigned int lSize=lexmat[i]->size();
for(unsigned int j=0;j<lSize;++j){
double count=(*lexmat[i])[j].second.count;
c+=count;
}
for(unsigned int j=0;j<lSize;++j){
if( c==0 )
(*lexmat[i])[j].second.prob=1.0/(lSize);
else{
double count=(*lexmat[i])[j].second.count;
(*lexmat[i])[j].second.prob=count/c;
}
(*lexmat[i])[j].second.count=0;
}
}
}
}
}
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::copyFromLM()
{
_lm.setForAlignment();
if(smooth_type==0){
double * p_tamcutoff=NULL;
if(tamcutoff>0)p_tamcutoff=&tamcutoff;
_lm.knEstimate(interpolate,p_tamcutoff);
}
else _lm.wbEstimate(interpolate);
cout<<"estimate done"<<endl;
for(unsigned int i=0;i<lexmat.size();++i)
{
double c=0.0;
if( lexmat[i] )
{
unsigned int lSize=lexmat[i]->size();
//for(unsigned int j=0;j<lSize;++j)
// c+=(*lexmat[i])[j].second.count;
for(unsigned int j=0;j<lSize;++j)
{
//if(c>0)
int e=i;
int f=(*lexmat[i])[j].first;
(*lexmat[i])[j].second.prob=_lm.bigramProb(f,e);
//else
// (*lexmat[i])[j].second.prob=0;
//cerr<<i<<","<<j<<":"<<(*lexmat[i])[j].second.prob<<endl;
(*lexmat[i])[j].second.count=0;
}
}
}
cerr<<"copy done"<<endl;
_lm.clear();
cerr<<"clear done"<<endl;
}
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::readProbTable(const char *){
}
template class tmodel<COUNT,PROB> ;
#else
/* ------------------ Method Definiotns for Class tmodel --------------------*/
#
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::printCountTable(const char *filename,
const Vector<WordEntry>& evlist,
const Vector<WordEntry>& fvlist,
const bool actual) const
// this function dumps the t table. Each line is of the following format:
//
// c(target_word/source_word) source_word target_word
{
ofstream of(filename);
typename hash_map<wordPairIds, CPPair, hashpair, equal_to<wordPairIds> >::const_iterator i;
for(i = ef.begin(); i != ef.end();++i){
if ( ((*i).second).count > COUNTINCREASE_CUTOFF)
if (actual)
of << ((*i).second).count << ' ' << evlist[ ((*i).first).first ].word << ' ' << fvlist[((*i).first).second].word << ' ' << (*i).second.prob << '\n';
else
of << ((*i).second).count << ' ' << ((*i).first).first << ' ' << ((*i).first).second << ' ' << (*i).second.prob << '\n';
}
}
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::printProbTable(const char *filename,
const Vector<WordEntry>& evlist,
const Vector<WordEntry>& fvlist,
const bool actual) const
// this function dumps the t table. Each line is of the following format:
//
// source_word target_word p(target_word/source_word)
{
ofstream of(filename);
typename hash_map<wordPairIds, CPPair, hashpair, equal_to<wordPairIds> >::const_iterator i;
for(i = ef.begin(); i != ef.end();++i)
if( actual )
of << evlist[((*i).first).first].word << ' ' <<
fvlist[((*i).first).second].word << ' ' << (*i).second.prob << '\n';
else
of << ((*i).first).first << ' ' << ((*i).first).second << ' ' <<
(*i).second.prob << '\n';
}
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::printProbTableInverse(const char *filename,
const Vector<WordEntry>& evlist,
const Vector<WordEntry>& fvlist,
const double,
const double,
const bool actual) const
// this function dumps the inverse t table. Each line is of the format:
//
// target_word_id source_word_id p(source_word/target_word)
//
// if flag "actual " is true then print actual word entries instead of
// token ids
{
cerr << "Dumping the t table inverse to file: " << filename << '\n';
ofstream of(filename);
typename hash_map<wordPairIds, CPPair, hashpair, equal_to<wordPairIds> >::const_iterator i;
PROB p_inv = 0 ;
// static const PROB ratio(double(fTotal)/eTotal);
WordIndex e, f ;
int no_errors(0);
vector<PROB> total(fvlist.size(),PROB(0)) ; // Sum over all e of P(f/e) * p(e) - needed for normalization
for(i = ef.begin(); i != ef.end(); i++){
e = ((*i).first).first ;
f = ((*i).first).second ;
total[f] += (PROB) evlist[e].freq * ((*i).second.prob); //add P(f/ei) * F(ei)
}
for(i = ef.begin(); i != ef.end(); i++){
e = ((*i).first).first ;
f = ((*i).first).second ;
p_inv = ((*i).second.prob) * (PROB) evlist[e].freq / total[f] ;
if (p_inv > 1.0001 || p_inv < 0){
no_errors++;
if (no_errors <= 10){
cerr << "printProbTableInverse(): Error - P("<<evlist[e].word<<"("<<
e<<") / "<<fvlist[f].word << "("<<f<<")) = " << p_inv <<'\n';
cerr << "f(e) = "<<evlist[e].freq << " Sum(p(f/e).f(e)) = " << total[f] <<
" P(f/e) = " <<((*i).second.prob) <<'\n';
if (no_errors == 10)
cerr<<"printProbTableInverse(): Too many P inverse errors ..\n";
}
}
if (actual)
of << fvlist[f].word << ' ' << evlist[e].word << ' ' << p_inv << '\n';
else
of << f << ' ' << e << ' ' << p_inv << '\n';
}
}
/*
{
cerr << "Dumping the t table inverse to file: " << filename << '\n';
ofstream of(filename);
hash_map<wordPairIds, CPPair, hashpair, equal_to<wordPairIds> >::const_iterator i;
PROB p_inv = 0 ;
static const PROB ratio(double(fTotal)/eTotal);
WordIndex e, f ;
for(i = ef.begin(); i != ef.end(); i++){
e = ((*i).first).first ;
f = ((*i).first).second ;
p_inv = ((*i).second.prob) * ratio * (PROB) evlist[e].freq /
(PROB) fvlist[f].freq ;
if (actual)
of << fvlist[f].word << ' ' << evlist[e].word << ' ' << p_inv << '\n';
else
of << f << ' ' << e << ' ' << p_inv << '\n';
}
}
*/
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::normalizeTable(const vcbList&engl, const vcbList&french, int iter)
// normalize conditional probability P(fj/ei):
// i.e. make sure that Sum over all j of P(fj/e) = 1
// this method reads the counts portion of the table and normalize into
// the probability portion. Then the counts are cleared (i.e. zeroed)
// if the resulting probability of an entry is below a threshold, then
// remove it .
{
if( iter==2 )
{
total2.resize(engl.uniqTokens());for(unsigned int i=0;i<total2.size();i++)total2[i]=0.0;
}
nFrench.resize(engl.uniqTokens());for(unsigned int i=0;i<nFrench.size();i++)nFrench[i]=0;
nEng.resize(french.uniqTokens());for(unsigned int i=0;i<nEng.size();i++)nEng[i]=0;
Vector<double> total(engl.uniqTokens(),0.0);
//Vector<int> nFrench(engl.uniqTokens(), 0);
//Vector<int> nEng(french.uniqTokens(), 0);
typename hash_map<wordPairIds, CPPair, hashpair, equal_to<wordPairIds> >::const_iterator i;
for(i = ef.begin(); i != ef.end(); i++){ // for all possible source words e
if( iter==2 )
total2[((*i).first).first] += (*i).second.count;
total[((*i).first).first] += (*i).second.count;
nFrench[((*i).first).first]++;
nEng[((*i).first).second]++;
}
for(unsigned int k=0;k<engl.uniqTokens();++k)
if( nFrench[k] )
{
double probMass=(french.uniqTokensInCorpus()-nFrench[k])*PROB_SMOOTH;
if( probMass<0.0 )
cout << k << " french.uniqTokensInCorpus(): " << french.uniqTokensInCorpus() << " nFrench[k]:"<< nFrench[k] << '\n';
total[k]+= total[k]*probMass/(1-probMass);
}
typename hash_map<wordPairIds, CPPair, hashpair, equal_to<wordPairIds> >::iterator j, k;
PROB p ;
int nParams=0;
for(j = ef.begin(); j != ef.end(); ){
k = j;
k++ ;
if( (total[((*j).first).first])>0.0 )
p = ((((*j).second).count) /(total[((*j).first).first])) ;
else
p= 0.0;
if (p > PROB_CUTOFF)
{
if( iter>0 )
{
((*j).second).prob = 0 ;
((*j).second).count = p ;
}
else
{
((*j).second).prob = p ;
((*j).second).count = 0 ;
}
nParams++;
}
else {
erase(((*j).first).first, ((*j).first).second);
}
j = k ;
}
if( iter>0 )
return normalizeTable(engl, french, iter-1);
else
{
}
}
template <class COUNT, class PROB>
void tmodel<COUNT, PROB>::readProbTable(const char *filename){
/* This function reads the t table from a file.
Each line is of the format: source_word_id target_word_id p(target_word|source_word)
This is the inverse operation of the printTable function.
NAS, 7/11/99
*/
ifstream inf(filename);
cerr << "Reading t prob. table from " << filename << "\n";
if(!inf){
cerr << "\nERROR: Cannot open " << filename << "\n";
return;
}
WordIndex src_id, trg_id;
PROB prob;
int nEntry=0;
while( inf >> src_id >> trg_id >> prob){
insert(src_id, trg_id, 0.0, prob);
nEntry++;
}
cerr << "Read " << nEntry << " entries in prob. table.\n";
}
template class tmodel<COUNT,PROB> ;
/* ---------------- End of Method Definitions of class tmodel ---------------*/
#endif