00001
00002 #pragma once
00003 #include <vector>
00004 #include <string>
00005
00006 #include <boost/scoped_ptr.hpp>
00007
00008 #include "moses/Hypothesis.h"
00009 #include "moses/ScoreComponentCollection.h"
00010 #include "moses/Range.h"
00011 #include "moses/Bitmap.h"
00012 #include "moses/TranslationOption.h"
00013 #include "moses/FF/FFState.h"
00014 #include "ReorderingStack.h"
00015
00016 namespace Moses
00017 {
00018 class LRState;
00019 class LexicalReordering;
00020 class SparseReordering;
00021
00023 class LRModel
00024 {
00025 public:
00026 friend class LexicalReordering;
00027 enum ModelType { Monotonic, MSD, MSLR, LeftRight, None };
00028 enum Direction { Forward, Backward, Bidirectional };
00029 enum Condition { F, E, FE };
00030
00031
00032
00033 #if 0
00034 typedef int ReorderingType;
00035 static const ReorderingType M = 0;
00036 static const ReorderingType NM = 1;
00037 static const ReorderingType S = 1;
00038 static const ReorderingType D = 2;
00039 static const ReorderingType DL = 2;
00040 static const ReorderingType DR = 3;
00041 static const ReorderingType R = 0;
00042 static const ReorderingType L = 1;
00043 static const ReorderingType MAX = 3;
00044 #else
00045 enum ReorderingType {
00046 M = 0,
00047 NM = 1,
00048 S = 1,
00049 D = 2,
00050 DL = 2,
00051 DR = 3,
00052 R = 0,
00053 L = 1,
00054 MAX = 3,
00055 NONE = 4
00056 };
00057 #endif
00058
00059
00060
00061 ReorderingType
00062 GetOrientation(Range const& cur) const;
00063
00064 ReorderingType
00065 GetOrientation(Range const& prev, Range const& cur) const;
00066
00067 ReorderingType
00068 GetOrientation(Range const& prev, Range const& cur,
00069 Bitmap const& cov) const;
00070
00071 ReorderingType
00072 GetOrientation(int const reoDistance) const;
00073
00074 LRModel(const std::string &modelType);
00075
00076 void
00077 ConfigureSparse(const std::map<std::string,std::string>& sparseArgs,
00078 const LexicalReordering* producer);
00079
00080 LRState*
00081 CreateLRState(const InputType &input) const;
00082
00083 size_t GetNumberOfTypes() const;
00084 size_t GetNumScoreComponents() const;
00085 void SetAdditionalScoreComponents(size_t number);
00086
00087 LexicalReordering*
00088 GetScoreProducer() const {
00089 return m_scoreProducer;
00090 }
00091
00092 ModelType GetModelType() const {
00093 return m_modelType;
00094 }
00095 Direction GetDirection() const {
00096 return m_direction;
00097 }
00098 Condition GetCondition() const {
00099 return m_condition;
00100 }
00101
00102 bool
00103 IsPhraseBased() const {
00104 return m_phraseBased;
00105 }
00106
00107 bool
00108 CollapseScores() const {
00109 return m_collapseScores;
00110 }
00111
00112 SparseReordering const*
00113 GetSparseReordering() const {
00114 return m_sparse.get();
00115 }
00116
00117 private:
00118 void
00119 SetScoreProducer(LexicalReordering* scoreProducer) {
00120 m_scoreProducer = scoreProducer;
00121 }
00122
00123 std::string const&
00124 GetModelString() const {
00125 return m_modelString;
00126 }
00127
00128 std::string m_modelString;
00129 LexicalReordering *m_scoreProducer;
00130 ModelType m_modelType;
00131 bool m_phraseBased;
00132 bool m_collapseScores;
00133 Direction m_direction;
00134 Condition m_condition;
00135 size_t m_additionalScoreComponents;
00136 boost::scoped_ptr<SparseReordering> m_sparse;
00137 };
00138
00140 class LRState : public FFState
00141 {
00142 public:
00143
00144 typedef LRModel::ReorderingType ReorderingType;
00145
00146 virtual
00147 LRState*
00148 Expand(const TranslationOption& hypo, const InputType& input,
00149 ScoreComponentCollection* scores) const = 0;
00150
00151 static
00152 LRState*
00153 CreateLRState(const std::vector<std::string>& config,
00154 LRModel::Direction dir,
00155 const InputType &input);
00156
00157 protected:
00158
00159 const LRModel& m_configuration;
00160
00161
00162
00163 LRModel::Direction m_direction;
00164 size_t m_offset;
00165
00166 const TranslationOption *m_prevOption;
00167
00168 inline
00169 LRState(const LRState *prev,
00170 const TranslationOption &topt)
00171 : m_configuration(prev->m_configuration)
00172 , m_direction(prev->m_direction)
00173 , m_offset(prev->m_offset)
00174 , m_prevOption(&topt)
00175 { }
00176
00177 inline
00178 LRState(const LRModel &config,
00179 LRModel::Direction dir,
00180 size_t offset)
00181 : m_configuration(config)
00182 , m_direction(dir)
00183 , m_offset(offset)
00184 , m_prevOption(NULL)
00185 { }
00186
00187
00188
00189 void
00190 CopyScores(ScoreComponentCollection* scores,
00191 const TranslationOption& topt,
00192 const InputType& input, ReorderingType reoType) const;
00193
00194 int
00195 ComparePrevScores(const TranslationOption *other) const;
00196 };
00197
00199 class BidirectionalReorderingState
00200 : public LRState
00201 {
00202 private:
00203 const LRState *m_backward;
00204 const LRState *m_forward;
00205 public:
00206 BidirectionalReorderingState(const LRModel &config,
00207 const LRState *bw,
00208 const LRState *fw, size_t offset)
00209 : LRState(config,
00210 LRModel::Bidirectional,
00211 offset)
00212 , m_backward(bw)
00213 , m_forward(fw)
00214 { }
00215
00216 ~BidirectionalReorderingState() {
00217 delete m_backward;
00218 delete m_forward;
00219 }
00220
00221 virtual size_t hash() const;
00222 virtual bool operator==(const FFState& other) const;
00223
00224 LRState*
00225 Expand(const TranslationOption& topt, const InputType& input,
00226 ScoreComponentCollection* scores) const;
00227 };
00228
00232 class PhraseBasedReorderingState
00233 : public LRState
00234 {
00235 private:
00236 Range m_prevRange;
00237 bool m_first;
00238 public:
00239 static bool m_useFirstBackwardScore;
00240 PhraseBasedReorderingState(const LRModel &config,
00241 LRModel::Direction dir,
00242 size_t offset);
00243 PhraseBasedReorderingState(const PhraseBasedReorderingState *prev,
00244 const TranslationOption &topt);
00245
00246 virtual size_t hash() const;
00247 virtual bool operator==(const FFState& other) const;
00248
00249 virtual
00250 LRState*
00251 Expand(const TranslationOption& topt,const InputType& input,
00252 ScoreComponentCollection* scores) const;
00253
00254 ReorderingType GetOrientationTypeMSD(Range currRange) const;
00255 ReorderingType GetOrientationTypeMSLR(Range currRange) const;
00256 ReorderingType GetOrientationTypeMonotonic(Range currRange) const;
00257 ReorderingType GetOrientationTypeLeftRight(Range currRange) const;
00258 };
00259
00263 class HReorderingBackwardState : public LRState
00264 {
00265 private:
00266 ReorderingStack m_reoStack;
00267 public:
00268 HReorderingBackwardState(const LRModel &config, size_t offset);
00269 HReorderingBackwardState(const HReorderingBackwardState *prev,
00270 const TranslationOption &topt,
00271 ReorderingStack reoStack);
00272 virtual size_t hash() const;
00273 virtual bool operator==(const FFState& other) const;
00274
00275 virtual LRState* Expand(const TranslationOption& hypo, const InputType& input,
00276 ScoreComponentCollection* scores) const;
00277
00278 private:
00279 ReorderingType GetOrientationTypeMSD(int reoDistance) const;
00280 ReorderingType GetOrientationTypeMSLR(int reoDistance) const;
00281 ReorderingType GetOrientationTypeMonotonic(int reoDistance) const;
00282 ReorderingType GetOrientationTypeLeftRight(int reoDistance) const;
00283 };
00284
00285
00287 class HReorderingForwardState : public LRState
00288 {
00289 private:
00290 bool m_first;
00291 Range m_prevRange;
00292 Bitmap m_coverage;
00293
00294 public:
00295 HReorderingForwardState(const LRModel &config, size_t sentenceLength,
00296 size_t offset);
00297 HReorderingForwardState(const HReorderingForwardState *prev,
00298 const TranslationOption &topt);
00299
00300 virtual size_t hash() const;
00301 virtual bool operator==(const FFState& other) const;
00302
00303 virtual LRState* Expand(const TranslationOption& hypo,
00304 const InputType& input,
00305 ScoreComponentCollection* scores) const;
00306 };
00307 }
00308