00001 // -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*- 00002 /*********************************************************************** 00003 Moses - factored phrase-based language decoder 00004 Copyright (C) 2006 University of Edinburgh 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 <iostream> 00024 #include <vector> 00025 #include <limits> 00026 #include "Hypothesis.h" 00027 #include "TypeDef.h" 00028 #include <boost/shared_ptr.hpp> 00029 00030 namespace Moses 00031 { 00032 00033 class TrellisPathCollection; 00034 class TrellisPathList; 00035 00043 class TrellisPath 00044 { 00045 friend std::ostream& operator<<(std::ostream&, const TrellisPath&); 00046 friend class Manager; 00047 00048 protected: 00049 std::vector<const Hypothesis *> m_path; //< list of hypotheses/arcs 00050 size_t m_prevEdgeChanged; 00055 float m_totalScore; 00056 mutable boost::shared_ptr<ScoreComponentCollection> m_scoreBreakdown; 00057 00058 //Used by Manager::LatticeSample() 00059 explicit TrellisPath(const std::vector<const Hypothesis*> edges); 00060 00061 void InitTotalScore(); 00062 00063 Manager const& manager() const { 00064 UTIL_THROW_IF2(m_path.size() == 0, "zero-length trellis path"); 00065 return m_path[0]->GetManager(); 00066 } 00067 00068 public: 00069 TrellisPath(); // not implemented 00070 00072 TrellisPath(const Hypothesis *hypo); 00073 00077 TrellisPath(const TrellisPath ©, size_t edgeIndex, const Hypothesis *arc); 00078 00080 inline float GetFutureScore() const { 00081 return m_totalScore; 00082 } 00083 00087 inline const std::vector<const Hypothesis *> &GetEdges() const { 00088 return m_path; 00089 } 00090 00091 inline size_t GetSize() const { 00092 return m_path.size(); 00093 } 00094 00096 void CreateDeviantPaths(TrellisPathCollection &pathColl) const; 00097 00099 void CreateDeviantPaths(TrellisPathList &pathColl) const; 00100 00101 const boost::shared_ptr<ScoreComponentCollection> GetScoreBreakdown() const; 00102 00104 Range GetTargetWordsRange(const Hypothesis &hypo) const; 00105 00106 Phrase GetTargetPhrase() const; 00107 Phrase GetSurfacePhrase() const; 00108 00109 TO_STRING(); 00110 00111 }; 00112 00113 // friend 00114 inline std::ostream& operator<<(std::ostream& out, const TrellisPath& path) 00115 { 00116 const size_t sizePath = path.m_path.size(); 00117 for (int pos = (int) sizePath - 1 ; pos >= 0 ; pos--) { 00118 const Hypothesis *edge = path.m_path[pos]; 00119 const Range &sourceRange = edge->GetCurrSourceWordsRange(); 00120 out << edge->GetId() << " " << sourceRange.GetStartPos() << "-" << sourceRange.GetEndPos() << ", "; 00121 } 00122 // scores 00123 out << " total=" << path.GetFutureScore() 00124 << " " << path.GetScoreBreakdown() 00125 << std::endl; 00126 00127 return out; 00128 } 00129 00130 } 00131