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

KategProblem.h « mkcls « src « mgizapp - github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5a5a461282193d33f01f1b0f748368632ee32c4 (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
/*

Copyright (C) 1997,1998,1999,2000,2001  Franz Josef Och

mkcls - a program for making word classes .

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 KATEG_OPT_H
#define KATEG_OPT_H
#include <string>

#include <stdlib.h>
#include "Problem.h"

extern double rhoLo; 

typedef int Kategory;        
typedef int Word;



#ifdef FREQTYPE_DOUBLE
typedef double FreqType;
#else
typedef int FreqType;
#endif


#include "KategProblemWBC.h" 


#include "KategProblemKBC.h" 


enum { 
  INIT_RAN=1,
  INIT_AIO=2,
  INIT_LWRW=3,
  INIT_FREQ=4,
  INIT_OTHER=5
     };


enum { 
  W_RAN=(8|16),
  W_DET_DECR=(16),
  W_DET_INCR =(32)
};
#define CHOOSE_WORD (8|16|32)


enum {
  K_DET=(64),
  K_RAN=(128),
  K_BEST=(64|128)
};
#define CHOOSE_KAT (64|128)


enum {
  CRITERION_ML=0,
  CRITERION_LO=1,
  CRITERION_MY=2
};



class NWG
{
 private:
  Array<FreqType> freq;       

  Array<int> timeOfFreq; 
                         
                         
			 

  int curTime;           
 public:
  NWG(int n);            
  void init();           

  int anzNot0;           
                         

  Array<int> not0;       
                         
  int word;          

  inline void addFreq(int C,FreqType n); 

  void sort();           
                         
  FreqType getFreq(int i)     
    {
      if( timeOfFreq[i]==curTime )
	return freq[i];
      else
	return 0;
    };
};

inline void NWG::addFreq(int g,FreqType n)
{
  if(timeOfFreq[g]==curTime)
    freq[g]+=n;
  else
    {
      timeOfFreq[g]=curTime;
      freq[g]=n;
      not0[anzNot0++]=g;
    }
}



struct KategProblemChange : public ProblemChange
{
  void *operator new(size_t size);
  void operator delete(void *ptr,size_t size);

  int word;     
  int toKat;    
  int fromKat;  
};

class KategProblem : public Problem
{
 private:
  double kat_h_full(int n);
  double kat_h_full(double n);
  double kat_h_part(int n);
  double kat_h_part(double n);
  double sigmaVerfaelschung;
  short katWasEmpty;
                    
		    

  int nwgWord;
              
  NWG nwg; 
  NWG ngw; 
  FreqType nww; 

  int ursprung,ziel;    
 
  Array<int> _katOfWord; 

  int _maxComp,_maxCompVal;

   double nmo_my(int i,int j);                            
   double nmo(int i,int j);                            
      

  double nmo_lo(int i,int j,int &e0,int &e1); 
  

   void putWord(int word,int to);
  

   void fastPutWord(int word,int to);
  

   void setKatOfWord(int w,int k)
{
  if( !(wordFreq.fixedWord[w]==k||wordFreq.fixedWord[w]==-1||k==-1) )
    {
      cout << "mkcls::setKatOfWord::ERROR: " << w << " " << k << " " << wordFreq.fixedWord[w] << " " << (*words)[w] << endl;
    }
  _katOfWord[w]=k;
  nwgWord=-1;
};
  

   void fillNWG(int w);
  

   inline FreqType nstrich(int i,int j);
  

   void vnstrich(int i,int j);
  


 protected:
   virtual int _change(ProblemChange **p);
  

   virtual void _doChange(ProblemChange &c);
  

   virtual void _undoChange(ProblemChange &c);
  

   virtual double _value();
  

   double _valueChange(KategProblemChange &k);
  

   virtual void incrementDirection();
  

   virtual int maxDimensionVal(void) ;
  

   virtual int maxDimension(void) ;
  
  
public:
  leda_array<string> *words;
typedef leda_set<int> intSet;

leda_array<intSet> *kats;
	
  KategProblemWBC wordFreq;   
  KategProblemKBC katFreq;    

  Array<int> initLike; 
  
   KategProblem(int aw,int mak,int _initialisierung,int _auswertung,
                           int _nachbarschaft,int minw=0);
  

  virtual ~KategProblem();
  

   virtual void _initialize(int initTyp);
   virtual void _initialize(int initTyp,int specialFixedWord);
  

   virtual double valueChange(ProblemChange&c);
  

   virtual Problem *makeEqualProblem();
  

   virtual double nicevalue(double value=1e100);
  

   void makeKats();
  

   virtual void dumpOn(ostream &strm);
  

   virtual void dumpInfos(ostream &strm);
  

  
  
  
   inline void katwahl(int k);
  

   inline void wortwahl(int w);
  

  
  
  
   inline int katOfWord(int w);
  

   inline short wortwahl();
  

   inline short katwahl() ;
  

   virtual int maxNonBetterIterations();
  

   virtual int expectedNumberOfIterations();
  

   const char *getString(int i);
   string getTheString(int i);
  

 void makeTitle(char x[512]);


 void fixInitLike();

};

inline int KategProblem::katOfWord(int w){return _katOfWord[w];};
inline short KategProblem::wortwahl(){return nachbarschaft&CHOOSE_WORD;};
inline short KategProblem::katwahl() {return nachbarschaft&CHOOSE_KAT;};

inline void KategProblem::katwahl(int k) 
    { 
      nachbarschaft = (nachbarschaft&(~CHOOSE_KAT)) | k; 
      if(k==K_BEST)
	_maxCompVal=1;
      else
	_maxCompVal=katFreq.nKats-2;
    };

inline void KategProblem::wortwahl(int w) 
    { 
      nachbarschaft = (nachbarschaft&(~CHOOSE_WORD)) | w;
    };



inline FreqType KategProblem::nstrich(int i,int j)
{
  FreqType n=0;

  if( i==ursprung )
    n-=nwg.getFreq(j); 
  if( i==ziel )
    n+=nwg.getFreq(j);

  if( j==ursprung )
    n-=ngw.getFreq(i);
  if( j==ziel )
    n+=ngw.getFreq(i);

  if( i==ursprung && j==ursprung )
    n+=nww;
  if( i==ziel && j==ziel )
    n+=nww;

  if( i==ursprung && j==ziel )
    n-=nww;
  if( i==ziel && j==ursprung )
    n-=nww;
  
  return n;
}





#define MAX_H_TABLE 4000 
extern double h_table[],l_table[],hmy_table[],hmy_sigma;

 
inline double kat_mlog(double x)
{
  if(x<=1e-9)
    return 0;
  else
    return log(x);
}

 
inline double kat_mlog(int s)
{
  if(s<=0)
    return 0;
  else if( s<MAX_H_TABLE )
    {
      massert( s==0 || l_table[s]==log(s) );
      return l_table[s];
    }
  else 
    return log((double)(s));
}


 
inline double kat_hlo(int n)
{
  return n*kat_mlog(n-1);
}
 
inline double kat_hlo(double n)
{
  return n*kat_mlog(n-1);
}

 
inline double kat_h(int n)
{
  massert(n>=-1);
  if(n<=0)
    return 0;
  else
    if(n<MAX_H_TABLE)
      {
	massert(n==0||fabs(h_table[n]-n*log((double)n))<1e-8);
	return h_table[n];
      }
    else
      return n*log((double)(n));
}
inline double kat_h(double n)
{
  if(n<=1e-9)
    return 0;
  else
    return n*log(n);
}

 
inline double kat_etaFkt(int _e0,int e1,int immer0,int cats)
{
  int e0    = _e0 - immer0;
  int ePlus = cats*cats - _e0;
  if(cats*cats-e0>1)
    return e1*log( (ePlus-1.0)/(e0+1.0)*rhoLo );
  else
    return 0;
}

double mkat_h_full(int n,double tf);
double mkat_h_part(int n,double cf);

int Hash(const string& s);


#endif