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

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

#pragma once

#include <vector>
#include <string>

#include "FFState.h"
#include "Hypothesis.h"
#include "LexicalReordering.h"
#include "WordsRange.h"
#include "WordsBitmap.h"
#include "ReorderingStack.h"
#include "TranslationOption.h"


namespace Moses
{
class LexicalReorderingState;
class LexicalReordering;

//! Factory class for lexical reordering states
class LexicalReorderingConfiguration
{
public:
  friend class LexicalReordering;
  enum ModelType {Monotonic, MSD, MSLR, LeftRight, None};
  enum Direction {Forward, Backward, Bidirectional};
  enum Condition {F, E, FE};

  LexicalReorderingConfiguration(const std::string &modelType);

  LexicalReorderingState *CreateLexicalReorderingState(const InputType &input) const;

  size_t GetNumScoreComponents() const;
  void SetAdditionalScoreComponents(size_t number);
  size_t GetNumberOfTypes() const;

  ScoreProducer *GetScoreProducer() const {
    return m_scoreProducer;
  }


  ModelType GetModelType() const {
    return m_modelType;
  }

  Direction GetDirection() const {
    return m_direction;
  }

  Condition GetCondition() const {
    return m_condition;
  }

  bool IsPhraseBased() const {
    return m_phraseBased;
  }

  bool CollapseScores() const {
    return m_collapseScores;
  }

private:
  void SetScoreProducer(ScoreProducer* scoreProducer) {
    m_scoreProducer = scoreProducer;
  }

  const std::string& GetModelString() const {
    return m_modelString;
  }

  std::string m_modelString;
  ScoreProducer *m_scoreProducer;
  ModelType m_modelType;
  bool m_phraseBased;
  bool m_collapseScores;
  Direction m_direction;
  Condition m_condition;
  size_t m_additionalScoreComponents;
};

//! Abstract class for lexical reordering model states
class LexicalReorderingState : public FFState
{
public:
  virtual int Compare(const FFState& o) const = 0;
  virtual LexicalReorderingState* Expand(const TranslationOption& hypo, Scores& scores) const = 0;

  static LexicalReorderingState* CreateLexicalReorderingState(const std::vector<std::string>& config,
      LexicalReorderingConfiguration::Direction dir, const InputType &input);

protected:
  typedef int ReorderingType;


  const LexicalReorderingConfiguration &m_configuration;
  // The following is the true direction of the object, which can be Backward or Forward even if the Configuration has Bidirectional.
  LexicalReorderingConfiguration::Direction m_direction;
  size_t m_offset;
  const Scores *m_prevScore;

  inline LexicalReorderingState(const LexicalReorderingState *prev, const TranslationOption &topt) :
    m_configuration(prev->m_configuration), m_direction(prev->m_direction), m_offset(prev->m_offset),
    m_prevScore(topt.GetCachedScores(m_configuration.GetScoreProducer())) {}

  inline LexicalReorderingState(const LexicalReorderingConfiguration &config, LexicalReorderingConfiguration::Direction dir, size_t offset)
    : m_configuration(config), m_direction(dir), m_offset(offset), m_prevScore(NULL) {}

  // copy the right scores in the right places, taking into account forward/backward, offset, collapse
  void CopyScores(Scores& scores, const TranslationOption& topt, ReorderingType reoType) const;
  void ClearScores(Scores& scores) const;
  int ComparePrevScores(const Scores *other) const;

  //constants for the different type of reorderings (corresponding to indexes in the table file)
  static const ReorderingType M = 0;  // monotonic
  static const ReorderingType NM = 1; // non-monotonic
  static const ReorderingType S = 1;  // swap
  static const ReorderingType D = 2;  // discontinuous
  static const ReorderingType DL = 2; // discontinuous, left
  static const ReorderingType DR = 3; // discontinuous, right
  static const ReorderingType R = 0;  // right
  static const ReorderingType L = 1;  // left
};

class BidirectionalReorderingState : public LexicalReorderingState
{
private:
  const LexicalReorderingState *m_backward;
  const LexicalReorderingState *m_forward;
public:
  BidirectionalReorderingState(const LexicalReorderingConfiguration &config, const LexicalReorderingState *bw, const LexicalReorderingState *fw, size_t offset) :
    LexicalReorderingState(config, LexicalReorderingConfiguration::Bidirectional, offset), m_backward(bw), m_forward(fw) {}

