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

getSentence.cpp « src « MGIZA « dual-model « experimental - github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 615a0b40bab3eadc3bcce429b80b6e93c0bf9dbd (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
/*

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.

*/
/* --------------------------------------------------------------------------*
 *                                                                           *
 * Module : getSentece                                                       *
 *                                                                           *
 * Method Definitions File: getSentence.cc                                   *
 *                                                                           *
 * Objective: Defines clases and methods for handling I/O for the parallel   *
 *            corpus.                                                        *
 *****************************************************************************/


#include "getSentence.h"
#include <iostream>
#include <strstream>
#include <cstdio>
#include <cstdlib>
#include <boost/algorithm/string.hpp>
#include <vector>
#include <set>
#include <pthread.h>
#include "Parameter.h"
#include "errno.h"

int PrintedTooLong=0;

/* -------------- Method Defnitions for Class sentenceHandler ---------------*/

GLOBAL_PARAMETER(double,ManlexMAX_MULTIPLICITY,"manlexMAX_MULTIPLICITY","",PARLEV_EM,20.0);
GLOBAL_PARAMETER(double,Manlexfactor1,"manlexfactor1","",PARLEV_EM,0.0);
GLOBAL_PARAMETER(double,Manlexfactor2,"manlexfactor2","",PARLEV_EM,0.0);

sentenceHandler::sentenceHandler(const char*  filename, vcbList* elist, 
				 vcbList*  flist) : realCount(0)
  // This method is the constructor of the class, it also intitializes the 
  // sentence pair sequential number (count) to zero.
{
    pthread_mutex_init(&readsent_mutex,NULL);
    pthread_mutex_init(&setprob_mutex,NULL);

    position = 0;
    readflag = false ;
    allInMemory = false ;
    inputFilename = filename ;
    inputFile = new ifstream(filename);
    pair_no = 0 ;
    if(!(*inputFile)){
        cerr << "\nERROR:(a) Cannot open " << filename;
        exit(1);
    }
    currentSentence = 0;
    totalPairs1 = 0 ;
    totalPairs2 =0;
    pair_no = 0 ;
    noSentInBuffer = 0 ;
    Buffer.clear();
    bool isNegative=0;
	std::set<WordIndex> evoc,fvoc;
	evoc.insert(0);
	fvoc.insert(0);
    if (elist && flist){
        cout << "Calculating vocabulary frequencies from corpus " << filename << '\n';
        sentPair s ;
        while (getNextSentence(s, elist, flist))
        {
			for(int i = 0 ; i< s.eSent.size() ; i++){
				evoc.insert(s.eSent[i]);
			}
			for(int i = 0 ; i< s.fSent.size() ; i++){
				fvoc.insert(s.fSent[i]);
			}			
            totalPairs1++;
            totalPairs2+=s.realCount; 
            // NOTE: this value might change during training 
            // for words from the manual dictionary, yet this is ignored!
            
            if( s.noOcc<0 )
                isNegative=1;
        }
    }
    if( isNegative==1 )
    {
        cerr << "WARNING: corpus contains negative occurrency frequencies => these are interpreted as entries of a manual dictionary.\n";
        realCount=new Vector<double>(totalPairs1,1.0);
    }
    else
        realCount=0;
	elist->compact(evoc);
	flist->compact(fvoc);
}

sentenceHandler::sentenceHandler(const char*  filename, vcbList* elist,
		vcbList*  flist,std::set<WordIndex>& eapp, std::set<WordIndex>& fapp) : realCount(0)
				   // This method is the constructor of the class, it also intitializes the 
				   // sentence pair sequential number (count) to z
{ 
    pthread_mutex_init(&readsent_mutex,NULL);
    pthread_mutex_init(&setprob_mutex,NULL);
    position = 0;
	readflag = false ;
	allInMemory = false ;
	inputFilename = filename ;
	inputFile = new ifstream(filename);
	pair_no = 0 ;
	if(!(*inputFile)){
		cerr << "\nERROR:(a) Cannot open " << filename;
		exit(1);
	}
	currentSentence = 0;
	totalPairs1 = 0 ;
	totalPairs2 =0;
	pair_no = 0 ;
	noSentInBuffer = 0 ;
	Buffer.clear();
	bool isNegative=0;
	if (elist && flist){
		cout << "Calculating vocabulary frequencies from corpus " << filename << '\n';
		sentPair s ;
		while (getNextSentence(s, elist, flist))
		{ 
			int k;
			for(k=0;k<s.eSent.size();k++){
				eapp.insert(s.eSent[k]);
			}
			for(k=0;k<s.fSent.size();k++){
				fapp.insert(s.fSent[k]);
			}
			totalPairs1++;
			totalPairs2+=s.realCount; 
			// NOTE: this value might change during training 
			// for words from the manual dictionary, yet this is ignored!
			
			if( s.noOcc<0 )
				isNegative=1;
		}
	}
	if( isNegative==1 )
	{ 
		cerr << "WARNING: corpus contains negative occurrency frequencies => these are interpreted as entries of a manual dictionary.\n";   
		realCount=new Vector<double>(totalPairs1,1.0);
	}
	else
		realCount=0;
}

void sentenceHandler::rewind()
{
    position = 0;
    currentSentence = 0;
    readflag = false ;
    if (!allInMemory || 
        !(Buffer.size() >= 1 && Buffer[currentSentence].sentenceNo == 1)){
            // check if the buffer doe not already has the first chunk of pairs 
            if (Buffer.size() > 0)
                cerr << ' ' <<  Buffer[currentSentence].sentenceNo << '\n';
            //    totalPairs = 0 ;
            pair_no = 0 ;
            noSentInBuffer = 0 ;
            Buffer.clear();
        }
    if (!allInMemory){
        delete inputFile;
        inputFile = new ifstream(inputFilename);
        if(!(*inputFile)){
            cerr << "\nERROR:(b) Cannot open " << inputFilename << " " << (int)errno;
        }
    }
}

  
int sentenceHandler::getNextSentence(sentPair& sent, vcbList* elist, vcbList* flist)
{
    pthread_mutex_lock(&readsent_mutex);
    
    do{
        sentPair s ;
        if (readflag){
            cerr << "Attempting to read from the end of corpus, rewinding\n";
            //rewind();
            break;
        } 
        if (currentSentence >= noSentInBuffer){
            if (allInMemory)
                break;
            /* no more sentences in buffer */
            noSentInBuffer = 0 ;
            currentSentence = 0 ;
            Buffer.clear();
            cout << "Reading more sentence pairs into memory ... \n";
            while((noSentInBuffer < TRAIN_BUFFER_SIZE) && readNextSentence(s)){
                if ((s.fSent.size()-1) > (MAX_FERTILITY-1) * (s.eSent.size()-1)){
                    cerr << "WARNING: The following sentence pair has source/target sentence length ratio more than\n"<<
                        "the maximum allowed limit for a source word fertility\n"<<
                        " source length = " << s.eSent.size()-1 << " target length = " << s.fSent.size()-1 <<
                        " ratio " << double(s.fSent.size()-1)/  (s.eSent.size()-1) << " ferility limit : " <<
                        MAX_FERTILITY-1 << '\n';
                    cerr << "Shortening sentence \n";
                    cerr << s;
                    s.eSent.resize(min(s.eSent.size(),s.fSent.size()));
                    s.fSent.resize(min(s.eSent.size(),s.fSent.size()));
                }
                Buffer.push_back(s) ;
                //cerr << s.eAnchor.size() << " " << Buffer[Buffer.size()-1].eAnchor.size()<< endl;
                if (elist && flist){
                    if ((*elist).size() > 0)
                        for (WordIndex i= 0 ; i < s.eSent.size() ; i++){
                            if (s.eSent[i] >= (*elist).uniqTokens()){
                                if( PrintedTooLong++<100)
                                    cerr << "ERROR: source word " << s.eSent[i] << " is not in the vocabulary list \n";
                                exit(-1);
                            }
                            (*elist).incFreq(s.eSent[i], s.realCount);
                        }
                    if ((*flist).size() > 0)
                        for (WordIndex j= 1 ; j < s.fSent.size() ; j++){
                            if (s.fSent[j] >= (*flist).uniqTokens()){
                                cerr << "ERROR: target word " << s.fSent[j] << " is not in the vocabulary list \n";
                                exit(-1);
                            }
                            (*flist).incFreq(s.fSent[j], s.realCount);
                        }
                }
                noSentInBuffer++;
            }
            if (inputFile->eof()){
                allInMemory = (Buffer.size() >= 1 && 
                               Buffer[currentSentence].sentenceNo == 1) ;
                if (allInMemory)
                    cout << "Corpus fits in memory, corpus has: " << Buffer.size() <<
                    " sentence pairs.\n";
            }
        }
        if(noSentInBuffer <= 0 ){
            //cerr << "# sent in buffer " << noSentInBuffer << '\n';
            readflag = true ;
            break;
        }
        sent = Buffer[currentSentence++] ;
       // cerr << currentSentence-1 << " " << sent.eAnchor.size() << " " << Buffer[currentSentence-1].eAnchor.size()<< endl;
        position ++;
        if( sent.noOcc<0 && realCount ){
            if( Manlexfactor1 && sent.noOcc==-1.0 )
                sent.realCount=Manlexfactor1;
            else if( Manlexfactor2 && sent.noOcc==-2.0 )
                sent.realCount=Manlexfactor2;
            else
                sent.realCount=(*realCount)[sent.getSentenceNo()-1];
        }
        pthread_mutex_unlock(&readsent_mutex);
        return position ;
    }while(false);
    pthread_mutex_unlock(&readsent_mutex);
    return 0;
}
bool sentenceHandler::readNextSentence(sentPair& sent)
  /* This method reads in a new pair of sentences, each pair is read from the 
     corpus file as line triples. The first line the no of times this line 
     pair occured in the corpus, the second line is the source sentence and 
     the third is the target sentence. The sentences are represented by a space
     separated positive integer token ids. */
{

  string line;
  bool fail(false) ;
  
  sent.clear();
  vector<string> splits;
  if (getline(*inputFile, line)){

	  boost::algorithm::split(splits,line,boost::algorithm::is_any_of("|#*"));

	  if(splits.size() == 1 || splits.size() == 0){
		  // continue, no problem
		  
	  }else if(splits.size()>=3){
		  line = splits[0];
	  }else{
		  fail = true;
		  return false;
	  }
	  
    istrstream buffer(line.c_str());
    buffer >> sent.noOcc;
    if( sent.noOcc<0 )
      {
	if( realCount )
	  {
	    if( Manlexfactor1 && sent.noOcc==-1.0 )
	      sent.realCount=Manlexfactor1;
	    else if( Manlexfactor2 && sent.noOcc==-2.0 )
	      sent.realCount=Manlexfactor2;
	    else
	      {
		sent.realCount=(*realCount)[pair_no];
	      }
	  }
	else
	  sent.realCount=1.0;
      }
    else
      sent.realCount=sent.noOcc;
  }
  else {
    fail = true ;;
  }
  if (splits.size()>=3 || getline(*inputFile, line)){
	  if(splits.size()>=3){
		  line = splits[1];
	  }
    istrstream buffer(line.c_str());
    WordIndex w;  // w is a local variabe for token id
    sent.eSent.push_back(0); // each source word is assumed to have 0 == 
    // a null word (id 0) at the begining of the sentence. 
    while(buffer>>w){ // read source sentece , word by word .
      if (sent.eSent.size() < MAX_SENTENCE_LENGTH)
	sent.eSent.push_back(w);
      else {
	if( PrintedTooLong++<100)
	  cerr << "{WARNING:(a)truncated sentence "<<pair_no<<"}";
	//cerr << "ERROR: getSentence.cc:getNextSentence(): sentence exceeds preset length limit of : " << MAX_SENTENCE_LENGTH << '\n';
	//cerr << "The following sentence will be truncated\n" << line;
	break ;
      }
    }
  }
  else {
    fail = true ;
  }
  if (splits.size()>=3 ||getline(*inputFile, line)){
	  if(splits.size()>=3){
		  line = splits[2];
	  }
    istrstream buffer(line.c_str());
    WordIndex w;  // w is a local variabe for token id
    sent.fSent.push_back(0); //0 is inserted for program uniformity
    while(buffer>>w){ // read target sentece , word by word .
      if (sent.fSent.size() < MAX_SENTENCE_LENGTH)
	sent.fSent.push_back(w);
      else {
	if( PrintedTooLong++<100)
	  cerr << "{WARNING:(b)truncated sentence "<<pair_no<<"}";
	//cerr << "ERROR: getSentence.cc:getNextSentence(): sentence exceeds preset length limit of : " << MAX_SENTENCE_LENGTH << '\n';
	//cerr << "The following sentence will be truncated\n" << line;
	break ;
      }
    }
  }
  else {
    fail = true ;
  }
  sent.eAnchor.clear();
  sent.fAnchor.clear();
//  cerr << "Splits: " << splits.size() << endl;
  if(splits.size()>3){
	  vector<string> al,eal;
	  al.clear();
	  boost::algorithm::split(al,splits[3],boost::algorithm::is_any_of(" "));
	  for(int w = 0 ; w < al.size(); w++){
		  eal.clear();
	  	  boost::algorithm::split(eal,al[w],boost::algorithm::is_any_of("-"));
		  if(eal.size()==2){
			  int ea = atoi(eal[0].c_str());
			  int fa = atoi(eal[1].c_str());
			  if(ea >= sent.eSent.size() || fa >= sent.fSent.size())
				  continue;
			  sent.eAnchor.push_back(ea);
			  sent.fAnchor.push_back(fa);
		  }
	  }
	//  cerr << "Read partial alignment: " << sent.eAnchor.size() << " " <<
	//	  sent.fAnchor.size() << "\n";
  }
  if (fail){
    sent.eSent.clear();
    sent.fSent.clear();
	sent.eAnchor.clear();
    sent.fAnchor.clear();	  
    sent.sentenceNo = 0 ;
    sent.noOcc = 0 ;
    sent.realCount=0;
    return(false);
  }  
  if( sent.eSent.size()==1||sent.fSent.size()==1 )
    cerr << "ERROR: Forbidden zero sentence length " << sent.sentenceNo << endl;
  sent.sentenceNo = ++pair_no;
  if(pair_no % 100000 == 0) 
    cout << "[sent:" << sent.sentenceNo  << "]"<< '\n';
  return true;
}

double optimize_lambda(Vector<double>&vd)
{
  Vector<double> l;
  for(double lambda=1.0;lambda<ManlexMAX_MULTIPLICITY;lambda+=0.33)
    {
      double prod=0.0;
      for(unsigned int i=0;i<vd.size();++i)
	{
	  prod += vd[i]*exp(lambda*vd[i])/(exp(lambda*vd[i])-1.0);
	}
      l.push_back(fabs(prod-1.0));
    }
  double lam=double(min_element(l.begin(),l.end())-l.begin())*0.33+1.0;
  if( lam<1.0 )
    {
      cerr << "ERROR: lambda is smaller than one: " << lam << endl;
      for(unsigned int i=0;i<vd.size();++i)
	cerr << vd[i] << ' ';
      cerr << endl;
    }
  return lam;
}

void sentenceHandler::setProbOfSentence(const sentPair&s,double d)
{
    
    if( realCount==0 )
        return;
    else{
        pthread_mutex_lock(&setprob_mutex);
        if( s.noOcc<=0 )
        {
            double ed=exp(d);
            if( oldPairs.size()>0&&(oldPairs.back().get_eSent()!=s.get_eSent()||oldPairs.back().getSentenceNo()>=s.getSentenceNo()) )
            {
                double lambda=optimize_lambda(oldProbs);
                for(unsigned int i=0;i<oldPairs.size();++i)
                {
                    if( oldProbs[i]<1e-5 )
                        (*realCount)[oldPairs[i].getSentenceNo()-1]=1.0;
                    else
                        (*realCount)[oldPairs[i].getSentenceNo()-1]=lambda*oldProbs[i]/(1-exp(-lambda*oldProbs[i]));		    
                }
                oldPairs.clear();
                oldProbs.clear();
            }
            oldPairs.push_back(s);
            oldProbs.push_back(ed);
        }
        pthread_mutex_unlock(&setprob_mutex);
    }
}

/* ------------- End of Method Definition of Class sentenceHandler ----------*/