Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NTables.cpp « src « mgizapp - github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5676d55db8acbdb74cf7a68d3d1fba93b0fe188 (plain)
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
/*

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 "NTables.h"
#include <iostream>
#include "defs.h"
#include <fstream>
#include "Parameter.h"

GLOBAL_PARAMETER(double,NTablesFactorGraphemes,"nSmooth","smoothing for fertility parameters (good value: 64): weight for wordlength-dependent fertility parameters",PARLEV_SMOOTH,64.0);
GLOBAL_PARAMETER(double,NTablesFactorGeneral,"nSmoothGeneral","smoothing for fertility parameters (default: 0): weight for word-independent fertility parameters",PARLEV_SMOOTH,0.0);

template <class VALTYPE>
void nmodel<VALTYPE>::printNTable(int noEW, const char* filename, 
				  const Vector<WordEntry>& evlist, 
				  bool actual) const
     // prints the fertility table but with actual sourcce words (not their id)
{
    cerr << "Dumping nTable to: " << filename <<  '\n';  
    ofstream of(filename);
    VALTYPE p ;
    WordIndex k, i ;
    for(i=1; int(i) < noEW; i++){
        if (evlist[i].freq > 0){
            if (actual)
                of << evlist[i].word << ' ' ;
            else 
                of << i << ' ' ;
            for( k=0; k < MAX_FERTILITY; k++){
                p = getValue(i, k);
                if (p <= PROB_SMOOTH) 
                    p = 0;
                of << p << ' ';      
            } 
            of << '\n';
        }
    }
}

template <class VALTYPE>
void nmodel<VALTYPE>::printRealNTable(int noEW, const char* filename, 
				  const Vector<WordEntry>& evlist, 
				  bool actual) const
     // prints the fertility table but with actual sourcce words (not their id)
{
    cerr << "Dumping nTable to: " << filename <<  '\n';  
    ofstream of(filename);
    VALTYPE p ;
    WordIndex k, i ;
    for(i=1; int(i) < noEW; i++){
        if (evlist[i].freq > 0){
            if (actual)
                of << evlist[i].word << ' ' ;
            else 
                of << i << ' ' ;
            for( k=0; k < MAX_FERTILITY; k++){
                p = getValue(i, k);
//                if (p <= PROB_SMOOTH) 
//                    p = 0;
                of << p << ' ';      
            } 
            of << '\n';
        }
    }
}

template <class VALTYPE>
bool nmodel<VALTYPE>::readNTable(const char *filename){
  /* This function reads the n table from a file.
     Each line is of the format:  source_word_id p0 p1 p2 ... pn
     This is the inverse operation of the printTable function.
     NAS, 7/11/99
  */
    ifstream inf(filename);
    if(!inf.is_open()){
        return false;
    }
    cerr << "Reading fertility table from " << filename << "\n";
    if(!inf){
        cerr << "\nERROR: Cannot open " << filename <<"\n";
        return false;
    }
    
    VALTYPE prob;
    WordIndex tok, i;
    int nFert=0;
    while(!inf.eof()){
        nFert++;
        inf >> ws >> tok;
        if (tok > MAX_VOCAB_SIZE){
            cerr << "NTables:readNTable(): unrecognized token id: " << tok
                <<'\n';
            exit(-1);
        }
        for(i = 0; i < MAX_FERTILITY; i++){
            inf >> ws >> prob;
            getRef(tok, i)=prob;
        }
    }
    cerr << "Read " << nFert << " entries in fertility table.\n";
    inf.close();
    return true;
}

template <class VALTYPE>
bool nmodel<VALTYPE>::merge(nmodel<VALTYPE>& n,int noEW, const Vector<WordEntry>& evlist){
  /* This function reads the n table from a file.
     Each line is of the format:  source_word_id p0 p1 p2 ... pn
     This is the inverse operation of the printTable function.
     NAS, 7/11/99
  */

    
    VALTYPE p ;
    WordIndex k, i ;
    for(i=1; int(i) < noEW; i++){
        if (evlist[i].freq > 0){
            for( k=0; k < MAX_FERTILITY; k++){
                p = n.getValue(i, k);
                getRef(i,k)+=p;
            } 
        }
    }
    return true;
}

template <class VALTYPE>
bool nmodel<VALTYPE>::readAugNTable(const char *filename){
  /* This function reads the n table from a file.
     Each line is of the format:  source_word_id p0 p1 p2 ... pn
     This is the inverse operation of the printTable function.
     NAS, 7/11/99
  */
    ifstream inf(filename);
    if(!inf.is_open()){
        return false;
    }
    cerr << "Reading fertility table from " << filename << "\n";
    if(!inf){
        cerr << "\nERROR: Cannot open " << filename <<"\n";
        return false;
    }
    
    VALTYPE prob;
    WordIndex tok, i;
    int nFert=0;
    while(!inf.eof()){
        nFert++;
        inf >> ws >> tok;
        if (tok > MAX_VOCAB_SIZE){
            cerr << "NTables:readNTable(): unrecognized token id: " << tok
                <<'\n';
            exit(-1);
        }
        for(i = 0; i < MAX_FERTILITY; i++){
            inf >> ws >> prob;
            getRef(tok, i)+=prob;
        }
    }
    cerr << "Read " << nFert << " entries in fertility table.\n";
    inf.close();
    return true;
}

template class nmodel<COUNT>;
//template class nmodel<PROB>;