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

ug_bitext.cc « mm « UG « TranslationModel « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a1a6dff7bf8d86226d4583a7dd72b0718fc73a2a (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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//-*- c++ -*-

#include "ug_bitext.h"
#include <algorithm>
#include <boost/math/distributions/binomial.hpp>

using namespace ugdiss;
using namespace std;
namespace Moses
{
  namespace bitext 
  {

    ThreadSafeCounter pstats::active;
    
    pstats::
    pstats()
      : raw_cnt     (0)
      , sample_cnt  (0)
      , good        (0)
      , sum_pairs   (0)
      , in_progress (0)
    {
      ofwd[0] = ofwd[1] = ofwd[2] = ofwd[3] = ofwd[4] = ofwd[5] = ofwd[6] = 0;
      obwd[0] = obwd[1] = obwd[2] = obwd[3] = obwd[4] = obwd[5] = obwd[6] = 0;
      // if (++active%5 == 0) 
      // cerr << size_t(active) << " active pstats at " << __FILE__ << ":" << __LINE__ << endl;
    }

    pstats::
    ~pstats()
    {
      --active;
    }

    void
    pstats::
    register_worker()
    {
      this->lock.lock();
      ++this->in_progress;
      this->lock.unlock();
    }
  
    void
    pstats::
    release()
    {
      this->lock.lock();
      if (this->in_progress-- == 1) // last one - >we're done
	this->ready.notify_all();
      this->lock.unlock();
    }

    bool
    pstats::
    add(uint64_t pid, float const w, 
	vector<uchar> const& a, 
	uint32_t const cnt2, 
	uint32_t fwd_o, 
	uint32_t bwd_o)
    {
      boost::lock_guard<boost::mutex> guard(this->lock);
      jstats& entry = this->trg[pid];
      entry.add(w,a,cnt2,fwd_o,bwd_o);
      if (this->good < entry.rcnt())
	{
	  UTIL_THROW(util::Exception, "more joint counts than good counts:" 
		     << entry.rcnt() << "/" << this->good << "!");
	}
      return true;
    }

    jstats::
    jstats()
      : my_rcnt(0), my_wcnt(0), my_cnt2(0)
    { 
      ofwd[0] = ofwd[1] = ofwd[2] = ofwd[3] = ofwd[4] = ofwd[5] = ofwd[6] = 0;
      obwd[0] = obwd[1] = obwd[2] = obwd[3] = obwd[4] = obwd[5] = obwd[6] = 0;
      my_aln.reserve(1); 
    }

    jstats::
    jstats(jstats const& other)
    {
      my_rcnt = other.rcnt();
      my_wcnt = other.wcnt();
      my_aln  = other.aln();
      for (int i = po_first; i <= po_other; i++)
	{
	  ofwd[i] = other.ofwd[i];
	  obwd[i] = other.obwd[i];
	}
    }
  
    uint32_t 
    jstats::
    dcnt_fwd(PhraseOrientation const idx) const
    {
      assert(idx <= po_other);
      return ofwd[idx];
    }

    uint32_t 
    jstats::
    dcnt_bwd(PhraseOrientation const idx) const
    {
      assert(idx <= po_other);
      return obwd[idx];
    }
    
    void 
    jstats::
    add(float w, vector<uchar> const& a, uint32_t const cnt2,
	uint32_t fwd_orient, uint32_t bwd_orient)
    {
      boost::lock_guard<boost::mutex> lk(this->lock);
      my_rcnt += 1;
      my_wcnt += w;
      // my_cnt2 += cnt2; // could I really be that stupid? [UG]
      my_cnt2 = cnt2;
      if (a.size())
	{
	  size_t i = 0;
	  while (i < my_aln.size() && my_aln[i].second != a) ++i;
	  if (i == my_aln.size()) 
	    my_aln.push_back(pair<size_t,vector<uchar> >(1,a));
	  else
	    my_aln[i].first++;
	  if (my_aln[i].first > my_aln[i/2].first)
	    push_heap(my_aln.begin(),my_aln.begin()+i+1);
	}
      ++ofwd[fwd_orient];
      ++obwd[bwd_orient];
    }
    
    uint32_t 
    jstats::
    rcnt() const 
    { return my_rcnt; }
    
    float
    jstats::
    wcnt() const 
    { return my_wcnt; }

    uint32_t
    jstats::
    cnt2() const 
    { return my_cnt2; }
   
    vector<pair<size_t, vector<uchar> > > const&
    jstats::
    aln() const 
    { return my_aln; }

    void 
    jstats::
    invalidate()
    {
      if (my_wcnt > 0) 
	my_wcnt *= -1;
    }

    void 
    jstats::
    validate()
    {
      if (my_wcnt < 0) 
	my_wcnt *= -1;
    }

    bool
    jstats::
    valid()
    {
      return my_wcnt >= 0;
    }

    
    float 
    lbop(size_t const tries, size_t const succ, float const confidence)
    {
      return (confidence == 0 
	      ? float(succ)/tries 
	      : (boost::math::binomial_distribution<>::
		 find_lower_bound_on_p(tries, succ, confidence)));
    }
    
    template<>
    sptr<imBitext<L2R_Token<SimpleWordId> > > 
    imBitext<L2R_Token<SimpleWordId> >::
    add(vector<string> const& s1, 
	vector<string> const& s2, 
	vector<string> const& aln) const
    {
      typedef L2R_Token<SimpleWordId> TKN;
      assert(s1.size() == s2.size() && s1.size() == aln.size());
      
#ifndef NDEBUG
      size_t first_new_snt = this->T1 ? this->T1->size() : 0;
#endif

      sptr<imBitext<TKN> > ret;
      {
	lock_guard<mutex> guard(this->lock);
	ret.reset(new imBitext<TKN>(*this));
      }
      
      // we add the sentences in separate threads (so it's faster)
      boost::thread thread1(snt_adder<TKN>(s1,*ret->V1,ret->myT1,ret->myI1));
      // thread1.join(); // for debugging
      boost::thread thread2(snt_adder<TKN>(s2,*ret->V2,ret->myT2,ret->myI2));
      BOOST_FOREACH(string const& a, aln)
	{
	  istringstream ibuf(a);
	  ostringstream obuf;
	  uint32_t row,col; char c;
	  while (ibuf >> row >> c >> col)
	    {
	      UTIL_THROW_IF2(c != '-', "[" << HERE << "] "
			     << "Error in alignment information:\n" << a);
	      binwrite(obuf,row);
	      binwrite(obuf,col);
	    }
	  // important: DO NOT replace the two lines below this comment by 
	  // char const* x = obuf.str().c_str(), as the memory x is pointing 
	  // to is freed immediately upon deconstruction of the string object.
	  string foo = obuf.str(); 
	  char const* x = foo.c_str();
	  vector<char> v(x,x+foo.size());
	  ret->myTx = append(ret->myTx, v);
	}

      thread1.join();
      thread2.join();

      ret->Tx = ret->myTx;
      ret->T1 = ret->myT1;
      ret->T2 = ret->myT2;
      ret->I1 = ret->myI1;
      ret->I2 = ret->myI2;

#ifndef NDEBUG
      // sanity check
      for (size_t i = first_new_snt; i < ret->T1->size(); ++i)
	{
	  size_t slen1  = ret->T1->sntLen(i);
	  size_t slen2  = ret->T2->sntLen(i);
	  char const* p = ret->Tx->sntStart(i);
	  char const* q = ret->Tx->sntEnd(i);
	  size_t k;
	  while (p < q)
	    {
	      p = binread(p,k);
	      assert(p);
	      assert(p < q);
	      assert(k < slen1);
	      p = binread(p,k);
	      assert(p);
	      assert(k < slen2);
	    }
	}
#endif
      return ret;
    }

    // template<>
    void
    snt_adder<L2R_Token<SimpleWordId> >::
    operator()()
    {
	vector<id_type> sids;
	sids.reserve(snt.size());
	BOOST_FOREACH(string const& foo, snt)
	  {
	    sids.push_back(track ? track->size() : 0);
	    istringstream buf(foo);
	    string w;
	    vector<L2R_Token<SimpleWordId > > s;
	    s.reserve(100);
	    while (buf >> w) 
	      s.push_back(L2R_Token<SimpleWordId>(V[w]));
	    track = append(track,s);
	  }
	if (index)
	  index.reset(new imTSA<L2R_Token<SimpleWordId> >(*index,track,sids,V.tsize()));
	else
	  index.reset(new imTSA<L2R_Token<SimpleWordId> >(track,NULL,NULL));
    }

    snt_adder<L2R_Token<SimpleWordId> >::
    snt_adder(vector<string> const& s, TokenIndex& v, 
     	      sptr<imTtrack<L2R_Token<SimpleWordId> > >& t, 
	      sptr<imTSA<L2R_Token<SimpleWordId> > >& i)
      : snt(s), V(v), track(t), index(i) 
    { }


    bool 
    expand_phrase_pair
    (vector<vector<ushort> >& a1, 
     vector<vector<ushort> >& a2,
     ushort const s2, // next word on in target side
     ushort const L1, ushort const R1, // limits of previous phrase
     ushort & s1, ushort & e1, ushort& e2) // start/end src; end trg
    {
      if (a2[s2].size() == 0) 
	{
	  cout << __FILE__ << ":" << __LINE__ << endl;
	  return false;
	}
      bitvector done1(a1.size());
      bitvector done2(a2.size());
      vector <pair<ushort,ushort> > agenda; 
      // x.first:  side (1 or 2)
      // x.second: word position
      agenda.reserve(a1.size() + a2.size());
      agenda.push_back(pair<ushort,ushort>(2,s2));
      e2 = s2;
      s1 = e1 = a2[s2].front();
      if (s1 >= L1 && s1 < R1) 
	{
	  cout << __FILE__ << ":" << __LINE__ << endl;
	  return false;
	}
      agenda.push_back(pair<ushort,ushort>(2,s2));
      while (agenda.size())
	{
	  ushort side = agenda.back().first;
	  ushort p    = agenda.back().second;
	  agenda.pop_back();
	  if (side == 1)
	    {
	      done1.set(p);
	      BOOST_FOREACH(ushort i, a1[p])
		{
		  if (i < s2) 
		    {
		      // cout << __FILE__ << ":" << __LINE__ << endl;
		      return false;
		    }
		  if (done2[i]) continue;
		  for (;e2 <= i;++e2)
		    if (!done2[e2]) 
		      agenda.push_back(pair<ushort,ushort>(2,e2));
		}
	    }
	  else
	    {
	      done2.set(p);
	      BOOST_FOREACH(ushort i, a2[p])
		{
		  if ((e1 < L1 && i >= L1) || (s1 >= R1 && i < R1) || (i >= L1 && i < R1))
		    {
		      // cout << __FILE__ << ":" << __LINE__ << " " 
		      // << L1 << "-" << R1 << " " << i << " " 
		      // << s1 << "-" << e1<< endl;
		      return false;
		    }
		  
		  if (e1 < i)
		    {
		      for (; e1 <= i; ++e1)
			if (!done1[e1])
			  agenda.push_back(pair<ushort,ushort>(1,e1));
		    }
		  else if (s1 > i)
		    {
		      for (; i <= s1; ++i)
			if (!done1[i])
			  agenda.push_back(pair<ushort,ushort>(1,i));
		    }
		}
	    }
	}
      ++e1;
      ++e2;
      return true;
    }
    //   s1 = seed;
    //   e1 = seed;
    //   s2 = e2 = a1[seed].front();

    //   BOOST_FOREACH(ushort k, a1[seed])
    // 	{
    // 	  if (s2 < k) s2 = k;
    // 	  if (e2 > k) e2 = k;
    // 	}

    //   for (ushort j = s2; j <= e2; ++j)
    // 	{
    // 	  if (a2[j].size() == 0) continue;
    // 	  done2.set(j);
    // 	  agenda.push_back(pair<ushort,ushort>(j,1));
    // 	}

    //   while (agenda.size())
    // 	{
    // 	  ushort side = agenda[0].second;
    // 	  ushort i    = agenda[0].first;
    // 	  agenda.pop_back();
    // 	  if (side)
    // 	    {
    // 	      BOOST_FOREACH(ushort k, a2[i])
    // 		{
    // 		  if (k < L1 || k > R1) 
    // 		    return false;
    // 		  if (done1[k]) 
    // 		    continue;
    // 		  while (s1 > k)
    // 		    {
    // 		      --s1;
    // 		      if (done1[s1] || !a1[s1].size()) 
    // 			continue;
    // 		      done1.set(s1);
    // 		      agenda.push_back(pair<ushort,ushort>(s1,0));
    // 		    }
    // 		  while (e1 < k)
    // 		    {
    // 		      ++e1;
    // 		      if (done1[e1] || !a1[e1].size())
    // 			continue;
    // 		      done1.set(e1);
    // 		      agenda.push_back(pair<ushort,ushort>(e1,0));
    // 		    }
    // 		}
    // 	    }
    // 	  else
    // 	    {
    // 	      BOOST_FOREACH(ushort k, a1[i])
    // 		{
    // 		  if (k < L2 || k > R2) 
    // 		    return false;
    // 		  if (done2[k]) 
    // 		    continue;
    // 		  while (s2 > k)
    // 		    {
    // 		      --s2;
    // 		      if (done2[s2] || !a2[s2].size()) 
    // 			continue;
    // 		      done1.set(s2);
    // 		      agenda.push_back(pair<ushort,ushort>(s2,1));
    // 		    }
    // 		  while (e2 < k)
    // 		    {
    // 		      ++e2;
    // 		      if (done1[e2] || !a1[e2].size())
    // 			continue;
    // 		      done2.set(e2);
    // 		      agenda.push_back(pair<ushort,ushort>(e2,1));
    // 		    }
    // 		}
    // 	    }
    // 	}
    //   ++e1;
    //   ++e2;
    //   return true;
    // }

    void 
    print_amatrix(vector<vector<ushort> > a1, uint32_t len2,
		  ushort b1, ushort e1, ushort b2, ushort e2)
    {
      vector<bitvector> M(a1.size(),bitvector(len2));
      for (ushort j = 0; j < a1.size(); ++j)
	{
	  BOOST_FOREACH(ushort k, a1[j])
	    M[j].set(k);
	}
      cout << b1 << "-" << e1 << " " << b2 << "-" << e2 << endl;
      cout << "   ";
      for (size_t c = 0; c < len2;++c)
	cout << c%10;
      cout << endl;
      for (size_t r = 0; r < M.size(); ++r)
	{
	  cout << setw(3) << r << " ";
	  for (size_t c = 0; c < M[r].size(); ++c)
	    {
	      if ((b1 <= r) && (r < e1) && b2 <= c && c < e2)
		cout << (M[r][c] ? 'x' : '-');
	      else cout << (M[r][c] ? 'o' : '.');
	    }
	  cout << endl;
	}
      cout  << string(90,'-') << endl;
    }

    PhraseOrientation 
    find_po_fwd(vector<vector<ushort> >& a1,
		vector<vector<ushort> >& a2,
		size_t b1, size_t e1,
		size_t b2, size_t e2)
    {
      size_t n2 = e2;
      while (n2 < a2.size() && a2[n2].size() == 0) ++n2;

      if (n2 == a2.size()) 
	return po_last;
      
      ushort ns1,ne1,ne2;
      if (!expand_phrase_pair(a1,a2,n2,b1,e1,ns1,ne1,ne2))
	return po_other;

      if (ns1 >= e1)
	{
	  for (ushort j = e1; j < ns1; ++j)
	    if (a1[j].size()) 
	      return po_jfwd;
	  return po_mono;
	}
      else
	{
	  for (ushort j = ne1; j < b1; ++j)
	    if (a1[j].size()) return po_jbwd;
	  return po_swap;
	}
    }


    PhraseOrientation 
    find_po_bwd(vector<vector<ushort> >& a1,
		vector<vector<ushort> >& a2,
		size_t b1, size_t e1,
		size_t b2, size_t e2)
    {
      int p2 = b2-1;
      while (p2 >= 0 && !a2[p2].size()) --p2;
      if (p2 < 0) return po_first;
      ushort ps1,pe1,pe2;
      if (!expand_phrase_pair(a1,a2,p2,b1,e1,ps1,pe1,pe2))
	return po_other;
      
      if (pe1 < b1)
	{
	  for (ushort j = pe1; j < b1; ++j)
	    if (a1[j].size()) return po_jfwd;
	  return po_mono;
	}
      else
	{
	  for (ushort j = e1; j < ps1; ++j)
	    if (a1[j].size()) return po_jbwd;
	  return po_swap;
	}
    }
  }
}