diff --git a/contrib/server/mosesserver.cpp b/contrib/server/mosesserver.cpp index 4d0872f24..f20bab592 100644 --- a/contrib/server/mosesserver.cpp +++ b/contrib/server/mosesserver.cpp @@ -310,7 +310,7 @@ public: } if (addWordAlignInfo) { stringstream wordAlignment; - OutputAlignment(wordAlignment, hypo); + IOWrapper::OutputAlignment(wordAlignment, hypo); vector alignments; string alignmentPair; while (wordAlignment >> alignmentPair) { @@ -472,7 +472,7 @@ public: if ((int)edges.size() > 0) { stringstream wordAlignment; - OutputAlignment(wordAlignment, edges[0]); + IOWrapper::OutputAlignment(wordAlignment, edges[0]); vector alignments; string alignmentPair; while (wordAlignment >> alignmentPair) { @@ -490,7 +490,7 @@ public: { // should the score breakdown be reported in a more structured manner? ostringstream buf; - Moses::OutputAllFeatureScores(path.GetScoreBreakdown(),buf); + IOWrapper::OutputAllFeatureScores(path.GetScoreBreakdown(),buf); nBestXMLItem["fvals"] = xmlrpc_c::value_string(buf.str()); } diff --git a/moses-cmd/LatticeMBRGrid.cpp b/moses-cmd/LatticeMBRGrid.cpp index bbf5f2a8b..f00f40fd0 100644 --- a/moses-cmd/LatticeMBRGrid.cpp +++ b/moses-cmd/LatticeMBRGrid.cpp @@ -177,7 +177,7 @@ int main(int argc, char* argv[]) const vector& prune_grid = grid.getGrid(lmbr_prune); const vector& scale_grid = grid.getGrid(lmbr_scale); - while(ioWrapper->ReadInput(*ioWrapper,staticData.GetInputType(),source)) { + while(ioWrapper->ReadInput(staticData.GetInputType(),source)) { ++lineCount; source->SetTranslationId(lineCount); @@ -200,7 +200,7 @@ int main(int argc, char* argv[]) staticData.SetMBRScale(scale); cout << lineCount << " ||| " << p << " " << r << " " << prune << " " << scale << " ||| "; vector mbrBestHypo = doLatticeMBR(manager,nBestList); - OutputBestHypo(mbrBestHypo, lineCount, staticData.GetReportSegmentation(), + ioWrapper->OutputBestHypo(mbrBestHypo, lineCount, staticData.GetReportSegmentation(), staticData.GetReportAllFactors(),cout); } } diff --git a/moses-cmd/Main.cpp b/moses-cmd/Main.cpp index 5758103f3..7e8a4a0e3 100644 --- a/moses-cmd/Main.cpp +++ b/moses-cmd/Main.cpp @@ -148,7 +148,7 @@ int main(int argc, char** argv) // main loop over set of input sentences InputType* source = NULL; size_t lineCount = staticData.GetStartTranslationId(); - while(ioWrapper->ReadInput(*ioWrapper,staticData.GetInputType(),source)) { + while(ioWrapper->ReadInput(staticData.GetInputType(),source)) { source->SetTranslationId(lineCount); IFVERBOSE(1) { ResetUserTime(); diff --git a/moses/IOWrapper.cpp b/moses/IOWrapper.cpp index f59fd8f4d..e96fd5445 100644 --- a/moses/IOWrapper.cpp +++ b/moses/IOWrapper.cpp @@ -242,7 +242,7 @@ void IOWrapper::FixPrecision(std::ostream &stream, size_t size) stream.precision(size); } -std::map GetPlaceholders(const Hypothesis &hypo, FactorType placeholderFactor) +std::map IOWrapper::GetPlaceholders(const Hypothesis &hypo, FactorType placeholderFactor) { const InputPath &inputPath = hypo.GetTranslationOption().GetInputPath(); const Phrase &inputPhrase = inputPath.GetPhrase(); @@ -973,7 +973,7 @@ size_t IOWrapper::OutputAlignmentNBest( /*** * print surface factor only for the given phrase */ -void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector &outputFactorOrder, +void IOWrapper::OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector &outputFactorOrder, char reportSegmentation, bool reportAllFactors) { UTIL_THROW_IF2(outputFactorOrder.size() == 0, @@ -1046,7 +1046,7 @@ void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector< } } -void OutputBestSurface(std::ostream &out, const Hypothesis *hypo, const std::vector &outputFactorOrder, +void IOWrapper::OutputBestSurface(std::ostream &out, const Hypothesis *hypo, const std::vector &outputFactorOrder, char reportSegmentation, bool reportAllFactors) { if (hypo != NULL) { @@ -1056,7 +1056,7 @@ void OutputBestSurface(std::ostream &out, const Hypothesis *hypo, const std::vec } } -void OutputAlignment(ostream &out, const AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset) +void IOWrapper::OutputAlignment(ostream &out, const AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset) { typedef std::vector< const std::pair* > AlignVec; AlignVec alignments = ai.GetSortedAlignments(); @@ -1069,7 +1069,7 @@ void OutputAlignment(ostream &out, const AlignmentInfo &ai, size_t sourceOffset, } -void OutputAlignment(ostream &out, const vector &edges) +void IOWrapper::OutputAlignment(ostream &out, const vector &edges) { size_t targetOffset = 0; @@ -1084,7 +1084,7 @@ void OutputAlignment(ostream &out, const vector &edges) } } -void OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo) +void IOWrapper::OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo) { std::vector edges; const Hypothesis *currentHypo = hypo; @@ -1097,7 +1097,7 @@ void OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo) } -void OutputAlignment(OutputCollector* collector, size_t lineNo , const vector &edges) +void IOWrapper::OutputAlignment(OutputCollector* collector, size_t lineNo , const vector &edges) { ostringstream out; OutputAlignment(out, edges); @@ -1105,7 +1105,7 @@ void OutputAlignment(OutputCollector* collector, size_t lineNo , const vectorWrite(lineNo,out.str()); } -void OutputAlignment(OutputCollector* collector, size_t lineNo , const Hypothesis *hypo) +void IOWrapper::OutputAlignment(OutputCollector* collector, size_t lineNo , const Hypothesis *hypo) { if (collector) { std::vector edges; @@ -1119,14 +1119,14 @@ void OutputAlignment(OutputCollector* collector, size_t lineNo , const Hypothesi } } -void OutputAlignment(OutputCollector* collector, size_t lineNo , const TrellisPath &path) +void IOWrapper::OutputAlignment(OutputCollector* collector, size_t lineNo , const TrellisPath &path) { if (collector) { OutputAlignment(collector,lineNo, path.GetEdges()); } } -void OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/, char reportSegmentation, bool reportAllFactors, std::ostream &out) +void IOWrapper::OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/, char reportSegmentation, bool reportAllFactors, std::ostream &out) { const std::vector &edges = path.GetEdges(); @@ -1146,7 +1146,7 @@ void IOWrapper::Backtrack(const Hypothesis *hypo) } } -void OutputBestHypo(const std::vector& mbrBestHypo, long /*translationId*/, char /*reportSegmentation*/, bool /*reportAllFactors*/, ostream& out) +void IOWrapper::OutputBestHypo(const std::vector& mbrBestHypo, long /*translationId*/, char /*reportSegmentation*/, bool /*reportAllFactors*/, ostream& out) { for (size_t i = 0 ; i < mbrBestHypo.size() ; i++) { @@ -1160,7 +1160,7 @@ void OutputBestHypo(const std::vector& mbrBestHypo, long /*translationId* } -void OutputInput(std::vector& map, const Hypothesis* hypo) +void IOWrapper::OutputInput(std::vector& map, const Hypothesis* hypo) { if (hypo->GetPrevHypo()) { OutputInput(map, hypo->GetPrevHypo()); @@ -1168,7 +1168,7 @@ void OutputInput(std::vector& map, const Hypothesis* hypo) } } -void OutputInput(std::ostream& os, const Hypothesis* hypo) +void IOWrapper::OutputInput(std::ostream& os, const Hypothesis* hypo) { size_t len = hypo->GetInput().GetSize(); std::vector inp_phrases(len, 0); @@ -1204,21 +1204,21 @@ void IOWrapper::OutputBestHypo(const Hypothesis *hypo, long /*translationId*/, c } } -bool IOWrapper::ReadInput(IOWrapper &ioWrapper, InputTypeEnum inputType, InputType*& source) +bool IOWrapper::ReadInput(InputTypeEnum inputType, InputType*& source) { delete source; switch(inputType) { case SentenceInput: - source = ioWrapper.GetInput(new Sentence); + source = GetInput(new Sentence); break; case ConfusionNetworkInput: - source = ioWrapper.GetInput(new ConfusionNet); + source = GetInput(new ConfusionNet); break; case WordLatticeInput: - source = ioWrapper.GetInput(new WordLattice); + source = GetInput(new WordLattice); break; case TreeInputType: - source = ioWrapper.GetInput(new TreeInput); + source = GetInput(new TreeInput); break; default: TRACE_ERR("Unknown input type: " << inputType << "\n"); @@ -1226,7 +1226,7 @@ bool IOWrapper::ReadInput(IOWrapper &ioWrapper, InputTypeEnum inputType, InputTy return (source ? true : false); } -void OutputNBest(std::ostream& out +void IOWrapper::OutputNBest(std::ostream& out , const Moses::TrellisPathList &nBestList , const std::vector& outputFactorOrder , long translationId @@ -1300,7 +1300,7 @@ void OutputNBest(std::ostream& out out << std::flush; } -void OutputAllFeatureScores(const Moses::ScoreComponentCollection &features +void IOWrapper::OutputAllFeatureScores(const Moses::ScoreComponentCollection &features , std::ostream &out) { std::string lastName = ""; @@ -1321,7 +1321,7 @@ void OutputAllFeatureScores(const Moses::ScoreComponentCollection &features } } -void OutputFeatureScores( std::ostream& out +void IOWrapper::OutputFeatureScores( std::ostream& out , const ScoreComponentCollection &features , const FeatureFunction *ff , std::string &lastName ) @@ -1348,7 +1348,7 @@ void OutputFeatureScores( std::ostream& out } } -void OutputLatticeMBRNBest(std::ostream& out, const vector& solutions,long translationId) +void IOWrapper::OutputLatticeMBRNBest(std::ostream& out, const vector& solutions,long translationId) { for (vector::const_iterator si = solutions.begin(); si != solutions.end(); ++si) { out << translationId; diff --git a/moses/IOWrapper.h b/moses/IOWrapper.h index 92e08371e..43b654f22 100644 --- a/moses/IOWrapper.h +++ b/moses/IOWrapper.h @@ -32,8 +32,7 @@ POSSIBILITY OF SUCH DAMAGE. // example file on how to use moses library -#ifndef moses_cmd_IOWrapper_h -#define moses_cmd_IOWrapper_h +#pragma once #include #include @@ -125,6 +124,8 @@ protected: long translationId); void OutputSurface(std::ostream &out, const Phrase &phrase, const std::vector &outputFactorOrder, bool reportAllFactors); + void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector &outputFactorOrder, + char reportSegmentation, bool reportAllFactors); size_t OutputAlignment(Alignments &retAlign, const Moses::ChartHypothesis *hypo, size_t startTarget); size_t OutputAlignmentNBest(Alignments &retAlign, @@ -160,7 +161,7 @@ public: ~IOWrapper(); Moses::InputType* GetInput(Moses::InputType *inputType); - bool ReadInput(IOWrapper &ioWrapper, Moses::InputTypeEnum inputType, Moses::InputType*& source); + bool ReadInput(Moses::InputTypeEnum inputType, Moses::InputType*& source); void OutputBestHypo(const Moses::Hypothesis *hypo, long translationId, char reportSegmentation, bool reportAllFactors); void OutputLatticeMBRNBestList(const std::vector& solutions,long translationId); @@ -218,35 +219,42 @@ public: const Moses::Sentence &sentence, long translationId); + // phrase-based + void OutputBestSurface(std::ostream &out, const Moses::Hypothesis *hypo, const std::vector &outputFactorOrder, char reportSegmentation, bool reportAllFactors); + void OutputLatticeMBRNBest(std::ostream& out, const std::vector& solutions,long translationId); + void OutputBestHypo(const std::vector& mbrBestHypo, long /*translationId*/, + char reportSegmentation, bool reportAllFactors, std::ostream& out); + void OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/,char reportSegmentation, bool reportAllFactors, std::ostream &out); + void OutputInput(std::ostream& os, const Moses::Hypothesis* hypo); + void OutputInput(std::vector& map, const Hypothesis* hypo); + + void OutputAlignment(Moses::OutputCollector* collector, size_t lineNo, const Moses::Hypothesis *hypo); + void OutputAlignment(Moses::OutputCollector* collector, size_t lineNo, const Moses::TrellisPath &path); + void OutputAlignment(OutputCollector* collector, size_t lineNo , const std::vector &edges); + + static void OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo); + static void OutputAlignment(std::ostream &out, const std::vector &edges); + static void OutputAlignment(std::ostream &out, const Moses::AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset); + + void OutputNBest(std::ostream& out + , const Moses::TrellisPathList &nBestList + , const std::vector& outputFactorOrder + , long translationId + , char reportSegmentation); + + static void OutputAllFeatureScores(const Moses::ScoreComponentCollection &features + , std::ostream &out); + static void OutputFeatureScores( std::ostream& out + , const Moses::ScoreComponentCollection &features + , const Moses::FeatureFunction *ff + , std::string &lastName ); + + // creates a map of TARGET positions which should be replaced by word using placeholder + std::map GetPlaceholders(const Moses::Hypothesis &hypo, Moses::FactorType placeholderFactor); + }; -void OutputBestSurface(std::ostream &out, const Moses::Hypothesis *hypo, const std::vector &outputFactorOrder, char reportSegmentation, bool reportAllFactors); -void OutputLatticeMBRNBest(std::ostream& out, const std::vector& solutions,long translationId); -void OutputBestHypo(const std::vector& mbrBestHypo, long /*translationId*/, - char reportSegmentation, bool reportAllFactors, std::ostream& out); -void OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/,char reportSegmentation, bool reportAllFactors, std::ostream &out); -void OutputInput(std::ostream& os, const Moses::Hypothesis* hypo); -void OutputAlignment(Moses::OutputCollector* collector, size_t lineNo, const Moses::Hypothesis *hypo); -void OutputAlignment(Moses::OutputCollector* collector, size_t lineNo, const Moses::TrellisPath &path); -void OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo); -void OutputAlignment(std::ostream &out, const Moses::AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset); - -void OutputNBest(std::ostream& out - , const Moses::TrellisPathList &nBestList - , const std::vector& outputFactorOrder - , long translationId - , char reportSegmentation); -void OutputAllFeatureScores(const Moses::ScoreComponentCollection &features - , std::ostream &out); -void OutputFeatureScores( std::ostream& out - , const Moses::ScoreComponentCollection &features - , const Moses::FeatureFunction *ff - , std::string &lastName ); - -// creates a map of TARGET positions which should be replaced by word using placeholder -std::map GetPlaceholders(const Moses::Hypothesis &hypo, Moses::FactorType placeholderFactor); } -#endif diff --git a/moses/TranslationTask.cpp b/moses/TranslationTask.cpp index 49dc7ca07..a5e01a359 100644 --- a/moses/TranslationTask.cpp +++ b/moses/TranslationTask.cpp @@ -163,7 +163,7 @@ void TranslationTask::RunPb() out << bestHypo->GetTotalScore() << ' '; } if (staticData.IsPathRecoveryEnabled()) { - OutputInput(out, bestHypo); + m_ioWrapper.OutputInput(out, bestHypo); out << "||| "; } if (staticData.GetParam("print-id").size() && Scan(staticData.GetParam("print-id")[0]) ) { @@ -173,7 +173,7 @@ void TranslationTask::RunPb() if (staticData.GetReportSegmentation() == 2) { manager.GetOutputLanguageModelOrder(out, bestHypo); } - OutputBestSurface( + m_ioWrapper.OutputBestSurface( out, bestHypo, staticData.GetOutputFactorOrder(), @@ -181,10 +181,10 @@ void TranslationTask::RunPb() staticData.GetReportAllFactors()); if (staticData.PrintAlignmentInfo()) { out << "||| "; - OutputAlignment(out, bestHypo); + m_ioWrapper.OutputAlignment(out, bestHypo); } - OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), bestHypo); + m_ioWrapper.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), bestHypo); IFVERBOSE(1) { debug << "BEST TRANSLATION: " << *bestHypo << endl; } @@ -218,12 +218,12 @@ void TranslationTask::RunPb() size_t n = min(nBestSize, staticData.GetNBestSize()); getLatticeMBRNBest(manager,nBestList,solutions,n); ostringstream out; - OutputLatticeMBRNBest(out, solutions,m_source->GetTranslationId()); + m_ioWrapper.OutputLatticeMBRNBest(out, solutions,m_source->GetTranslationId()); m_ioWrapper.GetNBestOutputCollector()->Write(m_source->GetTranslationId(), out.str()); } else { //Lattice MBR decoding vector mbrBestHypo = doLatticeMBR(manager,nBestList); - OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(), staticData.GetReportSegmentation(), + m_ioWrapper.OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(), staticData.GetReportSegmentation(), staticData.GetReportAllFactors(),out); IFVERBOSE(2) { PrintUserTime("finished Lattice MBR decoding"); @@ -234,10 +234,10 @@ void TranslationTask::RunPb() // consensus decoding else if (staticData.UseConsensusDecoding()) { const TrellisPath &conBestHypo = doConsensusDecoding(manager,nBestList); - OutputBestHypo(conBestHypo, m_source->GetTranslationId(), + m_ioWrapper.OutputBestHypo(conBestHypo, m_source->GetTranslationId(), staticData.GetReportSegmentation(), staticData.GetReportAllFactors(),out); - OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), conBestHypo); + m_ioWrapper.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), conBestHypo); IFVERBOSE(2) { PrintUserTime("finished Consensus decoding"); } @@ -246,10 +246,10 @@ void TranslationTask::RunPb() // n-best MBR decoding else { const TrellisPath &mbrBestHypo = doMBR(nBestList); - OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(), + m_ioWrapper.OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(), staticData.GetReportSegmentation(), staticData.GetReportAllFactors(),out); - OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), mbrBestHypo); + m_ioWrapper.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), mbrBestHypo); IFVERBOSE(2) { PrintUserTime("finished MBR decoding"); } @@ -270,7 +270,7 @@ void TranslationTask::RunPb() TrellisPathList nBestList; ostringstream out; manager.CalcNBest(staticData.GetNBestSize(), nBestList,staticData.GetDistinctNBest()); - OutputNBest(out, nBestList, staticData.GetOutputFactorOrder(), m_source->GetTranslationId(), + m_ioWrapper.OutputNBest(out, nBestList, staticData.GetOutputFactorOrder(), m_source->GetTranslationId(), staticData.GetReportSegmentation()); m_ioWrapper.GetNBestOutputCollector()->Write(m_source->GetTranslationId(), out.str()); } @@ -280,7 +280,7 @@ void TranslationTask::RunPb() TrellisPathList latticeSamples; ostringstream out; manager.CalcLatticeSamples(staticData.GetLatticeSamplesSize(), latticeSamples); - OutputNBest(out,latticeSamples, staticData.GetOutputFactorOrder(), m_source->GetTranslationId(), + m_ioWrapper.OutputNBest(out,latticeSamples, staticData.GetOutputFactorOrder(), m_source->GetTranslationId(), staticData.GetReportSegmentation()); m_ioWrapper.GetLatticeSamplesCollector()->Write(m_source->GetTranslationId(), out.str()); }