  ~BidirectionalReorderingState() {
    delete m_backward;
    delete m_forward;
  }

  virtual int Compare(const FFState& o) const;
  virtual LexicalReorderingState* Expand(const TranslationOption& topt, Scores& scores) const;
};

//! State for the standard Moses implementation of lexical reordering models
//! (see Koehn et al, Edinburgh System Description for the 2005 NIST MT Evaluation)
class PhraseBasedReorderingState : public LexicalReorderingState
{
private:
  WordsRange m_prevRange;
  bool m_first;
public:
  PhraseBasedReorderingState(const LexicalReorderingConfiguration &config, LexicalReorderingConfiguration::Direction dir, size_t offset);
  PhraseBasedReorderingState(const PhraseBasedReorderingState *prev, const TranslationOption &topt);

  virtual int Compare(const FFState& o) const;
  virtual LexicalReorderingState* Expand(const TranslationOption& topt, Scores& scores) const;

  ReorderingType GetOrientationTypeMSD(WordsRange currRange) const;
  ReorderingType GetOrientationTypeMSLR(WordsRange currRange) const;
  ReorderingType GetOrientationTypeMonotonic(WordsRange currRange) const;
  ReorderingType GetOrientationTypeLeftRight(WordsRange currRange) const;
};

//! State for a hierarchical reordering model
//! (see Galley and Manning, A Simple and Effective Hierarchical Phrase Reordering Model, EMNLP 2008)
//!backward state (conditioned on the previous phrase)
class HierarchicalReorderingBackwardState : public LexicalReorderingState
{
private:
  ReorderingStack m_reoStack;
public:
  HierarchicalReorderingBackwardState(const LexicalReorderingConfiguration &config, size_t offset);
  HierarchicalReorderingBackwardState(const HierarchicalReorderingBackwardState *prev,
                                      const TranslationOption &topt, ReorderingStack reoStack);

  virtual int Compare(const FFState& o) const;
  virtual LexicalReorderingState* Expand(const TranslationOption& hypo, Scores& scores) const;

private:
  ReorderingType GetOrientationTypeMSD(int reoDistance) const;
  ReorderingType GetOrientationTypeMSLR(int reoDistance) const;
  ReorderingType GetOrientationTypeMonotonic(int reoDistance) const;
  ReorderingType GetOrientationTypeLeftRight(int reoDistance) const;
};


//!forward state (conditioned on the next phrase)
class HierarchicalReorderingForwardState : public LexicalReorderingState
{
private:
  bool m_first;
  WordsRange m_prevRange;
  WordsBitmap m_coverage;

public:
  HierarchicalReorderingForwardState(const LexicalReorderingConfiguration &config, size_t sentenceLength, size_t offset);
  HierarchicalReorderingForwardState(const HierarchicalReorderingForwardState *prev, const TranslationOption &topt);

  virtual int Compare(const FFState& o) const;
  virtual LexicalReorderingState* Expand(const TranslationOption& hypo, Scores& scores) const;

private:
  ReorderingType GetOrientationTypeMSD(WordsRange currRange, WordsBitmap coverage) const;
  ReorderingType GetOrientationTypeMSLR(WordsRange currRange, WordsBitmap coverage) const;
  ReorderingType GetOrientationTypeMonotonic(WordsRange currRange, WordsBitmap coverage) const;
  ReorderingType GetOrientationTypeLeftRight(WordsRange currRange, WordsBitmap coverage) const;
};

}