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

mmsapt_phrase_scorers.h « UG « TranslationModel « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e852b44b55fb9017e60ec463c5137b57ac1d540 (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
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
// -*- c++ -*-
#pragma once
#include "moses/TranslationModel/UG/mm/ug_bitext.h"
#include "util/exception.hh"

namespace Moses {
  namespace bitext
  {

    template<typename Token>
    class
    PhraseScorer
    {
    protected:
      int m_index;
      int m_num_feats;
      vector<string> m_feature_names;
    public:
 
      virtual 
      void 
      operator()(Bitext<Token> const& pt, PhrasePair& pp, vector<float> * dest=NULL) 
	const = 0;
    
      int 
      fcnt() const 
      { return m_num_feats; }
    
      vector<string> const &
      fnames() const
      { return m_feature_names; }

      string const &
      fname(int i) const
      { 
	UTIL_THROW_IF2((i < m_index || i >= m_index + m_num_feats),
		       "Feature name index out of range at " 
		       << __FILE__ << ":" << __LINE__);
	return m_feature_names.at(i - m_index); 
      }
    
      int 
      getIndex() const 
      { return m_index; }
    };
  
    ////////////////////////////////////////////////////////////////////////////////
  
    template<typename Token>
    class
    PScorePfwd : public PhraseScorer<Token>
    {
      float conf;
      char denom;
    public:
      PScorePfwd() 
      {
	this->m_num_feats = 1;
      }

      int 
      init(int const i, float const c, char d) 
      { 
	conf  = c; 
	denom = d;
	this->m_index = i;
	ostringstream buf;
	buf << format("pfwd-%c%.3f") % denom % c;
	this->m_feature_names.push_back(buf.str());
	return i + this->m_num_feats;
      }

      void 
      operator()(Bitext<Token> const& bt, PhrasePair & pp, 
		 vector<float> * dest = NULL) const
      {
	if (!dest) dest = &pp.fvals;
	if (pp.joint > pp.good1) 
	  {
	    cerr<<bt.toString(pp.p1,0)<<" ::: "<<bt.toString(pp.p2,1)<<endl;
	    cerr<<pp.joint<<"/"<<pp.good1<<"/"<<pp.raw2<<endl;
	  }
	switch (denom)
	  {
	  case 'g': 
	    (*dest)[this->m_index] = log(lbop(pp.good1, pp.joint, conf)); 
	    break;
	  case 's': 
	    (*dest)[this->m_index] = log(lbop(pp.sample1, pp.joint, conf)); 
	    break;
	  case 'r':
	    (*dest)[this->m_index] = log(lbop(pp.raw1, pp.joint, conf)); 
	  }
      }
    };
  
    ////////////////////////////////////////////////////////////////////////////////

    template<typename Token>
    class
    PScorePbwd : public PhraseScorer<Token>
    {
      float conf;
      char denom;
    public:
      PScorePbwd() 
      {
	this->m_num_feats = 1;
      }

      int 
      init(int const i, float const c, char d) 
      { 
	conf = c; 
	denom = d;
	this->m_index = i;
	ostringstream buf;
	buf << format("pbwd-%c%.3f") % denom % c;
	this->m_feature_names.push_back(buf.str());
	return i + this->m_num_feats;
      }

      void 
      operator()(Bitext<Token> const& bt, PhrasePair& pp, 
		 vector<float> * dest = NULL) const
      {
	if (!dest) dest = &pp.fvals;
	// we use the denominator specification to scale the raw counts on the 
	// target side; the clean way would be to counter-sample
	uint32_t r2 = pp.raw2;
	if      (denom == 'g') r2 = round(r2 * float(pp.good1)   / pp.raw1);
	else if (denom == 's') r2 = round(r2 * float(pp.sample1) / pp.raw1);
	(*dest)[this->m_index] = log(lbop(max(r2, pp.joint),pp.joint,conf));
      }
    };
  
    ////////////////////////////////////////////////////////////////////////////////

    template<typename Token>
    class
    PScoreCoherence : public PhraseScorer<Token>
    {
    public:
      PScoreCoherence() 
      {
	this->m_num_feats = 1;
      }
    
      int 
      init(int const i) 
      { 
	this->m_index = i;
	this->m_feature_names.push_back(string("coherence"));
	return i + this->m_num_feats;
      }

      void 
      operator()(Bitext<Token> const& bt, PhrasePair& pp, 
		 vector<float> * dest = NULL) const
      {
	if (!dest) dest = &pp.fvals;
	(*dest)[this->m_index] = log(pp.good1) - log(pp.sample1);
      }
    };
  
    ////////////////////////////////////////////////////////////////////////////////

    template<typename Token>
    class
    PScoreLogCounts : public PhraseScorer<Token>
    {
      float conf;
    public:
      PScoreLogCounts() 
      {
	this->m_num_feats = 5;
      }
    
      int 
      init(int const i) 
      { 
	this->m_index = i;
	this->m_feature_names.push_back("log-r1");
	this->m_feature_names.push_back("log-s1");
	this->m_feature_names.push_back("log-g1");
	this->m_feature_names.push_back("log-j");
	this->m_feature_names.push_back("log-r2");
	return i + this->m_num_feats;
      }
    
      void 
      operator()(Bitext<Token> const& bt, PhrasePair& pp, 
		 vector<float> * dest = NULL) const
      {
	if (!dest) dest = &pp.fvals;
	size_t i = this->m_index;
	assert(pp.raw1);
	assert(pp.sample1);
	assert(pp.good1);
	assert(pp.joint);
	assert(pp.raw2);
	(*dest)[i]   = -log(pp.raw1);
	(*dest)[++i] = -log(pp.sample1);
	(*dest)[++i] = -log(pp.good1);
	(*dest)[++i] = +log(pp.joint);
	(*dest)[++i] = -log(pp.raw2);
      }
    };
  
    template<typename Token>
    class
    PScoreLex : public PhraseScorer<Token>
    {
      float const m_alpha;
    public:
      LexicalPhraseScorer2<Token> scorer;
    
      PScoreLex(float const a) 
	: m_alpha(a) 
      { this->m_num_feats = 2; }
    
      int 
      init(int const i, string const& fname) 
      { 
	scorer.open(fname); 
	this->m_index = i;
	this->m_feature_names.push_back("lexfwd");
	this->m_feature_names.push_back("lexbwd");
	return i + this->m_num_feats;
      }
    
      void 
      operator()(Bitext<Token> const& bt, PhrasePair& pp, vector<float> * dest = NULL) const
      {
	if (!dest) dest = &pp.fvals;
	uint32_t sid1=0,sid2=0,off1=0,off2=0,len1=0,len2=0;
	parse_pid(pp.p1, sid1, off1, len1);
	parse_pid(pp.p2, sid2, off2, len2);
	
#if 0
	cout << len1 << " " << len2 << endl;
	Token const* t1 = bt.T1->sntStart(sid1);
	for (size_t i = off1; i < off1 + len1; ++i)
	  cout << (*bt.V1)[t1[i].id()] << " "; 
	cout << __FILE__ << ":" << __LINE__ << endl;
	
	Token const* t2 = bt.T2->sntStart(sid2);
	for (size_t i = off2; i < off2 + len2; ++i)
	  cout << (*bt.V2)[t2[i].id()] << " "; 
	cout << __FILE__ << ":" << __LINE__ << endl;
	
	BOOST_FOREACH (int a, pp.aln)
	  cout << a << " " ;
	cout << __FILE__ << ":" << __LINE__ << "\n" << endl;
	
#endif
	scorer.score(bt.T1->sntStart(sid1)+off1,0,len1,
		     bt.T2->sntStart(sid2)+off2,0,len2,
		     pp.aln, m_alpha,
		     (*dest)[this->m_index],
		     (*dest)[this->m_index+1]);
      }
      
    };
  
    /// Word penalty
    template<typename Token>
    class
    PScoreWP : public PhraseScorer<Token>
    {
    public:
    
      PScoreWP() { this->m_num_feats = 1; }
    
      int 
      init(int const i) 
      {
	this->m_index = i;
	return i + this->m_num_feats;
      }
    
      void 
      operator()(Bitext<Token> const& bt, PhrasePair& pp, vector<float> * dest = NULL) const
      {
	if (!dest) dest = &pp.fvals;
	uint32_t sid2=0,off2=0,len2=0;
	parse_pid(pp.p2, sid2, off2, len2);
	(*dest)[this->m_index] = len2;
      }
    
    };
  
    /// Phrase penalty
    template<typename Token>
    class
    PScorePP : public PhraseScorer<Token>
    {
    public:
    
      PScorePP() { this->m_num_feats = 1; }
    
      int 
      init(int const i) 
      {
	this->m_index = i;
	return i + this->m_num_feats;
      }
    
      void 
      operator()(Bitext<Token> const& bt, PhrasePair& pp, vector<float> * dest = NULL) const
      {
	if (!dest) dest = &pp.fvals;
	(*dest)[this->m_index] = 1;
      }
    
    };
  }
}