From 83e2406f4250f255fe682bbe610abed6fe2f8584 Mon Sep 17 00:00:00 2001 From: ales-t Date: Tue, 8 Feb 2011 17:15:50 +0000 Subject: [PATCH] Word alignment output also works with MBR decoding. git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@3875 1f5c12ca-751b-0410-a591-d2e778427230 --- moses-cmd/src/IOWrapper.cpp | 77 ++++++++++++++++++++++--------------- moses-cmd/src/IOWrapper.h | 4 +- moses-cmd/src/Main.cpp | 6 ++- 3 files changed, 53 insertions(+), 34 deletions(-) diff --git a/moses-cmd/src/IOWrapper.cpp b/moses-cmd/src/IOWrapper.cpp index 4d84a569f..e44ff8065 100755 --- a/moses-cmd/src/IOWrapper.cpp +++ b/moses-cmd/src/IOWrapper.cpp @@ -223,40 +223,55 @@ void OutputSurface(std::ostream &out, const Phrase &phrase, const std::vector &edges) +{ + size_t targetOffset = 0; + + for (int currEdge = (int)edges.size() - 1 ; currEdge >= 0 ; currEdge--) + { + const Hypothesis &edge = *edges[currEdge]; + const TargetPhrase &tp = edge.GetCurrTargetPhrase(); + size_t sourceOffset = edge.GetCurrSourceWordsRange().GetStartPos(); + AlignmentInfo::const_iterator it; + for (it = tp.GetAlignmentInfo().begin(); it != tp.GetAlignmentInfo().end(); ++it) + { + *alignmentStream << it->first + sourceOffset << "-" << it->second + targetOffset << " "; + } + targetOffset += tp.GetSize(); + } + *alignmentStream << std::endl; +} + +void OutputAlignment(std::ofstream *alignmentStream, const Hypothesis *hypo) +{ + if (hypo && ! StaticData::Instance().GetAlignmentOutputFile().empty() && alignmentStream) + { + std::vector edges; + const Hypothesis *currentHypo = hypo; + while (currentHypo) + { + edges.push_back(currentHypo); + currentHypo = currentHypo->GetPrevHypo(); + } + + OutputAlignment(alignmentStream, edges); + } +} + +void OutputAlignment(std::ofstream *alignmentStream, const TrellisPath &path) +{ + if (! StaticData::Instance().GetAlignmentOutputFile().empty() && alignmentStream) + { + OutputAlignment(alignmentStream, path.GetEdges()); + } +} + void OutputSurface(std::ostream &out, const Hypothesis *hypo, const std::vector &outputFactorOrder - ,bool reportSegmentation, bool reportAllFactors, std::ofstream *alignmentStream) + ,bool reportSegmentation, bool reportAllFactors) { if ( hypo != NULL) { - if (! StaticData::Instance().GetAlignmentOutputFile().empty() && alignmentStream) - { - size_t targetOffset = 0; - - std::stack edges; - const Hypothesis *currentHypo = hypo; - while (currentHypo) - { - edges.push(currentHypo); - currentHypo = currentHypo->GetPrevHypo(); - } - - while (!edges.empty()) - { - const Hypothesis &edge = *edges.top(); - edges.pop(); - const TargetPhrase &tp = edge.GetCurrTargetPhrase(); - size_t sourceOffset = edge.GetCurrSourceWordsRange().GetStartPos(); - AlignmentInfo::const_iterator it; - for (it = tp.GetAlignmentInfo().begin(); it != tp.GetAlignmentInfo().end(); ++it) - { - *alignmentStream << it->first + sourceOffset << "-" << it->second + targetOffset << " "; - } - targetOffset += tp.GetSize(); - } - *alignmentStream << std::endl; - } - - OutputSurface(out, hypo->GetPrevHypo(), outputFactorOrder, reportSegmentation, reportAllFactors, NULL); + OutputSurface(out, hypo->GetPrevHypo(), outputFactorOrder, reportSegmentation, reportAllFactors); OutputSurface(out, hypo->GetCurrTargetPhrase(), outputFactorOrder, reportAllFactors); if (reportSegmentation == true @@ -338,7 +353,7 @@ void IOWrapper::OutputBestHypo(const Hypothesis *hypo, long /*translationId*/, b OutputInput(cout, hypo); cout << "||| "; } - OutputSurface(cout, hypo, m_outputFactorOrder, reportSegmentation, reportAllFactors, NULL); + OutputSurface(cout, hypo, m_outputFactorOrder, reportSegmentation, reportAllFactors); cout << endl; } } diff --git a/moses-cmd/src/IOWrapper.h b/moses-cmd/src/IOWrapper.h index 32ddf52f7..59d122b84 100755 --- a/moses-cmd/src/IOWrapper.h +++ b/moses-cmd/src/IOWrapper.h @@ -120,7 +120,7 @@ public: IOWrapper *GetIODevice(const Moses::StaticData &staticData); bool ReadInput(IOWrapper &ioWrapper, Moses::InputTypeEnum inputType, Moses::InputType*& source); -void OutputSurface(std::ostream &out, const Moses::Hypothesis *hypo, const std::vector &outputFactorOrder ,bool reportSegmentation, bool reportAllFactors, std::ofstream *alignmentStream); +void OutputSurface(std::ostream &out, const Moses::Hypothesis *hypo, const std::vector &outputFactorOrder ,bool reportSegmentation, bool reportAllFactors); void OutputNBest(std::ostream& out, const Moses::TrellisPathList &nBestList, const std::vector&, const TranslationSystem* system, long translationId); void OutputLatticeMBRNBest(std::ostream& out, const std::vector& solutions,long translationId); @@ -128,5 +128,7 @@ void OutputBestHypo(const std::vector& mbrBestHypo, long /*transla bool reportSegmentation, bool reportAllFactors, std::ostream& out); void OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/,bool reportSegmentation, bool reportAllFactors, std::ostream &out); void OutputInput(std::ostream& os, const Hypothesis* hypo); +void OutputAlignment(std::ofstream *alignmentStream, const Hypothesis *hypo); +void OutputAlignment(std::ofstream *alignmentStream, const TrellisPath &path); #endif diff --git a/moses-cmd/src/Main.cpp b/moses-cmd/src/Main.cpp index a360c9ff3..70b4c245c 100644 --- a/moses-cmd/src/Main.cpp +++ b/moses-cmd/src/Main.cpp @@ -139,8 +139,8 @@ class TranslationTask : public Task { bestHypo, staticData.GetOutputFactorOrder(), staticData.GetReportSegmentation(), - staticData.GetReportAllFactors(), - m_alignmentStream); + staticData.GetReportAllFactors()); + OutputAlignment(m_alignmentStream, bestHypo); IFVERBOSE(1) { debug << "BEST TRANSLATION: " << *bestHypo << endl; } @@ -186,6 +186,7 @@ class TranslationTask : public Task { OutputBestHypo(conBestHypo, m_lineNumber, staticData.GetReportSegmentation(), staticData.GetReportAllFactors(),out); + OutputAlignment(m_alignmentStream, conBestHypo); IFVERBOSE(2) { PrintUserTime("finished Consensus decoding"); } } else @@ -195,6 +196,7 @@ class TranslationTask : public Task { OutputBestHypo(mbrBestHypo, m_lineNumber, staticData.GetReportSegmentation(), staticData.GetReportAllFactors(),out); + OutputAlignment(m_alignmentStream, mbrBestHypo); IFVERBOSE(2) { PrintUserTime("finished MBR decoding"); } }