mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-12-29 06:52:34 +03:00
Refactor output collector so that it can delete the output and debug streams. Very useful if your output can be either cout/cerr or stream.
This commit is contained in:
parent
4ec7b56a16
commit
097735469e
@ -62,7 +62,6 @@ IOWrapper::IOWrapper(const std::vector<FactorType> &inputFactorOrder
|
|||||||
:m_inputFactorOrder(inputFactorOrder)
|
:m_inputFactorOrder(inputFactorOrder)
|
||||||
,m_outputFactorOrder(outputFactorOrder)
|
,m_outputFactorOrder(outputFactorOrder)
|
||||||
,m_inputFactorUsed(inputFactorUsed)
|
,m_inputFactorUsed(inputFactorUsed)
|
||||||
,m_nBestStream(NULL)
|
|
||||||
,m_outputSearchGraphStream(NULL)
|
,m_outputSearchGraphStream(NULL)
|
||||||
,m_detailedTranslationReportingStream(NULL)
|
,m_detailedTranslationReportingStream(NULL)
|
||||||
,m_inputFilePath(inputFilePath)
|
,m_inputFilePath(inputFilePath)
|
||||||
@ -79,21 +78,19 @@ IOWrapper::IOWrapper(const std::vector<FactorType> &inputFactorOrder
|
|||||||
m_inputStream = new InputFileStream(inputFilePath);
|
m_inputStream = new InputFileStream(inputFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_surpressSingleBestOutput = false;
|
bool suppressSingleBestOutput = false;
|
||||||
|
|
||||||
if (nBestSize > 0) {
|
if (nBestSize > 0) {
|
||||||
if (nBestFilePath == "-") {
|
if (nBestFilePath == "-") {
|
||||||
m_nBestStream = &std::cout;
|
m_nBestOutputCollector = new Moses::OutputCollector(&std::cout);
|
||||||
m_surpressSingleBestOutput = true;
|
suppressSingleBestOutput = true;
|
||||||
} else {
|
} else {
|
||||||
std::ofstream *nBestFile = new std::ofstream;
|
m_nBestOutputCollector = new Moses::OutputCollector(new std::ofstream(nBestFilePath.c_str()));
|
||||||
m_nBestStream = nBestFile;
|
m_nBestOutputCollector->HoldOutputStream();
|
||||||
nBestFile->open(nBestFilePath.c_str());
|
|
||||||
}
|
}
|
||||||
m_nBestOutputCollector = new Moses::OutputCollector(m_nBestStream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_surpressSingleBestOutput) {
|
if (!suppressSingleBestOutput) {
|
||||||
m_singleBestOutputCollector = new Moses::OutputCollector(&std::cout);
|
m_singleBestOutputCollector = new Moses::OutputCollector(&std::cout);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,10 +116,6 @@ IOWrapper::~IOWrapper()
|
|||||||
if (!m_inputFilePath.empty()) {
|
if (!m_inputFilePath.empty()) {
|
||||||
delete m_inputStream;
|
delete m_inputStream;
|
||||||
}
|
}
|
||||||
if (!m_surpressSingleBestOutput) {
|
|
||||||
// outputting n-best to file, rather than stdout. need to close file and delete obj
|
|
||||||
delete m_nBestStream;
|
|
||||||
}
|
|
||||||
delete m_outputSearchGraphStream;
|
delete m_outputSearchGraphStream;
|
||||||
delete m_detailedTranslationReportingStream;
|
delete m_detailedTranslationReportingStream;
|
||||||
delete m_detailOutputCollector;
|
delete m_detailOutputCollector;
|
||||||
@ -328,6 +321,8 @@ void IOWrapper::OutputDetailedTranslationReport(
|
|||||||
|
|
||||||
void IOWrapper::OutputBestHypo(const ChartHypothesis *hypo, long translationId, bool /* reportSegmentation */, bool /* reportAllFactors */)
|
void IOWrapper::OutputBestHypo(const ChartHypothesis *hypo, long translationId, bool /* reportSegmentation */, bool /* reportAllFactors */)
|
||||||
{
|
{
|
||||||
|
if (!m_singleBestOutputCollector)
|
||||||
|
return;
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
IOWrapper::FixPrecision(out);
|
IOWrapper::FixPrecision(out);
|
||||||
if (hypo != NULL) {
|
if (hypo != NULL) {
|
||||||
@ -339,23 +334,21 @@ void IOWrapper::OutputBestHypo(const ChartHypothesis *hypo, long translationId,
|
|||||||
if (StaticData::Instance().GetOutputHypoScore()) {
|
if (StaticData::Instance().GetOutputHypoScore()) {
|
||||||
out << hypo->GetTotalScore() << " ";
|
out << hypo->GetTotalScore() << " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_surpressSingleBestOutput) {
|
if (StaticData::Instance().IsPathRecoveryEnabled()) {
|
||||||
if (StaticData::Instance().IsPathRecoveryEnabled()) {
|
out << "||| ";
|
||||||
out << "||| ";
|
|
||||||
}
|
|
||||||
Phrase outPhrase(ARRAY_SIZE_INCR);
|
|
||||||
hypo->CreateOutputPhrase(outPhrase);
|
|
||||||
|
|
||||||
// delete 1st & last
|
|
||||||
CHECK(outPhrase.GetSize() >= 2);
|
|
||||||
outPhrase.RemoveWord(0);
|
|
||||||
outPhrase.RemoveWord(outPhrase.GetSize() - 1);
|
|
||||||
|
|
||||||
const std::vector<FactorType> outputFactorOrder = StaticData::Instance().GetOutputFactorOrder();
|
|
||||||
string output = outPhrase.GetStringRep(outputFactorOrder);
|
|
||||||
out << output << endl;
|
|
||||||
}
|
}
|
||||||
|
Phrase outPhrase(ARRAY_SIZE_INCR);
|
||||||
|
hypo->CreateOutputPhrase(outPhrase);
|
||||||
|
|
||||||
|
// delete 1st & last
|
||||||
|
CHECK(outPhrase.GetSize() >= 2);
|
||||||
|
outPhrase.RemoveWord(0);
|
||||||
|
outPhrase.RemoveWord(outPhrase.GetSize() - 1);
|
||||||
|
|
||||||
|
const std::vector<FactorType> outputFactorOrder = StaticData::Instance().GetOutputFactorOrder();
|
||||||
|
string output = outPhrase.GetStringRep(outputFactorOrder);
|
||||||
|
out << output << endl;
|
||||||
} else {
|
} else {
|
||||||
VERBOSE(1, "NO BEST TRANSLATION" << endl);
|
VERBOSE(1, "NO BEST TRANSLATION" << endl);
|
||||||
|
|
||||||
@ -365,10 +358,7 @@ void IOWrapper::OutputBestHypo(const ChartHypothesis *hypo, long translationId,
|
|||||||
|
|
||||||
out << endl;
|
out << endl;
|
||||||
}
|
}
|
||||||
|
m_singleBestOutputCollector->Write(translationId, out.str());
|
||||||
if (m_singleBestOutputCollector) {
|
|
||||||
m_singleBestOutputCollector->Write(translationId, out.str());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IOWrapper::OutputNBestList(const ChartTrellisPathList &nBestList, const ChartHypothesis *bestHypo, const TranslationSystem* system, long translationId)
|
void IOWrapper::OutputNBestList(const ChartTrellisPathList &nBestList, const ChartHypothesis *bestHypo, const TranslationSystem* system, long translationId)
|
||||||
@ -376,7 +366,7 @@ void IOWrapper::OutputNBestList(const ChartTrellisPathList &nBestList, const Cha
|
|||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
|
|
||||||
// Check if we're writing to std::cout.
|
// Check if we're writing to std::cout.
|
||||||
if (m_surpressSingleBestOutput) {
|
if (m_nBestOutputCollector->OutputIsCout()) {
|
||||||
// Set precision only if we're writing the n-best list to cout. This is to
|
// Set precision only if we're writing the n-best list to cout. This is to
|
||||||
// preserve existing behaviour, but should probably be done either way.
|
// preserve existing behaviour, but should probably be done either way.
|
||||||
IOWrapper::FixPrecision(out);
|
IOWrapper::FixPrecision(out);
|
||||||
|
@ -64,11 +64,10 @@ protected:
|
|||||||
const std::vector<Moses::FactorType> &m_inputFactorOrder;
|
const std::vector<Moses::FactorType> &m_inputFactorOrder;
|
||||||
const std::vector<Moses::FactorType> &m_outputFactorOrder;
|
const std::vector<Moses::FactorType> &m_outputFactorOrder;
|
||||||
const Moses::FactorMask &m_inputFactorUsed;
|
const Moses::FactorMask &m_inputFactorUsed;
|
||||||
std::ostream *m_nBestStream, *m_outputSearchGraphStream;
|
std::ostream *m_outputSearchGraphStream;
|
||||||
std::ostream *m_detailedTranslationReportingStream;
|
std::ostream *m_detailedTranslationReportingStream;
|
||||||
std::string m_inputFilePath;
|
std::string m_inputFilePath;
|
||||||
std::istream *m_inputStream;
|
std::istream *m_inputStream;
|
||||||
bool m_surpressSingleBestOutput;
|
|
||||||
Moses::OutputCollector *m_detailOutputCollector;
|
Moses::OutputCollector *m_detailOutputCollector;
|
||||||
Moses::OutputCollector *m_nBestOutputCollector;
|
Moses::OutputCollector *m_nBestOutputCollector;
|
||||||
Moses::OutputCollector *m_searchGraphOutputCollector;
|
Moses::OutputCollector *m_searchGraphOutputCollector;
|
||||||
|
@ -43,8 +43,31 @@ class OutputCollector
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OutputCollector(std::ostream* outStream= &std::cout, std::ostream* debugStream=&std::cerr) :
|
OutputCollector(std::ostream* outStream= &std::cout, std::ostream* debugStream=&std::cerr) :
|
||||||
m_nextOutput(0),m_outStream(outStream),m_debugStream(debugStream) {}
|
m_nextOutput(0),m_outStream(outStream),m_debugStream(debugStream),
|
||||||
|
m_isHoldingOutputStream(false), m_isHoldingDebugStream(false) {}
|
||||||
|
|
||||||
|
~OutputCollector()
|
||||||
|
{
|
||||||
|
if (m_isHoldingOutputStream)
|
||||||
|
delete m_outStream;
|
||||||
|
if (m_isHoldingDebugStream)
|
||||||
|
delete m_debugStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HoldOutputStream()
|
||||||
|
{
|
||||||
|
m_isHoldingOutputStream = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HoldDebugStream()
|
||||||
|
{
|
||||||
|
m_isHoldingDebugStream = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool OutputIsCout() const
|
||||||
|
{
|
||||||
|
return (m_outStream == std::cout);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write or cache the output, as appropriate.
|
* Write or cache the output, as appropriate.
|
||||||
@ -82,6 +105,8 @@ private:
|
|||||||
int m_nextOutput;
|
int m_nextOutput;
|
||||||
std::ostream* m_outStream;
|
std::ostream* m_outStream;
|
||||||
std::ostream* m_debugStream;
|
std::ostream* m_debugStream;
|
||||||
|
bool m_isHoldingOutputStream;
|
||||||
|
bool m_isHoldingDebugStream;
|
||||||
#ifdef WITH_THREADS
|
#ifdef WITH_THREADS
|
||||||
boost::mutex m_mutex;
|
boost::mutex m_mutex;
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user