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

SyntacticLanguageModelState.h « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf35616d984e14f66e4531cbbe15b38d6bc9be97 (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
//

#ifndef moses_SyntacticLanguageModelState_h
#define moses_SyntacticLanguageModelState_h

#include "nl-iomacros.h"
#include "nl-cpt.h"
#include "nl-hmm.h"

#include "SyntacticLanguageModelFiles.h"
#include "moses/FF/FFState.h"
#include <string>

namespace Moses
{

template <class MY, class MX, class YS=typename MY::RandVarType, class B=NullBackDat<typename MY::RandVarType> >
class SyntacticLanguageModelState : public FFState
{
public:

  // Initialize an empty LM state
  SyntacticLanguageModelState( SyntacticLanguageModelFiles<MY,MX>* modelData, int beamSize );

  // Get the next LM state from an existing LM state and the next word
  SyntacticLanguageModelState( const SyntacticLanguageModelState* prev, std::string word );


  ~SyntacticLanguageModelState() {
    VERBOSE(3,"Destructing SyntacticLanguageModelState" << std::endl);
    delete randomVariableStore;
  }

  virtual int Compare(const FFState& other) const;

  // Get the LM score from this LM state
  double getScore() const;

  double getProb() const;

private:

  void setScore(double score);
  void printRV();

  SafeArray1D<Id<int>,pair<YS,LogProb> >* randomVariableStore;
  double prob;
  double score;
  int beamSize;
  SyntacticLanguageModelFiles<MY,MX>* modelData;
  bool sentenceStart;
};


////////////////////////////////////////////////////////////////////////////////


template <class MY, class MX, class YS, class B>
void SyntacticLanguageModelState<MY,MX,YS,B>::printRV()
{

  cerr << "*********** BEGIN printRV() ******************" << endl;
  int size=randomVariableStore->getSize();
  cerr << "randomVariableStore->getSize() == " << size << endl;

  for (int depth=0; depth<size; depth+=1) {


    const pair<YS,LogProb> *data = &(randomVariableStore->get(depth));
    std::cerr << "randomVariableStore[" << depth << "]\t" << data->first << "\tprob = " << data->second.toProb() << "\tlogProb = " << double(data->second.toInt())/100 << std::endl;

  }
  cerr << "*********** END printRV() ******************" << endl;

}

// Initialize an empty LM state from grammar files
//
//    nArgs is the number of model files
//    argv is the list of model file names
//
template <class MY, class MX, class YS, class B>
SyntacticLanguageModelState<MY,MX,YS,B>::SyntacticLanguageModelState( SyntacticLanguageModelFiles<MY,MX>* modelData, int beamSize )
{

  this->randomVariableStore = new SafeArray1D<Id<int>,pair<YS,LogProb> >();
  this->modelData = modelData;
  this->beamSize = beamSize;

  // Initialize an empty random variable value
  YS xBEG;
  StringInput(String(BEG_STATE).c_array())>>xBEG>>"\0";
  cerr<<xBEG<<"\n";

  //  cout << "Examining RV store just before RV init" << endl;
  //printRV();

  // Initialize the random variable store
  this->randomVariableStore->init(1,pair<YS,LogProb>(xBEG,0));

  this->sentenceStart = true;

  IFVERBOSE(3) {
    VERBOSE(3,"Examining RV store just after RV init" << endl);
    printRV();
  }

  // Get score of final frame in HHMM
  LogProb l(1.0);
  //score = l.toDouble();
  setScore(l.toDouble());
  //  MY::F_ROOT_OBS = true;
// this->modelData->getHiddenModel()->setRootObs(true);


}


template <class MY, class MX, class YS, class B>
int SyntacticLanguageModelState<MY,MX,YS,B>::Compare(const FFState& other) const
{
  /*
  const SyntacticLanguageModelState<MY,MX,YS,B>& o =
    static_cast<const SyntacticLanguageModelState<MY,MX,YS,B>&>(other);

  if (o.score > score) return 1;
  else if (o.score < score) return -1;
  else return 0;
  */
  return 0;
}


template <class MY, class MX, class YS, class B>
SyntacticLanguageModelState<MY,MX,YS,B>::SyntacticLanguageModelState( const SyntacticLanguageModelState* prev, std::string word )
{

  // Initialize member variables
  this->randomVariableStore = new SafeArray1D<Id<int>,pair<YS,LogProb> >();
  this->modelData = prev->modelData;
  this->beamSize = prev->beamSize;
  this->randomVariableStore->init(this->beamSize);
  this->sentenceStart=false;

  YS ysEND;
  StringInput(String(END_STATE).c_array())>>ysEND>>"\0";

  // Get HHMM model files
  MY& mH = *(modelData->getHiddenModel());
  MX& mO = *(modelData->getObservedModel());

  // Initialize HHMM
  HMM<MY,MX,YS,B> hmm(mH,mO);
  int MAX_WORDS  = 2;
  hmm.init(MAX_WORDS,this->beamSize,prev->randomVariableStore);
  typename MX::RandVarType x(word.c_str());
  //  cout << "Examining HHMM just after hmm.init" << endl;
  //  hmm.debugPrint();


  /*  cerr << "*********** BEGIN writeCurr() ******************" << endl;
  hmm.writeCurr(cout,0);
  hmm.writeCurr(cout,1);
  cerr << "*********** END writeCurr() ******************" << endl;
  */
  /*
    {

    int wnum=1;
    list<TrellNode<YS,B> > lys = hmm.getMLSnodes(ysEND);                                           // get mls list
        for ( typename list<TrellNode<YS,B> >::iterator i=lys.begin(); i!=lys.end(); i++, wnum++ ) {   // for each frame
          cout << "HYPOTH " << wnum
               << " " << i->getBackData()
               << " " << x
               << " " << i->getId()
               << " (" << i->getLogProb() << ")"
               << endl;                                                                             //   print RV val
        }
    }
    */


  /*
  cerr << "Writing hmm.writeCurr" << endl;
  hmm.writeCurr(cerr,0);
  hmm.writeCurr(cerr,1);
  cerr << "...done writing hmm.writeCurr" << endl;
  */
  hmm.getCurrSum();



  // Initialize observed variable
  //  typename MX::RandVarType ov;
  //  ov.set(word.c_str(),mO);
  //  MY::WORD = ov.getW();
  //bool endOfSentence = prev->sentenceStart;//true;

  //  std::cerr << "About to give HHMM a word of input:\t" << word << std::endl;

  hmm.updateRanked(x, prev->sentenceStart);

  //  cout << "Examining HHMM just after hmm.updateRanked(" << x << "," << prev->sentenceStart << ")" << endl;
  //  hmm.debugPrint();
  /*
    cerr << "*********** BEGIN writeCurr() ******************" << endl;
    hmm.writeCurr(cout,0);
    hmm.writeCurr(cout,1);
    cerr << "*********** END writeCurr() ******************" << endl;
    */
  /*
  {

    int wnum=1;
    list<TrellNode<YS,B> > lys = hmm.getMLSnodes(ysEND);                                           // get mls list
        for ( typename list<TrellNode<YS,B> >::iterator i=lys.begin(); i!=lys.end(); i++, wnum++ ) {   // for each frame
          cout << "HYPOTH " << wnum
               << " " << i->getBackData()
               << " " << x
               << " " << i->getId()
               << " (" << i->getLogProb() << ")"
               << endl;                                                                             //   print RV val
        }
    }
    */
//  X ov(word.c_str());
  //mH.setWord(ov);
  // MY::WORD = ov;//ov.getW();

  // Update HHMM based on observed variable
  //hmm.updateRanked(ov);
  //mH.setRootObs(true);
  //MY::F_ROOT_OBS = false;

  // Get the current score
  double currSum = hmm.getCurrSum();
  //VERBOSE(3,"Setting score using currSum for " << scientific << x << " = " << currSum << endl);
  setScore(currSum);
  //  cout << "Examining RV store just before RV init via gatherElementsInBeam" << endl;
  //  printRV();

  // Get new hidden random variable store from HHMM
  hmm.gatherElementsInBeam(randomVariableStore);
  //  cout << "Examining RV store just after RV init via gatherElementsInBeam" << endl;
  //  printRV();
  /*
  cerr << "Writing hmm.writeCurr..." << endl;
  hmm.writeCurr(cerr,0);
  hmm.writeCurr(cerr,1);
  cerr << "...done writing hmm.writeCurr" << endl;
  */
}


template <class MY, class MX, class YS, class B>
double SyntacticLanguageModelState<MY,MX,YS,B>::getProb() const
{

  return prob;
}

template <class MY, class MX, class YS, class B>
double SyntacticLanguageModelState<MY,MX,YS,B>::getScore() const
{

  return score;
}


template <class MY, class MX, class YS, class B>
void SyntacticLanguageModelState<MY,MX,YS,B>::setScore(double score)
{




  this->prob = score;

  // We want values to range from -100 to 0
  //
  // If the minimum positive value for a double is min=4.94065645841246544e-324
  //    then to scale, we want a logarithmic base such that log_b(min)=-100
  //
  // -100 = log(min) / log(b)
  //
  // log(b) = log(min) / -100
  //
  // b = exp( log(min) / -100 )
  //
  // b = 7.44440071921381

  // Check for score==0 to avoid causing -infinity with log(score)
  if (score==0) {
    this->score = -100;
  } else {
    double x = log(score) / 7.44440071921381;
    if ( x >= -100) {
      this->score = x;
    } else {
      this->score = -100;
    }
  }

  VERBOSE(3,"\tSyntacticLanguageModelState has score=" << this->score << endl);

}


}

#endif