00001 // -*- c++ -*- 00002 // $Id$ 00003 00004 /*********************************************************************** 00005 Moses - factored phrase-based language decoder 00006 Copyright (C) 2006 University of Edinburgh 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 ***********************************************************************/ 00022 00023 #ifndef moses_TranslationOption_h 00024 #define moses_TranslationOption_h 00025 00026 #include <map> 00027 #include <vector> 00028 #include <boost/functional/hash.hpp> 00029 #include "Bitmap.h" 00030 #include "Range.h" 00031 #include "Phrase.h" 00032 #include "TargetPhrase.h" 00033 #include "Hypothesis.h" 00034 #include "Util.h" 00035 #include "TypeDef.h" 00036 #include "ScoreComponentCollection.h" 00037 #include "StaticData.h" 00038 namespace Moses 00039 { 00040 00041 class PhraseDictionary; 00042 class GenerationDictionary; 00043 class FeatureFunction; 00044 class LexicalReordering; 00045 00046 00062 class TranslationOption 00063 { 00064 friend std::ostream& operator<<(std::ostream& out, const TranslationOption& possibleTranslation); 00065 00066 protected: 00067 00068 TargetPhrase m_targetPhrase; /*< output phrase when using this translation option */ 00069 const InputPath *m_inputPath; 00070 const Range m_sourceWordsRange; /*< word position in the input that are covered by this translation option */ 00071 float m_futureScore; /*< estimate of total cost when using this translation option, includes language model probabilities */ 00072 00073 // typedef std::map<const LexicalReordering*, Scores> _ScoreCacheMap; 00074 // _ScoreCacheMap m_lexReorderingScores; 00075 // m_lexReorderingScores was moved to TargetPhrase.h so that phrase tables 00076 // can add information (such as lexical reordering scores) to target phrases 00077 // during lookup. 00078 00079 public: 00080 struct Better { 00081 bool operator()(TranslationOption const& a, TranslationOption const& b) const { 00082 return a.GetFutureScore() > b.GetFutureScore(); 00083 } 00084 00085 bool operator()(TranslationOption const* a, TranslationOption const* b) const { 00086 return a->GetFutureScore() > b->GetFutureScore(); 00087 } 00088 }; 00089 00090 00091 explicit TranslationOption(); // For initial hypo that does translate nothing 00092 00094 TranslationOption(const Range &range 00095 , const TargetPhrase &targetPhrase); 00096 00098 bool IsCompatible(const Phrase& phrase, const std::vector<FactorType>& featuresToCheck) const; 00099 00101 inline const TargetPhrase &GetTargetPhrase() const { 00102 return m_targetPhrase; 00103 } 00104 00106 inline const Range &GetSourceWordsRange() const { 00107 return m_sourceWordsRange; 00108 } 00109 00111 const InputPath &GetInputPath() const; 00112 00113 void SetInputPath(const InputPath &inputPath); 00114 00116 bool Overlap(const Hypothesis &hypothesis) const; 00117 00119 inline size_t GetStartPos() const { 00120 return m_sourceWordsRange.GetStartPos(); 00121 } 00122 00124 inline size_t GetEndPos() const { 00125 return m_sourceWordsRange.GetEndPos(); 00126 } 00127 00129 inline size_t GetSize() const { 00130 return m_sourceWordsRange.GetEndPos() - m_sourceWordsRange.GetStartPos() + 1; 00131 } 00132 00134 inline float GetFutureScore() const { 00135 return m_futureScore; 00136 } 00137 00139 inline bool IsDeletionOption() const { 00140 return m_targetPhrase.GetSize() == 0; 00141 } 00142 00144 inline const ScoreComponentCollection &GetScoreBreakdown() const { 00145 return m_targetPhrase.GetScoreBreakdown(); 00146 } 00147 00148 inline ScoreComponentCollection &GetScoreBreakdown() { 00149 return m_targetPhrase.GetScoreBreakdown(); 00150 } 00151 00152 void EvaluateWithSourceContext(const InputType &input); 00153 00154 void UpdateScore(ScoreComponentCollection *futureScoreBreakdown = NULL) { 00155 m_targetPhrase.UpdateScore(futureScoreBreakdown); 00156 } 00157 00159 // inline 00160 const Scores* 00161 GetLexReorderingScores(const LexicalReordering *scoreProducer) const; 00162 // { 00163 // return m_targetPhrase.GetExtraScores(scoreProducer); 00164 // } 00165 00166 void CacheLexReorderingScores(const LexicalReordering &scoreProducer, 00167 const Scores &score); 00168 00169 TO_STRING(); 00170 00171 bool operator== (const TranslationOption &rhs) const { 00172 return m_sourceWordsRange == rhs.m_sourceWordsRange && 00173 m_targetPhrase == rhs.m_targetPhrase; 00174 } 00175 00176 }; 00177 00178 00179 //XXX: This doesn't look at the alignment. Is this correct? 00180 inline size_t hash_value(const TranslationOption& translationOption) 00181 { 00182 size_t seed = 0; 00183 boost::hash_combine(seed, translationOption.GetTargetPhrase()); 00184 boost::hash_combine(seed, translationOption.GetStartPos()); 00185 boost::hash_combine(seed, translationOption.GetEndPos()); 00186 return seed; 00187 } 00188 00189 00190 } 00191 00192 #endif 00193 00194