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

ChartHypothesis.h « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9ed4f2f125cf60a288e0912300e7beb562bb24b8 (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
// vim:tabstop=2
/***********************************************************************
 Moses - factored phrase-based language decoder
 Copyright (C) 2010 Hieu Hoang

 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
 ***********************************************************************/

#pragma once

#include <vector>
#include <boost/scoped_ptr.hpp>
#include "Util.h"
#include "Range.h"
#include "ScoreComponentCollection.h"
#include "Phrase.h"
#include "ChartTranslationOptions.h"
#include "ObjectPool.h"

namespace Moses
{

class ChartKBestExtractor;
class ChartHypothesis;
class ChartManager;
class RuleCubeItem;
class FFState;

typedef std::vector<ChartHypothesis*> ChartArcList;

/** a hypothesis in the hierarchical/syntax decoder.
 * Contain a pointer to the current target phrase, a vector of previous hypos, and some scores
 */
class ChartHypothesis
{
  friend std::ostream& operator<<(std::ostream&, const ChartHypothesis&);
//  friend class ChartKBestExtractor;

protected:

  boost::shared_ptr<ChartTranslationOption> m_transOpt;

  Range m_currSourceWordsRange;
  std::vector<const FFState*> m_ffStates; /*! stateful feature function states */
  /*! sum of scores of this hypothesis, and previous hypotheses. Lazily initialised.  */
  mutable boost::scoped_ptr<ScoreComponentCollection> m_scoreBreakdown;
  mutable boost::scoped_ptr<ScoreComponentCollection> m_deltaScoreBreakdown;
  ScoreComponentCollection m_currScoreBreakdown /*! scores for this hypothesis only */
  ,m_lmNGram
  ,m_lmPrefix;
  float m_totalScore;

  ChartArcList *m_arcList; /*! all arcs that end at the same trellis point as this hypothesis */
  const ChartHypothesis *m_winningHypo;

  std::vector<const ChartHypothesis*> m_prevHypos; // always sorted by source position?

  ChartManager& m_manager;

  unsigned m_id; /* pkoehn wants to log the order in which hypotheses were generated */

  //! not implemented
  ChartHypothesis();

  //! not implemented
  ChartHypothesis(const ChartHypothesis &copy);

public:
  ChartHypothesis(const ChartTranslationOptions &, const RuleCubeItem &item,
                  ChartManager &manager);

  //! only used by ChartKBestExtractor
  ChartHypothesis(const ChartHypothesis &, const ChartKBestExtractor &);

  ~ChartHypothesis();

  unsigned GetId() const {
    return m_id;
  }

  const ChartTranslationOption &GetTranslationOption() const {
    return *m_transOpt;
  }

  //! Get the rule that created this hypothesis
  const TargetPhrase &GetCurrTargetPhrase() const {
    return m_transOpt->GetPhrase();
  }

  //! the source range that this hypothesis spans
  const Range &GetCurrSourceRange() const {
    return m_currSourceWordsRange;
  }

  //! the arc list when creating n-best lists
  inline const ChartArcList* GetArcList() const {
    return m_arcList;
  }

  //! the feature function states for a particular feature \param featureID
  inline const FFState* GetFFState( size_t featureID ) const {
    return m_ffStates[ featureID ];
  }

  //! reference back to the manager
  inline const ChartManager& GetManager() const {
    return m_manager;
  }

  void GetOutputPhrase(Phrase &outPhrase) const;
  Phrase GetOutputPhrase() const;

  // get leftmost/rightmost words only
  // leftRightMost: 1=left, 2=right
  void GetOutputPhrase(size_t leftRightMost, size_t numWords, Phrase &outPhrase) const;

  void EvaluateWhenApplied();

  void AddArc(ChartHypothesis *loserHypo);
  void CleanupArcList();
  void SetWinningHypo(const ChartHypothesis *hypo);

  //! get the unweighted score for each feature function
  const ScoreComponentCollection &GetScoreBreakdown() const {
    // Note: never call this method before m_currScoreBreakdown is fully computed
    if (!m_scoreBreakdown.get()) {
      m_scoreBreakdown.reset(new ScoreComponentCollection());
      // score breakdown from current translation rule
      if (m_transOpt) {
        m_scoreBreakdown->PlusEquals(GetTranslationOption().GetScores());
      }
      m_scoreBreakdown->PlusEquals(m_currScoreBreakdown);
      // score breakdowns from prev hypos
      for (std::vector<const ChartHypothesis*>::const_iterator iter = m_prevHypos.begin(); iter != m_prevHypos.end(); ++iter) {
        const ChartHypothesis &prevHypo = **iter;
        m_scoreBreakdown->PlusEquals(prevHypo.GetScoreBreakdown());
      }
    }
    return *(m_scoreBreakdown.get());
  }

  //! get the unweighted score delta for each feature function
  const ScoreComponentCollection &GetDeltaScoreBreakdown() const {
    // Note: never call this method before m_currScoreBreakdown is fully computed
    if (!m_deltaScoreBreakdown.get()) {
      m_deltaScoreBreakdown.reset(new ScoreComponentCollection());
      // score breakdown from current translation rule
      if (m_transOpt) {
        m_deltaScoreBreakdown->PlusEquals(GetTranslationOption().GetScores());
      }
      m_deltaScoreBreakdown->PlusEquals(m_currScoreBreakdown);
      // delta: score breakdowns from prev hypos _not_ added
    }
    return *(m_deltaScoreBreakdown.get());
  }

  //! Get the weighted total score
  float GetFutureScore() const {
    // scores from current translation rule. eg. translation models & word penalty
    return m_totalScore;
  }

  //! vector of previous hypotheses this hypo is built on
  const std::vector<const ChartHypothesis*> &GetPrevHypos() const {
    return m_prevHypos;
  }

  //! get a particular previous hypos
  const ChartHypothesis* GetPrevHypo(size_t pos) const {
    return m_prevHypos[pos];
  }

  //! get the constituency label that covers this hypo
  const Word &GetTargetLHS() const {
    return GetCurrTargetPhrase().GetTargetLHS();
  }

  //! get the best hypo in the arc list when doing n-best list creation. It's either this hypothesis, or the best hypo is this hypo is in the arc list
  const ChartHypothesis* GetWinningHypothesis() const {
    return m_winningHypo;
  }

  // for unordered_set in stack
  size_t hash() const;
  bool operator==(const ChartHypothesis& other) const;

  TO_STRING();

}; // class ChartHypothesis

}