00001
00002
00003 #ifndef moses_SyntacticLanguageModelState_h
00004 #define moses_SyntacticLanguageModelState_h
00005
00006 #include "nl-iomacros.h"
00007 #include "nl-cpt.h"
00008 #include "nl-hmm.h"
00009
00010 #include "SyntacticLanguageModelFiles.h"
00011 #include "moses/FF/FFState.h"
00012 #include <string>
00013
00014 namespace Moses
00015 {
00016
00017 template <class MY, class MX, class YS=typename MY::RandVarType, class B=NullBackDat<typename MY::RandVarType> >
00018 class SyntacticLanguageModelState : public FFState
00019 {
00020 public:
00021
00022
00023 SyntacticLanguageModelState( SyntacticLanguageModelFiles<MY,MX>* modelData, int beamSize );
00024
00025
00026 SyntacticLanguageModelState( const SyntacticLanguageModelState* prev, std::string word );
00027
00028
00029 ~SyntacticLanguageModelState() {
00030 VERBOSE(3,"Destructing SyntacticLanguageModelState" << std::endl);
00031 delete randomVariableStore;
00032 }
00033
00034 virtual int Compare(const FFState& other) const;
00035
00036
00037 double getScore() const;
00038
00039 double getProb() const;
00040
00041 private:
00042
00043 void setScore(double score);
00044 void printRV();
00045
00046 SafeArray1D<Id<int>,pair<YS,LogProb> >* randomVariableStore;
00047 double prob;
00048 double score;
00049 int beamSize;
00050 SyntacticLanguageModelFiles<MY,MX>* modelData;
00051 bool sentenceStart;
00052 };
00053
00054
00056
00057
00058 template <class MY, class MX, class YS, class B>
00059 void SyntacticLanguageModelState<MY,MX,YS,B>::printRV()
00060 {
00061
00062 cerr << "*********** BEGIN printRV() ******************" << endl;
00063 int size=randomVariableStore->getSize();
00064 cerr << "randomVariableStore->getSize() == " << size << endl;
00065
00066 for (int depth=0; depth<size; depth+=1) {
00067
00068
00069 const pair<YS,LogProb> *data = &(randomVariableStore->get(depth));
00070 std::cerr << "randomVariableStore[" << depth << "]\t" << data->first << "\tprob = " << data->second.toProb() << "\tlogProb = " << double(data->second.toInt())/100 << std::endl;
00071
00072 }
00073 cerr << "*********** END printRV() ******************" << endl;
00074
00075 }
00076
00077
00078
00079
00080
00081
00082 template <class MY, class MX, class YS, class B>
00083 SyntacticLanguageModelState<MY,MX,YS,B>::SyntacticLanguageModelState( SyntacticLanguageModelFiles<MY,MX>* modelData, int beamSize )
00084 {
00085
00086 this->randomVariableStore = new SafeArray1D<Id<int>,pair<YS,LogProb> >();
00087 this->modelData = modelData;
00088 this->beamSize = beamSize;
00089
00090
00091 YS xBEG;
00092 StringInput(String(BEG_STATE).c_array())>>xBEG>>"\0";
00093 cerr<<xBEG<<"\n";
00094
00095
00096
00097
00098
00099 this->randomVariableStore->init(1,pair<YS,LogProb>(xBEG,0));
00100
00101 this->sentenceStart = true;
00102
00103 IFVERBOSE(3) {
00104 VERBOSE(3,"Examining RV store just after RV init" << endl);
00105 printRV();
00106 }
00107
00108
00109 LogProb l(1.0);
00110
00111 setScore(l.toDouble());
00112
00113
00114
00115
00116 }
00117
00118
00119 template <class MY, class MX, class YS, class B>
00120 int SyntacticLanguageModelState<MY,MX,YS,B>::Compare(const FFState& other) const
00121 {
00122
00123
00124
00125
00126
00127
00128
00129
00130 return 0;
00131 }
00132
00133
00134 template <class MY, class MX, class YS, class B>
00135 SyntacticLanguageModelState<MY,MX,YS,B>::SyntacticLanguageModelState( const SyntacticLanguageModelState* prev, std::string word )
00136 {
00137
00138
00139 this->randomVariableStore = new SafeArray1D<Id<int>,pair<YS,LogProb> >();
00140 this->modelData = prev->modelData;
00141 this->beamSize = prev->beamSize;
00142 this->randomVariableStore->init(this->beamSize);
00143 this->sentenceStart=false;
00144
00145 YS ysEND;
00146 StringInput(String(END_STATE).c_array())>>ysEND>>"\0";
00147
00148
00149 MY& mH = *(modelData->getHiddenModel());
00150 MX& mO = *(modelData->getObservedModel());
00151
00152
00153 HMM<MY,MX,YS,B> hmm(mH,mO);
00154 int MAX_WORDS = 2;
00155 hmm.init(MAX_WORDS,this->beamSize,prev->randomVariableStore);
00156 typename MX::RandVarType x(word.c_str());
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 hmm.getCurrSum();
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 hmm.updateRanked(x, prev->sentenceStart);
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 double currSum = hmm.getCurrSum();
00237
00238 setScore(currSum);
00239
00240
00241
00242
00243 hmm.gatherElementsInBeam(randomVariableStore);
00244
00245
00246
00247
00248
00249
00250
00251
00252 }
00253
00254
00255 template <class MY, class MX, class YS, class B>
00256 double SyntacticLanguageModelState<MY,MX,YS,B>::getProb() const
00257 {
00258
00259 return prob;
00260 }
00261
00262 template <class MY, class MX, class YS, class B>
00263 double SyntacticLanguageModelState<MY,MX,YS,B>::getScore() const
00264 {
00265
00266 return score;
00267 }
00268
00269
00270 template <class MY, class MX, class YS, class B>
00271 void SyntacticLanguageModelState<MY,MX,YS,B>::setScore(double score)
00272 {
00273
00274
00275
00276
00277 this->prob = score;
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 if (score==0) {
00294 this->score = -100;
00295 } else {
00296 double x = log(score) / 7.44440071921381;
00297 if ( x >= -100) {
00298 this->score = x;
00299 } else {
00300 this->score = -100;
00301 }
00302 }
00303
00304 VERBOSE(3,"\tSyntacticLanguageModelState has score=" << this->score << endl);
00305
00306 }
00307
00308
00309 }
00310
00311 #endif