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

TranslationOption.h « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f191f0215a4352720281875af483bbff8717d0c1 (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
// -*- c++ -*-
// $Id$

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#ifndef moses_TranslationOption_h
#define moses_TranslationOption_h

#include <map>
#include <vector>
#include <boost/functional/hash.hpp>
#include "Bitmap.h"
#include "Range.h"
#include "Phrase.h"
#include "TargetPhrase.h"
#include "Hypothesis.h"
#include "Util.h"
#include "TypeDef.h"
#include "ScoreComponentCollection.h"
#include "StaticData.h"
namespace Moses
{

class PhraseDictionary;
class GenerationDictionary;
class FeatureFunction;
class LexicalReordering;


/** Available phrase translation for a particular sentence pair.
 * In a multi-factor model, this is expanded from the entries in the
 * translation tables and generation tables (and pruned to the maximum
 * number allowed). By pre-computing the allowable phrase translations,
 * efficient beam search in Manager is possible when expanding instances
 * of the class Hypothesis - the states in the search.
 *
 * A translation option contains source and target phrase, aggregate
 * and details scores (in m_scoreBreakdown), including an estimate
 * how expensive this option will be in search (used to build the
 * future cost matrix.)
 *
 * m_targetPhrase points to a phrase-table entry.
 * The source word range is zero-indexed, so it can't refer to an empty range. The target phrase may be empty.
 */
class TranslationOption
{
  friend std::ostream& operator<<(std::ostream& out, const TranslationOption& possibleTranslation);

protected:

  TargetPhrase 		m_targetPhrase; /*< output phrase when using this translation option */
  const InputPath		*m_inputPath;
  const Range	m_sourceWordsRange; /*< word position in the input that are covered by this translation option */
  float             m_futureScore; /*< estimate of total cost when using this translation option, includes language model probabilities */

  // typedef std::map<const LexicalReordering*, Scores> _ScoreCacheMap;
  // _ScoreCacheMap m_lexReorderingScores;
  // m_lexReorderingScores was moved to TargetPhrase.h so that phrase tables
  // can add information (such as lexical reordering scores) to target phrases
  // during lookup.

public:
  struct Better {
    bool operator()(TranslationOption const& a, TranslationOption const& b) const {
      return a.GetFutureScore() > b.GetFutureScore();
    }

    bool operator()(TranslationOption const* a, TranslationOption const* b) const {
      return a->GetFutureScore() > b->GetFutureScore();
    }
  };


  explicit TranslationOption(); // For initial hypo that does translate nothing

  /** constructor. Used by initial translation step */
  TranslationOption(const Range &range
                    , const TargetPhrase &targetPhrase);

  /** returns true if all feature types in featuresToCheck are compatible between the two phrases */
  bool IsCompatible(const Phrase& phrase, const std::vector<FactorType>& featuresToCheck) const;

  /** returns target phrase */
  inline const TargetPhrase &GetTargetPhrase() const {
    return m_targetPhrase;
  }

  /** returns source word range */
  inline const Range &GetSourceWordsRange() const {
    return m_sourceWordsRange;
  }

  /** returns source phrase */
  const InputPath &GetInputPath() const;

  void SetInputPath(const InputPath &inputPath);

  /** whether source span overlaps with those of a hypothesis */
  bool Overlap(const Hypothesis &hypothesis) const;

  /** return start index of source phrase */
  inline size_t GetStartPos() const {
    return m_sourceWordsRange.GetStartPos();
  }

  /** return end index of source phrase */
  inline size_t GetEndPos() const {
    return m_sourceWordsRange.GetEndPos();
  }

  /** return length of source phrase */
  inline size_t GetSize() const {
    return m_sourceWordsRange.GetEndPos() - m_sourceWordsRange.GetStartPos() + 1;
  }

  /** return estimate of total cost of this option */
  inline float GetFutureScore() const {
    return m_futureScore;
  }

  /** return true if the source phrase translates into nothing */
  inline bool IsDeletionOption() const {
    return m_targetPhrase.GetSize() == 0;
  }

  /** returns detailed component scores */
  inline const ScoreComponentCollection &GetScoreBreakdown() const {
    return m_targetPhrase.GetScoreBreakdown();
  }

  inline ScoreComponentCollection &GetScoreBreakdown() {
    return m_targetPhrase.GetScoreBreakdown();
  }

  void EvaluateWithSourceContext(const InputType &input);

  void UpdateScore(ScoreComponentCollection *futureScoreBreakdown = NULL) {
    m_targetPhrase.UpdateScore(futureScoreBreakdown);
  }

  /** returns cached scores */
  // inline
  const Scores*
  GetLexReorderingScores(const LexicalReordering *scoreProducer) const;
  // {
  //   return m_targetPhrase.GetExtraScores(scoreProducer);
  // }

  void CacheLexReorderingScores(const LexicalReordering &scoreProducer,
                                const Scores &score);

  TO_STRING();

  bool operator== (const TranslationOption &rhs) const {
    return m_sourceWordsRange == rhs.m_sourceWordsRange &&
           m_targetPhrase == rhs.m_targetPhrase;
  }

};


//XXX: This doesn't look at the alignment. Is this correct?
inline size_t hash_value(const TranslationOption& translationOption)
{
  size_t  seed = 0;
  boost::hash_combine(seed, translationOption.GetTargetPhrase());
  boost::hash_combine(seed, translationOption.GetStartPos());
  boost::hash_combine(seed, translationOption.GetEndPos());
  return seed;
}


}

#endif