00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "TrellisPath.h"
00023 #include "TrellisPathList.h"
00024 #include "TrellisPathCollection.h"
00025 #include "StaticData.h"
00026 #include "Manager.h"
00027 using namespace std;
00028
00029 namespace Moses
00030 {
00031 TrellisPath::TrellisPath(const Hypothesis *hypo)
00032 : m_prevEdgeChanged(NOT_FOUND)
00033 {
00034 m_totalScore = hypo->GetFutureScore();
00035
00036
00037 while (hypo != NULL) {
00038 m_path.push_back(hypo);
00039 hypo = hypo->GetPrevHypo();
00040 }
00041 }
00042
00043 void TrellisPath::InitTotalScore()
00044 {
00045 m_totalScore = m_path[0]->GetWinningHypo()->GetFutureScore();
00046
00047
00048 size_t sizePath = m_path.size();
00049 for (size_t pos = 0 ; pos < sizePath ; pos++) {
00050 const Hypothesis *hypo = m_path[pos];
00051 const Hypothesis *winningHypo = hypo->GetWinningHypo();
00052 if (hypo != winningHypo) {
00053 m_totalScore += hypo->GetFutureScore() - winningHypo->GetFutureScore();
00054 }
00055 }
00056 }
00057
00058 TrellisPath::TrellisPath(const TrellisPath ©, size_t edgeIndex, const Hypothesis *arc)
00059 :m_prevEdgeChanged(edgeIndex)
00060 {
00061 m_path.reserve(copy.m_path.size());
00062 for (size_t currEdge = 0 ; currEdge < edgeIndex ; currEdge++) {
00063
00064 m_path.push_back(copy.m_path[currEdge]);
00065 }
00066
00067
00068 m_path.push_back(arc);
00069
00070
00071 const Hypothesis *prevHypo = arc->GetPrevHypo();
00072 while (prevHypo != NULL) {
00073 m_path.push_back(prevHypo);
00074 prevHypo = prevHypo->GetPrevHypo();
00075 }
00076
00077 InitTotalScore();
00078 }
00079
00080 TrellisPath::TrellisPath(const vector<const Hypothesis*> edges)
00081 :m_prevEdgeChanged(NOT_FOUND)
00082 {
00083 m_path.resize(edges.size());
00084 copy(edges.rbegin(),edges.rend(),m_path.begin());
00085 InitTotalScore();
00086 }
00087
00088
00089 void TrellisPath::CreateDeviantPaths(TrellisPathCollection &pathColl) const
00090 {
00091 const size_t sizePath = m_path.size();
00092
00093 if (m_prevEdgeChanged == NOT_FOUND) {
00094
00095 for (size_t currEdge = 0 ; currEdge < sizePath ; currEdge++) {
00096 const Hypothesis *hypo = static_cast<const Hypothesis*>(m_path[currEdge]);
00097 const ArcList *pAL = hypo->GetArcList();
00098 if (!pAL) continue;
00099 const ArcList &arcList = *pAL;
00100
00101
00102 ArcList::const_iterator iterArc;
00103 for (iterArc = arcList.begin() ; iterArc != arcList.end() ; ++iterArc) {
00104 const Hypothesis *arc = *iterArc;
00105 TrellisPath *deviantPath = new TrellisPath(*this, currEdge, arc);
00106 pathColl.Add(deviantPath);
00107 }
00108 }
00109 } else {
00110
00111 for (size_t currEdge = m_prevEdgeChanged + 1 ; currEdge < sizePath ; currEdge++) {
00112 const ArcList *pAL = m_path[currEdge]->GetArcList();
00113 if (!pAL) continue;
00114 const ArcList &arcList = *pAL;
00115 ArcList::const_iterator iterArc;
00116
00117 for (iterArc = arcList.begin() ; iterArc != arcList.end() ; ++iterArc) {
00118
00119 const Hypothesis *arcReplace = *iterArc;
00120
00121 TrellisPath *deviantPath = new TrellisPath(*this, currEdge, arcReplace);
00122 pathColl.Add(deviantPath);
00123 }
00124 }
00125 }
00126 }
00127
00128 void TrellisPath::CreateDeviantPaths(TrellisPathList &pathColl) const
00129 {
00130 const size_t sizePath = m_path.size();
00131
00132 if (m_prevEdgeChanged == NOT_FOUND) {
00133
00134 for (size_t currEdge = 0 ; currEdge < sizePath ; currEdge++) {
00135 const Hypothesis *hypo = static_cast<const Hypothesis*>(m_path[currEdge]);
00136 const ArcList *pAL = hypo->GetArcList();
00137 if (!pAL) continue;
00138 const ArcList &arcList = *pAL;
00139
00140
00141 ArcList::const_iterator iterArc;
00142 for (iterArc = arcList.begin() ; iterArc != arcList.end() ; ++iterArc) {
00143 const Hypothesis *arc = *iterArc;
00144 TrellisPath *deviantPath = new TrellisPath(*this, currEdge, arc);
00145 pathColl.Add(deviantPath);
00146 }
00147 }
00148 } else {
00149
00150 for (size_t currEdge = m_prevEdgeChanged + 1 ; currEdge < sizePath ; currEdge++) {
00151 const ArcList *pAL = m_path[currEdge]->GetArcList();
00152 if (!pAL) continue;
00153 const ArcList &arcList = *pAL;
00154 ArcList::const_iterator iterArc;
00155
00156 for (iterArc = arcList.begin() ; iterArc != arcList.end() ; ++iterArc) {
00157
00158 const Hypothesis *arcReplace = *iterArc;
00159
00160 TrellisPath *deviantPath = new TrellisPath(*this, currEdge, arcReplace);
00161 pathColl.Add(deviantPath);
00162 }
00163 }
00164 }
00165 }
00166
00167 boost::shared_ptr<ScoreComponentCollection> const
00168 TrellisPath::
00169 GetScoreBreakdown() const
00170 {
00171 if (!m_scoreBreakdown) {
00172 m_scoreBreakdown.reset(new ScoreComponentCollection());
00173 m_scoreBreakdown->PlusEquals(m_path[0]->GetWinningHypo()->GetScoreBreakdown());
00174
00175
00176
00177
00178
00179 size_t sizePath = m_path.size();
00180 for (size_t pos = 0 ; pos < sizePath ; pos++) {
00181 const Hypothesis *hypo = m_path[pos];
00182 const Hypothesis *winningHypo = hypo->GetWinningHypo();
00183 if (hypo != winningHypo) {
00184 m_scoreBreakdown->MinusEquals(winningHypo->GetScoreBreakdown());
00185 m_scoreBreakdown->PlusEquals(hypo->GetScoreBreakdown());
00186 }
00187 }
00188 }
00189
00190 return m_scoreBreakdown;
00191 }
00192
00193 Phrase TrellisPath::GetTargetPhrase() const
00194 {
00195 Phrase targetPhrase(ARRAY_SIZE_INCR);
00196
00197 int numHypo = (int) m_path.size();
00198 for (int node = numHypo - 2 ; node >= 0 ; --node) {
00199
00200 const Hypothesis &hypo = *m_path[node];
00201 const Phrase &currTargetPhrase = hypo.GetCurrTargetPhrase();
00202
00203 targetPhrase.Append(currTargetPhrase);
00204 }
00205
00206 return targetPhrase;
00207 }
00208
00209 Phrase TrellisPath::GetSurfacePhrase() const
00210 {
00211 std::vector<FactorType> const& oFactor = manager().options()->output.factor_order;
00212 Phrase targetPhrase = GetTargetPhrase();
00213 Phrase ret(targetPhrase.GetSize());
00214
00215 for (size_t pos = 0 ; pos < targetPhrase.GetSize() ; ++pos) {
00216 Word &newWord = ret.AddWord();
00217 for (size_t i = 0 ; i < oFactor.size() ; i++) {
00218 FactorType factorType = oFactor[i];
00219 const Factor *factor = targetPhrase.GetFactor(pos, factorType);
00220 UTIL_THROW_IF2(factor == NULL,
00221 "No factor " << factorType << " at position " << pos);
00222 newWord[factorType] = factor;
00223 }
00224 }
00225
00226 return ret;
00227 }
00228
00229 Range TrellisPath::GetTargetWordsRange(const Hypothesis &hypo) const
00230 {
00231 size_t startPos = 0;
00232
00233 for (int indEdge = (int) m_path.size() - 1 ; indEdge >= 0 ; --indEdge) {
00234 const Hypothesis *currHypo = m_path[indEdge];
00235 size_t endPos = startPos + currHypo->GetCurrTargetLength() - 1;
00236
00237 if (currHypo == &hypo) {
00238 return Range(startPos, endPos);
00239 }
00240 startPos = endPos + 1;
00241 }
00242
00243
00244 UTIL_THROW(util::Exception, "Hypothesis not found");
00245 return Range(NOT_FOUND, NOT_FOUND);
00246 }
00247
00248 TO_STRING_BODY(TrellisPath);
00249
00250
00251 }
00252