00001 // vim:tabstop=2 00002 /*********************************************************************** 00003 Moses - factored phrase-based language decoder 00004 Copyright (C) 2010 Hieu Hoang 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 ***********************************************************************/ 00020 00021 #pragma once 00022 00023 #include <vector> 00024 #include <boost/scoped_ptr.hpp> 00025 #include "Util.h" 00026 #include "Range.h" 00027 #include "ScoreComponentCollection.h" 00028 #include "Phrase.h" 00029 #include "ChartTranslationOptions.h" 00030 #include "ObjectPool.h" 00031 00032 namespace Moses 00033 { 00034 00035 class ChartKBestExtractor; 00036 class ChartHypothesis; 00037 class ChartManager; 00038 class RuleCubeItem; 00039 class FFState; 00040 00041 typedef std::vector<ChartHypothesis*> ChartArcList; 00042 00046 class ChartHypothesis 00047 { 00048 friend std::ostream& operator<<(std::ostream&, const ChartHypothesis&); 00049 // friend class ChartKBestExtractor; 00050 00051 protected: 00052 00053 boost::shared_ptr<ChartTranslationOption> m_transOpt; 00054 00055 Range m_currSourceWordsRange; 00056 std::vector<const FFState*> m_ffStates; 00058 mutable boost::scoped_ptr<ScoreComponentCollection> m_scoreBreakdown; 00059 mutable boost::scoped_ptr<ScoreComponentCollection> m_deltaScoreBreakdown; 00060 ScoreComponentCollection m_currScoreBreakdown 00061 ,m_lmNGram 00062 ,m_lmPrefix; 00063 float m_totalScore; 00064 00065 ChartArcList *m_arcList; 00066 const ChartHypothesis *m_winningHypo; 00067 00068 std::vector<const ChartHypothesis*> m_prevHypos; // always sorted by source position? 00069 00070 ChartManager& m_manager; 00071 00072 unsigned m_id; /* pkoehn wants to log the order in which hypotheses were generated */ 00073 00075 ChartHypothesis(); 00076 00078 ChartHypothesis(const ChartHypothesis ©); 00079 00080 public: 00081 ChartHypothesis(const ChartTranslationOptions &, const RuleCubeItem &item, 00082 ChartManager &manager); 00083 00085 ChartHypothesis(const ChartHypothesis &, const ChartKBestExtractor &); 00086 00087 ~ChartHypothesis(); 00088 00089 unsigned GetId() const { 00090 return m_id; 00091 } 00092 00093 const ChartTranslationOption &GetTranslationOption() const { 00094 return *m_transOpt; 00095 } 00096 00098 const TargetPhrase &GetCurrTargetPhrase() const { 00099 return m_transOpt->GetPhrase(); 00100 } 00101 00103 const Range &GetCurrSourceRange() const { 00104 return m_currSourceWordsRange; 00105 } 00106 00108 inline const ChartArcList* GetArcList() const { 00109 return m_arcList; 00110 } 00111 00113 inline const FFState* GetFFState( size_t featureID ) const { 00114 return m_ffStates[ featureID ]; 00115 } 00116 00118 inline const ChartManager& GetManager() const { 00119 return m_manager; 00120 } 00121 00122 void GetOutputPhrase(Phrase &outPhrase) const; 00123 Phrase GetOutputPhrase() const; 00124 00125 // get leftmost/rightmost words only 00126 // leftRightMost: 1=left, 2=right 00127 void GetOutputPhrase(size_t leftRightMost, size_t numWords, Phrase &outPhrase) const; 00128 00129 void EvaluateWhenApplied(); 00130 00131 void AddArc(ChartHypothesis *loserHypo); 00132 void CleanupArcList(); 00133 void SetWinningHypo(const ChartHypothesis *hypo); 00134 00136 const ScoreComponentCollection &GetScoreBreakdown() const { 00137 // Note: never call this method before m_currScoreBreakdown is fully computed 00138 if (!m_scoreBreakdown.get()) { 00139 m_scoreBreakdown.reset(new ScoreComponentCollection()); 00140 // score breakdown from current translation rule 00141 if (m_transOpt) { 00142 m_scoreBreakdown->PlusEquals(GetTranslationOption().GetScores()); 00143 } 00144 m_scoreBreakdown->PlusEquals(m_currScoreBreakdown); 00145 // score breakdowns from prev hypos 00146 for (std::vector<const ChartHypothesis*>::const_iterator iter = m_prevHypos.begin(); iter != m_prevHypos.end(); ++iter) { 00147 const ChartHypothesis &prevHypo = **iter; 00148 m_scoreBreakdown->PlusEquals(prevHypo.GetScoreBreakdown()); 00149 } 00150 } 00151 return *(m_scoreBreakdown.get()); 00152 } 00153 00155 const ScoreComponentCollection &GetDeltaScoreBreakdown() const { 00156 // Note: never call this method before m_currScoreBreakdown is fully computed 00157 if (!m_deltaScoreBreakdown.get()) { 00158 m_deltaScoreBreakdown.reset(new ScoreComponentCollection()); 00159 // score breakdown from current translation rule 00160 if (m_transOpt) { 00161 m_deltaScoreBreakdown->PlusEquals(GetTranslationOption().GetScores()); 00162 } 00163 m_deltaScoreBreakdown->PlusEquals(m_currScoreBreakdown); 00164 // delta: score breakdowns from prev hypos _not_ added 00165 } 00166 return *(m_deltaScoreBreakdown.get()); 00167 } 00168 00170 float GetFutureScore() const { 00171 // scores from current translation rule. eg. translation models & word penalty 00172 return m_totalScore; 00173 } 00174 00176 const std::vector<const ChartHypothesis*> &GetPrevHypos() const { 00177 return m_prevHypos; 00178 } 00179 00181 const ChartHypothesis* GetPrevHypo(size_t pos) const { 00182 return m_prevHypos[pos]; 00183 } 00184 00186 const Word &GetTargetLHS() const { 00187 return GetCurrTargetPhrase().GetTargetLHS(); 00188 } 00189 00191 const ChartHypothesis* GetWinningHypothesis() const { 00192 return m_winningHypo; 00193 } 00194 00195 // for unordered_set in stack 00196 size_t hash() const; 00197 bool operator==(const ChartHypothesis& other) const; 00198 00199 TO_STRING(); 00200 00201 }; // class ChartHypothesis 00202 00203 } 00204