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()
: m_nBestStream(NULL)
, m_outputWordGraphStream(NULL)
, m_outputSearchGraphStream(NULL)
, m_detailedTranslationReportingStream(NULL)
, m_unknownsStream(NULL)
, m_alignmentInfoStream(NULL)
, m_latticeSamplesStream(NULL)
// , m_outputWordGraphStream(NULL)
// , m_outputSearchGraphStream(NULL)
// , m_detailedTranslationReportingStream(NULL)
// , m_unknownsStream(NULL)
// , m_alignmentInfoStream(NULL)
// , m_latticeSamplesStream(NULL)
, m_surpressSingleBestOutput(false)
, m_look_ahead(0)
, m_look_back(0)
@ -94,6 +94,7 @@ IOWrapper::IOWrapper()
, spe_aln(NULL)
{
const StaticData &staticData = StaticData::Instance();
Parameter const& P = staticData.GetParameter();
// context buffering for context-sensitive decoding
m_look_ahead = staticData.options().context.look_ahead;
@ -122,98 +123,41 @@ IOWrapper::IOWrapper()
}
if (nBestSize > 0) {
if (nBestFilePath == "-" || nBestFilePath == "/dev/stdout") {
m_nBestStream = &std::cout;
m_nBestOutputCollector.reset(new Moses::OutputCollector(&std::cout));
m_nBestOutputCollector.reset(new Moses::OutputCollector(nBestFilePath));
if (m_nBestOutputCollector->OutputIsCout()) {
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
if (staticData.GetOutputSearchGraph()) {
string fileName;
if (staticData.GetOutputSearchGraphExtended()) {
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());
}
std::string path;
P.SetParameter<std::string>(path, "output-search-graph-extended", "");
if (!path.size()) P.SetParameter<std::string>(path, "output-search-graph", "");
if (path.size()) m_searchGraphOutputCollector.reset(new OutputCollector(path));
if (!staticData.GetOutputUnknownsFile().empty()) {
m_unknownsStream = new std::ofstream(staticData.GetOutputUnknownsFile().c_str());
m_unknownsCollector.reset(new Moses::OutputCollector(m_unknownsStream));
UTIL_THROW_IF2(!m_unknownsStream->good(),
"File for unknowns words could not be opened: " <<
staticData.GetOutputUnknownsFile());
}
P.SetParameter<std::string>(path, "output-unknowns", "");
if (path.size()) m_unknownsCollector.reset(new OutputCollector(path));
if (!staticData.GetAlignmentOutputFile().empty()) {
m_alignmentInfoStream = new std::ofstream(staticData.GetAlignmentOutputFile().c_str());
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()) {
string fileName;
staticData.GetParameter().SetParameter<string>(fileName, "output-search-graph", "");
std::ofstream *file = new std::ofstream;
m_outputSearchGraphStream = file;
file->open(fileName.c_str());
m_searchGraphOutputCollector.reset(new Moses::OutputCollector(m_outputSearchGraphStream));
}
// detailed translation reporting
if (staticData.IsDetailedTranslationReportingEnabled()) {
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));
}
P.SetParameter<std::string>(path, "alignment-output-file", "");
if (path.size()) m_alignmentInfoCollector.reset(new OutputCollector(path));
P.SetParameter<string>(path, "translation-details", "");
if (path.size()) m_detailedTranslationCollector.reset(new OutputCollector(path));
P.SetParameter<string>(path, "tree-translation-details", "");
if (path.size()) m_detailTreeFragmentsOutputCollector.reset(new OutputCollector(path));
P.SetParameter<string>(path, "output-word-graph", "");
if (path.size()) m_wordGraphCollector.reset(new OutputCollector(path));
size_t latticeSamplesSize = staticData.GetLatticeSamplesSize();
string latticeSamplesFile = staticData.GetLatticeSamplesFilePath();
if (latticeSamplesSize) {
if (latticeSamplesFile == "-" || latticeSamplesFile == "/dev/stdout") {
m_latticeSamplesCollector.reset(new OutputCollector());
m_latticeSamplesCollector.reset(new OutputCollector(latticeSamplesFile));
if (m_latticeSamplesCollector->OutputIsCout()) {
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));
}
}
if (!m_surpressSingleBestOutput) {
m_singleBestOutputCollector.reset(new Moses::OutputCollector(&std::cout));
}
@ -236,6 +180,7 @@ IOWrapper::IOWrapper()
<< "' for hypergraph output!");
fmt += string("%d.") + extension;
// input streams for simulated post-editing
if (staticData.GetParameter().GetParam("spe-src")) {
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());
@ -247,17 +192,17 @@ IOWrapper::~IOWrapper()
{
if (m_inputFile != NULL)
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
delete m_nBestStream;
}
// delete m_nBestStream;
// }
delete m_detailedTranslationReportingStream;
delete m_alignmentInfoStream;
delete m_unknownsStream;
delete m_outputSearchGraphStream;
delete m_outputWordGraphStream;
delete m_latticeSamplesStream;
// delete m_detailedTranslationReportingStream;
// delete m_alignmentInfoStream;
// delete m_unknownsStream;
// delete m_outputSearchGraphStream;
// delete m_outputWordGraphStream;
// delete m_latticeSamplesStream;
}
// InputType*

View File

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

View File

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