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

NTables.h « src « v0.6.4 - github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 90d1b3d680a8c02408f3675998ac0e66f7605083 (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
/*

 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.

 */
#ifndef _ntables_h
#define _ntables_h 1
#include "Array2.h"
#include "Vector.h"
#include <assert.h>
#include "defs.h"
#include "vocab.h"
#include "myassert.h"
#include "Globals.h"
#include "syncObj.h"

extern double NTablesFactorGraphemes, NTablesFactorGeneral;

template<class VALTYPE> class nmodel {
private:
	Array2<VALTYPE, Vector<VALTYPE> > ntab;
public:
	nmodel(int maxw, int maxn) :
		ntab(maxw, maxn, 0.0) {
	}
	VALTYPE getValue(int w, unsigned int n) const {
		massert(w!=0);
		if (n>=ntab.getLen2())
			return 0.0;
		else
			return max(ntab(w, n), VALTYPE(PROB_SMOOTH));
	}
protected:
	inline VALTYPE&getRef(int w, int n) {
		//massert(w!=0);
		return ntab(w, n);
	};
	Mutex lock;
public:
	inline void addValue(int w , int n,const VALTYPE& t){lock.lock();ntab(w,n)+=t;lock.unlock();};
public:
	template<class COUNT> void normalize(nmodel<COUNT>&write,
			const Vector<WordEntry>* _evlist) const {
		int h1=ntab.getLen1(), h2=ntab.getLen2();
		int nParams=0;
		if (_evlist&&(NTablesFactorGraphemes||NTablesFactorGeneral)) {
			size_t maxlen=0;
			const Vector<WordEntry>&evlist=*_evlist;
			for (unsigned int i=1; i<evlist.size(); i++)
				maxlen=max(maxlen, evlist[i].word.length());
			Array2<COUNT,Vector<COUNT> > counts(maxlen+1, MAX_FERTILITY+1, 0.0);
			Vector<COUNT> nprob_general(MAX_FERTILITY+1,0.0);
			for (unsigned int i=1; i<min((unsigned int)h1,
					(unsigned int)evlist.size()); i++) {
				int l=evlist[i].word.length();
				for (int k=0; k<h2; k++) {
					counts(l, k)+=getValue(i, k);
					nprob_general[k]+=getValue(i, k);
				}
			}
			COUNT sum2=0;
			for (unsigned int i=1; i<maxlen+1; i++) {
				COUNT sum=0.0;
				for (int k=0; k<h2; k++)
					sum+=counts(i, k);
				sum2+=sum;
				if (sum) {
					double average=0.0;
					//cerr << "l: " << i << " " << sum << " ";
					for (int k=0; k<h2; k++) {
						counts(i, k)/=sum;
						//cerr << counts(i,k) << ' ';
						average+=k*counts(i, k);
					}
					//cerr << "avg: " << average << endl;
					//cerr << '\n';
				}
			}
			for (unsigned int k=0; k<nprob_general.size(); k++)
				nprob_general[k]/=sum2;

			for (int i=1; i<h1; i++) {
				int l=-1;
				if ((unsigned int)i<evlist.size())
					l=evlist[i].word.length();
				COUNT sum=0.0;
				for (int k=0; k<h2; k++)
					sum+=getValue(i, k)+((l==-1) ? 0.0 : (counts(l, k)
							*NTablesFactorGraphemes)) + NTablesFactorGeneral
							*nprob_general[k];
				assert(sum);
				for (int k=0; k<h2; k++) {
					write.getRef(i, k)=(getValue(i, k)+((l==-1) ? 0.0
							: (counts(l, k)*NTablesFactorGraphemes)))/sum
							+ NTablesFactorGeneral*nprob_general[k];
					nParams++;
				}
			}
		} else
			for (int i=1; i<h1; i++) {
				COUNT sum=0.0;
				for (int k=0; k<h2; k++)
					sum+=getValue(i, k);
				assert(sum);
				for (int k=0; k<h2; k++) {
					write.getRef(i, k)=getValue(i, k)/sum;
					nParams++;
				}
			}
		cerr << "NTable contains " << nParams << " parameter.\n";
	}

	bool merge(nmodel<VALTYPE>& n, int noEW, const Vector<WordEntry>& evlist);
	void clear() {
		int h1=ntab.getLen1(), h2=ntab.getLen2();
		for (int i=0; i<h1; i++)
			for (int k=0; k<h2; k++)
				ntab(i, k)=0;
	}
	void printNTable(int noEW, const char* filename,
			const Vector<WordEntry>& evlist, bool) const;
	void printRealNTable(int noEW, const char* filename,
			const Vector<WordEntry>& evlist, bool) const;
	bool readAugNTable(const char *filename);
	bool readNTable(const char *filename);

};

#endif