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 "Util.h" 00025 #include "WordsRange.h" 00026 #include "ScoreComponentCollection.h" 00027 #include "Phrase.h" 00028 #include "ChartTranslationOptions.h" 00029 #include "ObjectPool.h" 00030 00031 namespace Moses 00032 { 00033 00034 class ChartHypothesis; 00035 class ChartManager; 00036 class RuleCubeItem; 00037 00038 typedef std::vector<ChartHypothesis*> ChartArcList; 00039 00043 class ChartHypothesis 00044 { 00045 friend std::ostream& operator<<(std::ostream&, const ChartHypothesis&); 00046 00047 protected: 00048 #ifdef USE_HYPO_POOL 00049 static ObjectPool<ChartHypothesis> s_objectPool; 00050 #endif 00051 00052 const TargetPhrase &m_targetPhrase; 00053 00054 WordsRange m_currSourceWordsRange; 00055 std::vector<const FFState*> m_ffStates; 00056 ScoreComponentCollection m_scoreBreakdown 00057 ,m_lmNGram 00058 ,m_lmPrefix; 00059 float m_totalScore; 00060 00061 ChartArcList *m_arcList; 00062 const ChartHypothesis *m_winningHypo; 00063 00064 std::vector<const ChartHypothesis*> m_prevHypos; // always sorted by source position? 00065 00066 ChartManager& m_manager; 00067 00068 unsigned m_id; /* pkoehn wants to log the order in which hypotheses were generated */ 00069 00071 ChartHypothesis(); 00072 00074 ChartHypothesis(const ChartHypothesis ©); 00075 00076 public: 00077 #ifdef USE_HYPO_POOL 00078 void *operator new(size_t /* num_bytes */) { 00079 void *ptr = s_objectPool.getPtr(); 00080 return ptr; 00081 } 00082 00084 static void Delete(ChartHypothesis *hypo) { 00085 s_objectPool.freeObject(hypo); 00086 } 00087 #else 00089 static void Delete(ChartHypothesis *hypo) { 00090 delete hypo; 00091 } 00092 #endif 00093 00094 ChartHypothesis(const ChartTranslationOptions &, const RuleCubeItem &item, 00095 ChartManager &manager); 00096 00097 ~ChartHypothesis(); 00098 00099 unsigned GetId() const { return m_id; } 00100 00102 const TargetPhrase &GetCurrTargetPhrase()const { 00103 return m_targetPhrase; 00104 } 00105 00107 const WordsRange &GetCurrSourceRange()const { 00108 return m_currSourceWordsRange; 00109 } 00110 00112 inline const ChartArcList* GetArcList() const { 00113 return m_arcList; 00114 } 00115 00117 inline const FFState* GetFFState( size_t featureID ) const { 00118 return m_ffStates[ featureID ]; 00119 } 00120 00122 inline const ChartManager& GetManager() const { return m_manager; } 00123 00124 void CreateOutputPhrase(Phrase &outPhrase) const; 00125 Phrase GetOutputPhrase() const; 00126 00127 int RecombineCompare(const ChartHypothesis &compare) const; 00128 00129 void CalcScore(); 00130 00131 void AddArc(ChartHypothesis *loserHypo); 00132 void CleanupArcList(); 00133 void SetWinningHypo(const ChartHypothesis *hypo); 00134 00136 const ScoreComponentCollection &GetScoreBreakdown() const 00137 { return m_scoreBreakdown; } 00138 00140 float GetTotalScore() const 00141 { return m_totalScore; } 00142 00144 const std::vector<const ChartHypothesis*> &GetPrevHypos() const 00145 { return m_prevHypos; } 00146 00148 const ChartHypothesis* GetPrevHypo(size_t pos) const { 00149 return m_prevHypos[pos]; 00150 } 00151 00153 const Word &GetTargetLHS() const { 00154 return GetCurrTargetPhrase().GetTargetLHS(); 00155 } 00156 00158 const ChartHypothesis* GetWinningHypothesis() const 00159 { return m_winningHypo; } 00160 00161 TO_STRING(); 00162 00163 }; // class ChartHypothesis 00164 00165 } 00166
1.5.9