00001 // $Id$ 00002 00003 /*********************************************************************** 00004 Moses - factored phrase-based language decoder 00005 Copyright (C) 2006 University of Edinburgh 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2.1 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 ***********************************************************************/ 00021 00022 #ifndef moses_TargetPhrase_h 00023 #define moses_TargetPhrase_h 00024 00025 #include <vector> 00026 #include "TypeDef.h" 00027 #include "Phrase.h" 00028 #include "ScoreComponentCollection.h" 00029 #include "AlignmentInfo.h" 00030 00031 #include "util/string_piece.hh" 00032 00033 #ifdef HAVE_PROTOBUF 00034 #include "rule.pb.h" 00035 #endif 00036 00037 namespace Moses 00038 { 00039 00040 class FeatureFunction; 00041 00044 class TargetPhrase: public Phrase 00045 { 00046 friend std::ostream& operator<<(std::ostream&, const TargetPhrase&); 00047 protected: 00048 float m_fullScore; 00049 ScoreComponentCollection m_scoreBreakdown; 00050 00051 // in case of confusion net, ptr to source phrase 00052 Phrase m_sourcePhrase; 00053 const AlignmentInfo* m_alignTerm, *m_alignNonTerm; 00054 const Word *m_lhsTarget; 00055 00056 public: 00057 TargetPhrase(); 00058 TargetPhrase(const TargetPhrase ©); 00059 explicit TargetPhrase(std::string out_string); 00060 explicit TargetPhrase(const Phrase &targetPhrase); 00061 ~TargetPhrase(); 00062 00063 void Evaluate(); 00064 00065 void SetSparseScore(const FeatureFunction* translationScoreProducer, const StringPiece &sparseString); 00066 00067 // used to set translation or gen score 00068 void SetXMLScore(float score); 00069 void SetInputScore(const Scores &scoreVector); 00070 00071 TargetPhrase *MergeNext(const TargetPhrase &targetPhrase) const; 00072 // used for translation step 00073 00074 #ifdef HAVE_PROTOBUF 00075 void WriteToRulePB(hgmert::Rule* pb) const; 00076 #endif 00077 00078 /*** 00079 * return the estimated score resulting from our being added to a sentence 00080 * (it's an estimate because we don't have full n-gram info for the language model 00081 * without using the (unknown) full sentence) 00082 * 00083 */ 00084 inline float GetFutureScore() const { 00085 return m_fullScore; 00086 } 00087 00088 inline const ScoreComponentCollection &GetScoreBreakdown() const 00089 { return m_scoreBreakdown; } 00090 inline ScoreComponentCollection &GetScoreBreakdown() 00091 { return m_scoreBreakdown; } 00092 00093 //TODO: Probably shouldn't copy this, but otherwise ownership is unclear 00094 void SetSourcePhrase(const Phrase& p) 00095 { 00096 m_sourcePhrase=p; 00097 } 00098 // ... but if we must store a copy, at least initialize it in-place 00099 Phrase &MutableSourcePhrase() { 00100 return m_sourcePhrase; 00101 } 00102 const Phrase& GetSourcePhrase() const 00103 { 00104 return m_sourcePhrase; 00105 } 00106 00107 void SetTargetLHS(const Word *lhs) 00108 { m_lhsTarget = lhs; } 00109 const Word &GetTargetLHS() const 00110 { return *m_lhsTarget; } 00111 00112 void SetAlignmentInfo(const StringPiece &alignString); 00113 void SetAlignTerm(const AlignmentInfo *alignTerm) { 00114 m_alignTerm = alignTerm; 00115 } 00116 void SetAlignNonTerm(const AlignmentInfo *alignNonTerm) { 00117 m_alignNonTerm = alignNonTerm; 00118 } 00119 00120 void SetAlignTerm(const AlignmentInfo::CollType &coll); 00121 void SetAlignNonTerm(const AlignmentInfo::CollType &coll); 00122 00123 const AlignmentInfo &GetAlignTerm() const 00124 { return *m_alignTerm; } 00125 const AlignmentInfo &GetAlignNonTerm() const 00126 { return *m_alignNonTerm; } 00127 00128 00129 TO_STRING(); 00130 }; 00131 00132 std::ostream& operator<<(std::ostream&, const TargetPhrase&); 00133 00137 struct TargetPhraseHasher 00138 { 00139 inline size_t operator()(const TargetPhrase& targetPhrase) const 00140 { 00141 size_t seed = 0; 00142 boost::hash_combine(seed, targetPhrase); 00143 boost::hash_combine(seed, targetPhrase.GetSourcePhrase()); 00144 boost::hash_combine(seed, targetPhrase.GetAlignTerm()); 00145 boost::hash_combine(seed, targetPhrase.GetAlignNonTerm()); 00146 00147 return seed; 00148 } 00149 }; 00150 00151 struct TargetPhraseComparator 00152 { 00153 inline bool operator()(const TargetPhrase& lhs, const TargetPhrase& rhs) const 00154 { 00155 return lhs.Compare(rhs) == 0 && 00156 lhs.GetSourcePhrase().Compare(rhs.GetSourcePhrase()) == 0 && 00157 lhs.GetAlignTerm() == rhs.GetAlignTerm() && 00158 lhs.GetAlignNonTerm() == rhs.GetAlignNonTerm(); 00159 } 00160 00161 }; 00162 00163 } 00164 00165 #endif
1.5.9