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

nl-racpt.h « include « rvtl « hhmm « synlm « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5d1502f1fddebc77f543a8a25955c713f7d80039 (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
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. //
//                                                                           //
//    ModelBlocks 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 3 of the License, or      //
//    (at your option) any later version.                                    //
//                                                                           //
//    ModelBlocks 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 ModelBlocks.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//    ModelBlocks developers designate this particular file as subject to    //
//    the "Moses" exception as provided by ModelBlocks developers in         //
//    the LICENSE file that accompanies this code.                           //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////


#ifndef _NL_RACPT__
#define _NL_RACPT__


template<class K, class P>
class GenericRACPTModel : public SimpleHash<K,P> {
 private:
  typedef SimpleHash<K,P> HKP;
//  typedef typename SimpleHash<Y,P>::const_iterator IYP;
  //HKYP h;

 public:
  //typedef Y RVType;
  //typedef BaseIterVal<std::pair<IYP,IYP>,Y> IterVal;
  //typedef typename HKYP::const_iterator const_key_iterator;

  bool contains ( const K& k ) const {
    return ( SimpleHash<K,P>::contains(k) );
  }

/*  
  P getProb ( const IterVal& ikyp, const K& k ) const {
    if ( ikyp.iter.first == ikyp.iter.second ) { cerr<<"ERROR: no iterator to fix probability: "<<k<<endl; return P(); }
    return ( ikyp.iter.first->second );
  }
*/

  P getProb ( const K& k ) const {
    return SimpleHash<K,P>::get(k);
  }
  P& setProb ( const K& k ) {
    return SimpleHash<K,P>::set(k);
  }

/*
  void normalize ( ) {
    for ( typename HKYP::const_iterator ik=HKYP::begin(); ik!=HKYP::end(); ik++ ) {
      K k=ik->first;
      P p=P();
      IterVal y;
      for(bool by=setFirst(y,k); by; by=setNext(y,k))
        p+=getProb(y,k);
      if (p!=P())
        for(bool by=setFirst(y,k); by; by=setNext(y,k))
          setProb(y,k)/=p;
    }
  }
*/
/*
   void transmit ( int tSockfd, const char* psId ) const {
    for ( typename HKYP::const_iterator ik=HKYP::begin(); ik!=HKYP::end(); ik++ ) {
      K k=ik->first;
      IterVal y;
      // For each non-zero probability in model...
      for ( bool b=setFirst(y,k); b; b=setNext(y,k) ) {
        //if ( getProb(y,k) != P() ) {
          String str(1000);
          str<<psId<<" "<<k<<" : "<<y<<" = "<<getProb(y,k).toDouble()<<"\n";
          if ( send(tSockfd,str.c_array(),str.size()-1,MSG_EOR) != int(str.size()-1) )
            {cerr<<"ERROR writing to socket\n";exit(0);}
        //}
      }
    }
  }
*/
  void dump ( ostream& os, const char* psId ) const {
    for ( typename HKP::const_iterator ik=HKP::begin(); ik!=HKP::end(); ik++ ) {
      K k=ik->first;
      os << psId<<" "<<k<<" = "<<getProb(k).toDouble()<<endl;
    
//      IterVal y;
//      for ( bool b=setFirst(y,k); b; b=setNext(y,k) )
//        os<<psId<<" "<<k<<" : "<<y<<" = "<<getProb(y,k).toDouble()<<"\n";
    }
  }
  void subsume ( GenericRACPTModel<K,P>& m ) {
    for ( typename HKP::const_iterator ik=m.HKP::begin(); ik!=m.HKP::end(); ik++ ) {
      K k=ik->first;
      setProb(k) = m.getProb(k);
//      IterVal y;
//      for ( bool b=m.setFirst(y,k); b; b=m.setNext(y,k) )
//        setProb(y,k) = m.getProb(y,k);
    }
  }
  void clear ( ) { SimpleHash<K,P>::clear(); }

  friend pair<StringInput,GenericRACPTModel<K,P>*> operator>> ( StringInput si, GenericRACPTModel<K,P>& m ) {
    return pair<StringInput,GenericRACPTModel<K,P>*>(si,&m); }
    
  friend StringInput operator>> ( pair<StringInput,GenericRACPTModel<K,P>*> delimbuff, const char* psD ) {
    K k; 
    StringInput si,si2,si3; 
    GenericRACPTModel<K,P>& m = *delimbuff.second;
    si=delimbuff.first;
    if ( si==NULL ) return si;
    
    // Kill the colon since we're treating the whole thing as the condition
    char * str = si.c_str();
    char * p = strchr(str, ':');
    if(p){
      p[0] = ' ';
    }
    si=str;
    while((si2=si>>" ")!=NULL)si=si2; 
    si=si>>k>>" ";
    while((si2=si>>" ")!=NULL)si=si2; 
    si=si>>"= ";
    while((si2=si>>" ")!=NULL)si=si2; 
    return (si!=NULL) ? si>>m.setProb(k)>>psD : si;
  }
};


template<class Y, class P> 
class RandAccCPT1DModel : public GenericRACPTModel<MapKey1D<Y>,P> {
 public:
//  typedef typename GenericCPTModel<Y,MapKey1D<Unit>,P>::IterVal IterVal;

  bool contains ( const Y& y ) const {
    return GenericRACPTModel<MapKey1D<Y>,P>::contains ( MapKey1D<Y>(y) );
  }
/*
  P getProb ( const IterVal& ixyp ) const {
    return GenericCPTModel<MapKey1D<Y>,P>::getProb ( ixyp, MapKey1D<Y>(Y()) );
  }
*/
  P getProb ( const Y& y ) const {
    return GenericRACPTModel<MapKey1D<Y>,P>::getProb ( MapKey1D<Y>(y) );
  }
/*
P& setProb ( const Y& y ) {
    cerr << "setProb called on racpt1d" << endl;
    return GenericRACPTModel<MapKey1D<Y>,P>::setProb ( MapKey1D<Y>(y) );
  }
*/
  /*
  bool readFields ( Array<char*>& aps ) {
    if ( 3==aps.size() ) {
      GenericRACPTModel<MapKey1D<Y>,P>::setProb ( MapKey1D<Y>(aps[1]) ) = atof(aps[2]);
      return true;
    }
    return false;
  }
  */
};


////////////////////
template<class Y, class X1, class P> 
class RandAccCPT2DModel : public GenericRACPTModel<MapKey2D<X1,Y>,P> {
 public:

  // This stuff only for deterministic 'Determ' models...
//  typedef X1 Dep1Type;
//  typedef P ProbType;
//  MapKey1D<X1> condKey;

  bool contains ( const Y& y, const X1& x1 ) const {
//    MapKey2D<X1,Y> temp = MapKey2D<X1,Y>(x1,y);
    return GenericRACPTModel<MapKey2D<X1,Y>,P>::contains ( MapKey2D<X1,Y>(x1,y) );
  }

  P getProb ( const Y& y, const X1& x1 ) const {
    return GenericRACPTModel<MapKey2D<X1,Y>,P>::getProb ( MapKey2D<X1,Y>(x1,y) );
  }
  
/*
  P& setProb ( const Y& y, const X1& x1 ) {
    cerr << "setProb called on racpt2d" << endl;
    return GenericRACPTModel<MapKey2D<Y,X1>,P>::setProb ( MapKey2D<Y,X1>(y,x1) );
  }
*/

};


////////////////////
template<class Y, class X1, class X2, class P> 
class RandAccCPT3DModel : public GenericRACPTModel<MapKey3D<X1,X2,Y>,P> {
 public:

  bool contains ( const Y& y, const X1& x1, const X2& x2 ) const {
    return GenericRACPTModel<MapKey3D<X1,X2,Y>,P>::contains ( MapKey3D<X1,X2,Y>(x1,x2,y) );
  }

  P getProb ( const Y& y, const X1& x1, const X2& x2 ) const {
    return GenericRACPTModel<MapKey3D<X1,X2,Y>,P>::getProb ( MapKey3D<X1,X2,Y>(x1,x2,y) );
  }
/*
  P& setProb ( const Y& y, const X1& x1, const X2& x2 ) {
    return GenericRACPTModel<MapKey3D<X1,X2,Y>,P>::setProb (  MapKey3D<Y,X1,X2>(x1,x2,y) );
  }
*/
};

/*
////////////////////
template<class Y, class X1, class X2, class X3, class P> 
class CPT4DModel : public GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P> {
 public:
  typedef typename GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P>::IterVal IterVal;

  bool setFirst ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3 ) const {
    return GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P>::setFirst ( ixyp, MapKey3D<X1,X2,X3>(x1,x2,x3) );
  }
  bool setNext ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3 ) const {
    return GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P>::setNext ( ixyp, MapKey3D<X1,X2,X3>(x1,x2,x3) );
  }
  bool contains ( const Y& y, const X1& x1, const X2& x2, const X3& x3 ) const {
    return GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P>::contains ( y, MapKey3D<X1,X2,X3>(x1,x2,x3) );
  }
  bool contains ( const X1& x1, const X2& x2, const X3& x3 ) const {
    return GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P>::contains ( MapKey3D<X1,X2,X3>(x1,x2,x3) );
  }
  P getProb ( const IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3 ) const {
    return GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P>::getProb ( ixyp, MapKey3D<X1,X2,X3>(x1,x2,x3) );
  }
  P getProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3 ) const {
    return GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P>::getProb ( y, MapKey3D<X1,X2,X3>(x1,x2,x3) );
  }
  P& setProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3 ) {
    return GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P>::setProb ( y, MapKey3D<X1,X2,X3>(x1,x2,x3) );
  }
  bool readFields ( Array<char*>& aps ) {
    if ( 6==aps.size() ) {
      GenericCPTModel<Y,MapKey3D<X1,X2,X3>,P>::setProb ( Y(aps[4]), MapKey3D<X1,X2,X3>(aps[1],aps[2],aps[3]) ) = atof(aps[5]);
      return true;
    }
    return false;
  }
};


////////////////////
template<class Y, class X1, class X2, class X3, class X4, class P> 
class CPT5DModel : public GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P> {
 public:
  typedef typename GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P>::IterVal IterVal;

  bool setFirst ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const {
    return GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P>::setFirst ( ixyp, MapKey4D<X1,X2,X3,X4>(x1,x2,x3,x4) );
  }
  bool setNext ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const {
    return GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P>::setNext ( ixyp, MapKey4D<X1,X2,X3,X4>(x1,x2,x3,x4) );
  }
  bool contains ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const {
    return GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P>::contains ( y, MapKey4D<X1,X2,X3,X4>(x1,x2,x3,x4) );
  }
  bool contains ( const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const {
    return GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P>::contains ( MapKey4D<X1,X2,X3,X4>(x1,x2,x3,x4) );
  }
  P getProb ( const IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const {
    return GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P>::getProb ( ixyp, MapKey4D<X1,X2,X3,X4>(x1,x2,x3,x4) );
  }
  P getProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const {
    return GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P>::getProb ( y, MapKey4D<X1,X2,X3,X4>(x1,x2,x3,x4) );
  }
  P& setProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) {
    return GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P>::setProb ( y, MapKey4D<X1,X2,X3,X4>(x1,x2,x3,x4) );
  }
  bool readFields ( Array<char*>& aps ) {
    if ( 7==aps.size() ) {
      GenericCPTModel<Y,MapKey4D<X1,X2,X3,X4>,P>::setProb ( Y(aps[5]), MapKey4D<X1,X2,X3,X4>(aps[1],aps[2],aps[3],aps[4]) ) = atof(aps[6]);
      return true;
    }
    return false;
  }
};


////////////////////
template<class Y, class X1, class X2, class X3, class X4, class X5, class P> 
class RACPT6DModel : public GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P> {
 public:
  typedef typename GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P>::IterVal IterVal;

  bool setFirst ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const {
    return GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P>::setFirst ( ixyp, MapKey5D<X1,X2,X3,X4,X5>(x1,x2,x3,x4,x5) );
  }
  bool setNext ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const {
    return GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P>::setNext ( ixyp, MapKey5D<X1,X2,X3,X4,X5>(x1,x2,x3,x4,x5) );
  }
  bool contains ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const {
    return GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P>::contains ( y, MapKey5D<X1,X2,X3,X4,X5>(x1,x2,x3,x4,x5) );
  }
  bool contains ( const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const {
    return GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P>::contains ( MapKey5D<X1,X2,X3,X4,X5>(x1,x2,x3,x4,x5) );
  }
  P getProb ( const IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const {
    return GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P>::getProb ( ixyp, MapKey5D<X1,X2,X3,X4,X5>(x1,x2,x3,x4,x5) );
  }
  P getProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const {
    return GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P>::getProb ( y, MapKey5D<X1,X2,X3,X4,X5>(x1,x2,x3,x4,x5) );
  }
  P& setProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) {
    return GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P>::setProb ( y, MapKey5D<X1,X2,X3,X4,X5>(x1,x2,x3,x4,x5) );
  }
  bool readFields ( Array<char*>& aps ) {
    if ( 8==aps.size() ) {
      GenericCPTModel<Y,MapKey5D<X1,X2,X3,X4,X5>,P>::setProb ( Y(aps[6]), MapKey5D<X1,X2,X3,X4,X5>(aps[1],aps[2],aps[3],aps[4],aps[5]) ) = atof(aps[7]);
      return true;
    }
    return false;
  }
};

*/
#endif //_NL_RACPT__