Code simplification in IOWrapper.

This commit is contained in:
Ulrich Germann 2015-08-07 03:01:24 +01:00
parent 4ad6f2a5a6
commit d11906abcd
3 changed files with 49 additions and 103 deletions

View File

@ -79,12 +79,12 @@ namespace Moses
IOWrapper::IOWrapper() IOWrapper::IOWrapper()
: m_nBestStream(NULL) : m_nBestStream(NULL)
, m_outputWordGraphStream(NULL) // , m_outputWordGraphStream(NULL)
, m_outputSearchGraphStream(NULL) // , m_outputSearchGraphStream(NULL)
, m_detailedTranslationReportingStream(NULL) // , m_detailedTranslationReportingStream(NULL)
, m_unknownsStream(NULL) // , m_unknownsStream(NULL)
, m_alignmentInfoStream(NULL) // , m_alignmentInfoStream(NULL)
, m_latticeSamplesStream(NULL) // , m_latticeSamplesStream(NULL)
, m_surpressSingleBestOutput(false) , m_surpressSingleBestOutput(false)
, m_look_ahead(0) , m_look_ahead(0)
, m_look_back(0) , m_look_back(0)
@ -94,6 +94,7 @@ IOWrapper::IOWrapper()
, spe_aln(NULL) , spe_aln(NULL)
{ {
const StaticData &staticData = StaticData::Instance(); const StaticData &staticData = StaticData::Instance();
Parameter const& P = staticData.GetParameter();
// context buffering for context-sensitive decoding // context buffering for context-sensitive decoding
m_look_ahead = staticData.options().context.look_ahead; m_look_ahead = staticData.options().context.look_ahead;
@ -122,95 +123,38 @@ IOWrapper::IOWrapper()
} }
if (nBestSize > 0) { if (nBestSize > 0) {
if (nBestFilePath == "-" || nBestFilePath == "/dev/stdout") { m_nBestOutputCollector.reset(new Moses::OutputCollector(nBestFilePath));
m_nBestStream = &std::cout; if (m_nBestOutputCollector->OutputIsCout()) {
m_nBestOutputCollector.reset(new Moses::OutputCollector(&std::cout));
m_surpressSingleBestOutput = true; m_surpressSingleBestOutput = true;
} else {
std::ofstream *file = new std::ofstream;
file->open(nBestFilePath.c_str());
m_nBestStream = file;
m_nBestOutputCollector.reset(new Moses::OutputCollector(file));
//m_nBestOutputCollector->HoldOutputStream();
} }
} }
// search graph output std::string path;
if (staticData.GetOutputSearchGraph()) { P.SetParameter<std::string>(path, "output-search-graph-extended", "");
string fileName; if (!path.size()) P.SetParameter<std::string>(path, "output-search-graph", "");
if (staticData.GetOutputSearchGraphExtended()) { if (path.size()) m_searchGraphOutputCollector.reset(new OutputCollector(path));
staticData.GetParameter().SetParameter<string>(fileName, "output-search-graph-extended", "");
} else {
staticData.GetParameter().SetParameter<string>(fileName, "output-search-graph", "");
}
std::ofstream *file = new std::ofstream;
m_outputSearchGraphStream = file;
file->open(fileName.c_str());
}
if (!staticData.GetOutputUnknownsFile().empty()) { P.SetParameter<std::string>(path, "output-unknowns", "");
m_unknownsStream = new std::ofstream(staticData.GetOutputUnknownsFile().c_str()); if (path.size()) m_unknownsCollector.reset(new OutputCollector(path));
m_unknownsCollector.reset(new Moses::OutputCollector(m_unknownsStream));
UTIL_THROW_IF2(!m_unknownsStream->good(),
"File for unknowns words could not be opened: " <<
staticData.GetOutputUnknownsFile());
}
if (!staticData.GetAlignmentOutputFile().empty()) { P.SetParameter<std::string>(path, "alignment-output-file", "");
m_alignmentInfoStream = new std::ofstream(staticData.GetAlignmentOutputFile().c_str()); if (path.size()) m_alignmentInfoCollector.reset(new OutputCollector(path));
m_alignmentInfoCollector.reset(new Moses::OutputCollector(m_alignmentInfoStream));
UTIL_THROW_IF2(!m_alignmentInfoStream->good(),
"File for alignment output could not be opened: " << staticData.GetAlignmentOutputFile());
}
if (staticData.GetOutputSearchGraph()) { P.SetParameter<string>(path, "translation-details", "");
string fileName; if (path.size()) m_detailedTranslationCollector.reset(new OutputCollector(path));
staticData.GetParameter().SetParameter<string>(fileName, "output-search-graph", "");
std::ofstream *file = new std::ofstream; P.SetParameter<string>(path, "tree-translation-details", "");
m_outputSearchGraphStream = file; if (path.size()) m_detailTreeFragmentsOutputCollector.reset(new OutputCollector(path));
file->open(fileName.c_str());
m_searchGraphOutputCollector.reset(new Moses::OutputCollector(m_outputSearchGraphStream));
}
// detailed translation reporting P.SetParameter<string>(path, "output-word-graph", "");
if (staticData.IsDetailedTranslationReportingEnabled()) { if (path.size()) m_wordGraphCollector.reset(new OutputCollector(path));
const std::string &path = staticData.GetDetailedTranslationReportingFilePath();
m_detailedTranslationReportingStream = new std::ofstream(path.c_str());
m_detailedTranslationCollector.reset(new Moses::OutputCollector(m_detailedTranslationReportingStream));
}
if (staticData.IsDetailedTreeFragmentsTranslationReportingEnabled()) {
const std::string &path = staticData.GetDetailedTreeFragmentsTranslationReportingFilePath();
m_detailedTreeFragmentsTranslationReportingStream = new std::ofstream(path.c_str());
m_detailTreeFragmentsOutputCollector.reset(new Moses::OutputCollector(m_detailedTreeFragmentsTranslationReportingStream));
}
// wordgraph output
if (staticData.GetOutputWordGraph()) {
string fileName;
staticData.GetParameter().SetParameter<string>(fileName, "output-word-graph", "");
std::ofstream *file = new std::ofstream;
m_outputWordGraphStream = file;
file->open(fileName.c_str());
m_wordGraphCollector.reset(new OutputCollector(m_outputWordGraphStream));
}
size_t latticeSamplesSize = staticData.GetLatticeSamplesSize(); size_t latticeSamplesSize = staticData.GetLatticeSamplesSize();
string latticeSamplesFile = staticData.GetLatticeSamplesFilePath(); string latticeSamplesFile = staticData.GetLatticeSamplesFilePath();
if (latticeSamplesSize) { if (latticeSamplesSize) {
if (latticeSamplesFile == "-" || latticeSamplesFile == "/dev/stdout") { m_latticeSamplesCollector.reset(new OutputCollector(latticeSamplesFile));
m_latticeSamplesCollector.reset(new OutputCollector()); if (m_latticeSamplesCollector->OutputIsCout()) {
m_surpressSingleBestOutput = true; m_surpressSingleBestOutput = true;
} else {
m_latticeSamplesStream = new ofstream(latticeSamplesFile.c_str());
if (!m_latticeSamplesStream->good()) {
TRACE_ERR("ERROR: Failed to open " << latticeSamplesFile << " for lattice samples" << endl);
exit(1);
}
m_latticeSamplesCollector.reset(new OutputCollector(m_latticeSamplesStream));
} }
} }
@ -236,6 +180,7 @@ IOWrapper::IOWrapper()
<< "' for hypergraph output!"); << "' for hypergraph output!");
fmt += string("%d.") + extension; fmt += string("%d.") + extension;
// input streams for simulated post-editing
if (staticData.GetParameter().GetParam("spe-src")) { if (staticData.GetParameter().GetParam("spe-src")) {
spe_src = new ifstream(staticData.GetParameter().GetParam("spe-src")->at(0).c_str()); spe_src = new ifstream(staticData.GetParameter().GetParam("spe-src")->at(0).c_str());
spe_trg = new ifstream(staticData.GetParameter().GetParam("spe-trg")->at(0).c_str()); spe_trg = new ifstream(staticData.GetParameter().GetParam("spe-trg")->at(0).c_str());
@ -247,17 +192,17 @@ IOWrapper::~IOWrapper()
{ {
if (m_inputFile != NULL) if (m_inputFile != NULL)
delete m_inputFile; delete m_inputFile;
if (m_nBestStream != NULL && !m_surpressSingleBestOutput) { // if (m_nBestStream != NULL && !m_surpressSingleBestOutput) {
// outputting n-best to file, rather than stdout. need to close file and delete obj // outputting n-best to file, rather than stdout. need to close file and delete obj
delete m_nBestStream; // delete m_nBestStream;
} // }
delete m_detailedTranslationReportingStream; // delete m_detailedTranslationReportingStream;
delete m_alignmentInfoStream; // delete m_alignmentInfoStream;
delete m_unknownsStream; // delete m_unknownsStream;
delete m_outputSearchGraphStream; // delete m_outputSearchGraphStream;
delete m_outputWordGraphStream; // delete m_outputWordGraphStream;
delete m_latticeSamplesStream; // delete m_latticeSamplesStream;
} }
// InputType* // InputType*

View File

@ -1,4 +1,4 @@
// -*- c++ -*- // -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*-
// $Id$ // $Id$
/*********************************************************************** /***********************************************************************
@ -86,11 +86,11 @@ protected:
Moses::InputFileStream *m_inputFile; Moses::InputFileStream *m_inputFile;
std::istream *m_inputStream; std::istream *m_inputStream;
std::ostream *m_nBestStream; std::ostream *m_nBestStream;
std::ostream *m_outputWordGraphStream; // std::ostream *m_outputWordGraphStream;
std::ostream *m_outputSearchGraphStream; // std::auto_ptr<std::ostream> m_outputSearchGraphStream;
std::ostream *m_detailedTranslationReportingStream; // std::ostream *m_detailedTranslationReportingStream;
std::ostream *m_unknownsStream; std::ostream *m_unknownsStream;
std::ostream *m_detailedTreeFragmentsTranslationReportingStream; // std::ostream *m_detailedTreeFragmentsTranslationReportingStream;
std::ofstream *m_alignmentInfoStream; std::ofstream *m_alignmentInfoStream;
std::ofstream *m_latticeSamplesStream; std::ofstream *m_latticeSamplesStream;

View File

@ -33,7 +33,8 @@
#include <map> #include <map>
#include <ostream> #include <ostream>
#include <string> #include <string>
#include "Util.h"
#include "util/exception.hh"
namespace Moses namespace Moses
{ {
/** /**
@ -60,8 +61,8 @@ public:
m_isHoldingOutputStream = false; m_isHoldingOutputStream = false;
} else if (xout.size() && xout != "/dev/stdout" && xout != "-") { } else if (xout.size() && xout != "/dev/stdout" && xout != "-") {
m_outStream = new std::ofstream(xout.c_str()); m_outStream = new std::ofstream(xout.c_str());
UTIL_THROW_IF2(!m_outputStream->good(), "Failed to open output file" UTIL_THROW_IF2(!m_outStream->good(), "Failed to open output file"
<< xout << std::endl); << xout);
m_isHoldingOutputStream = true; m_isHoldingOutputStream = true;
} else { } else {
m_outStream = &std::cout; m_outStream = &std::cout;
@ -74,7 +75,7 @@ public:
} else if (xerr.size() && xerr != "/dev/stderr") { } else if (xerr.size() && xerr != "/dev/stderr") {
m_debugStream = new std::ofstream(xerr.c_str()); m_debugStream = new std::ofstream(xerr.c_str());
UTIL_THROW_IF2(!m_debugStream->good(), "Failed to open debug stream" UTIL_THROW_IF2(!m_debugStream->good(), "Failed to open debug stream"
<< xerr << std::endl); << xerr);
m_isHoldingDebugStream = true; m_isHoldingDebugStream = true;
} else { } else {
m_debugStream = &std::cerr; m_debugStream = &std::cerr;