Skip to content

Commit

Permalink
Use OutputCollector to write moses-chart output.
Browse files Browse the repository at this point in the history
git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@3843 1f5c12ca-751b-0410-a591-d2e778427230
  • Loading branch information
pjwilliams committed Jan 25, 2011
1 parent ce4cc28 commit ea6921c
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 110 deletions.
115 changes: 79 additions & 36 deletions moses-chart-cmd/src/IOWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ IOWrapper::IOWrapper(const std::vector<FactorType> &inputFactorOrder
,m_inputFactorUsed(inputFactorUsed)
,m_nBestStream(NULL)
,m_outputSearchGraphStream(NULL)
,m_detailedTranslationReportingStream(0)
,m_detailedTranslationReportingStream(NULL)
,m_inputFilePath(inputFilePath)
,m_nBestOutputCollector(NULL)
,m_searchGraphOutputCollector(NULL)
,m_singleBestOutputCollector(NULL)
{
const StaticData &staticData = StaticData::Instance();

Expand Down Expand Up @@ -88,15 +91,22 @@ IOWrapper::IOWrapper(const std::vector<FactorType> &inputFactorOrder
m_nBestStream = nBestFile;
nBestFile->open(nBestFilePath.c_str());
}
m_nBestOutputCollector = new Moses::OutputCollector(m_nBestStream);
}

if (!m_surpressSingleBestOutput)
{
m_singleBestOutputCollector = new Moses::OutputCollector(&std::cout);
}

// search graph output
if (staticData.GetOutputSearchGraph())
{
string fileName = staticData.GetParam("output-search-graph")[0];
std::ofstream *file = new std::ofstream;
m_outputSearchGraphStream = file;
file->open(fileName.c_str());
m_searchGraphOutputCollector = new Moses::OutputCollector(m_outputSearchGraphStream);
}

// detailed translation reporting
Expand All @@ -113,17 +123,15 @@ IOWrapper::~IOWrapper()
{
delete m_inputStream;
}
if (m_nBestStream != NULL && !m_surpressSingleBestOutput)
if (!m_surpressSingleBestOutput)
{ // outputting n-best to file, rather than stdout. need to close file and delete obj
delete m_nBestStream;
}

if (m_outputSearchGraphStream != NULL)
{
delete m_outputSearchGraphStream;
}

delete m_outputSearchGraphStream;
delete m_detailedTranslationReportingStream;
delete m_nBestOutputCollector;
delete m_searchGraphOutputCollector;
delete m_singleBestOutputCollector;
}

InputType*IOWrapper::GetInput(InputType* inputType)
Expand Down Expand Up @@ -251,6 +259,8 @@ void OutputTranslationOptions(std::ostream &out, const MosesChart::Hypothesis *h

void IOWrapper::OutputBestHypo(const MosesChart::Hypothesis *hypo, long translationId, bool reportSegmentation, bool reportAllFactors)
{
std::ostringstream out;
IOWrapper::FixPrecision(out);
if (hypo != NULL)
{
VERBOSE(1,"BEST TRANSLATION: " << *hypo << endl);
Expand All @@ -260,7 +270,7 @@ void IOWrapper::OutputBestHypo(const MosesChart::Hypothesis *hypo, long translat

if (StaticData::Instance().GetOutputHypoScore())
{
cout << hypo->GetTotalScore() << " "
out << hypo->GetTotalScore() << " "
<< MosesChart::Hypothesis::GetHypoCount() << " ";
}

Expand All @@ -272,8 +282,7 @@ void IOWrapper::OutputBestHypo(const MosesChart::Hypothesis *hypo, long translat
if (!m_surpressSingleBestOutput)
{
if (StaticData::Instance().IsPathRecoveryEnabled()) {
//OutputInput(cout, hypo);
cout << "||| ";
out << "||| ";
}
Phrase outPhrase(Output);
hypo->CreateOutputPhrase(outPhrase);
Expand All @@ -285,7 +294,7 @@ void IOWrapper::OutputBestHypo(const MosesChart::Hypothesis *hypo, long translat

const std::vector<FactorType> outputFactorOrder = StaticData::Instance().GetOutputFactorOrder();
string output = outPhrase.GetStringRep(outputFactorOrder);
cout << output << endl;
out << output << endl;
}
}
else
Expand All @@ -294,18 +303,44 @@ void IOWrapper::OutputBestHypo(const MosesChart::Hypothesis *hypo, long translat

if (StaticData::Instance().GetOutputHypoScore())
{
cout << "0 ";
out << "0 ";
}

if (!m_surpressSingleBestOutput)
{
cout << endl;
}
out << endl;
}

if (m_singleBestOutputCollector)
{
m_singleBestOutputCollector->Write(translationId, out.str());
}
}

void IOWrapper::OutputNBestList(const MosesChart::TrellisPathList &nBestList, const TranslationSystem* system, long translationId)
void IOWrapper::OutputNBestList(const MosesChart::TrellisPathList &nBestList, const MosesChart::Hypothesis *bestHypo, const TranslationSystem* system, long translationId)
{
std::ostringstream out;

// Check if we're writing to std::cout.
if (m_surpressSingleBestOutput)
{
// 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.
IOWrapper::FixPrecision(out);

// The output from -output-hypo-score is always written to std::cout.
if (StaticData::Instance().GetOutputHypoScore())
{
if (bestHypo != NULL)
{
out << bestHypo->GetTotalScore() << " "
<< MosesChart::Hypothesis::GetHypoCount() << " ";
}
else
{
out << "0 ";
}
}
}

bool labeledOutput = StaticData::Instance().IsLabeledNBestList();
//bool includeAlignment = StaticData::Instance().NBestIncludesAlignment();

Expand All @@ -323,9 +358,9 @@ void IOWrapper::OutputNBestList(const MosesChart::TrellisPathList &nBestList, co
outputPhrase.RemoveWord(outputPhrase.GetSize() - 1);

// print the surface factor of the translation
*m_nBestStream << translationId << " ||| ";
OutputSurface(*m_nBestStream, outputPhrase, m_outputFactorOrder, false);
*m_nBestStream << " ||| ";
out << translationId << " ||| ";
OutputSurface(out, outputPhrase, m_outputFactorOrder, false);
out << " ||| ";

// print the scores in a hardwired order
// before each model type, the corresponding command-line-like name must be emitted
Expand All @@ -335,10 +370,10 @@ void IOWrapper::OutputNBestList(const MosesChart::TrellisPathList &nBestList, co
const LMList& lml = system->GetLanguageModels();
if (lml.size() > 0) {
if (labeledOutput)
*m_nBestStream << "lm: ";
out << "lm: ";
LMList::const_iterator lmi = lml.begin();
for (; lmi != lml.end(); ++lmi) {
*m_nBestStream << path.GetScoreBreakdown().GetScoreForProducer(*lmi) << " ";
out << path.GetScoreBreakdown().GetScoreForProducer(*lmi) << " ";
}
}

Expand All @@ -348,12 +383,12 @@ void IOWrapper::OutputNBestList(const MosesChart::TrellisPathList &nBestList, co
vector<PhraseDictionaryFeature*> pds = system->GetPhraseDictionaries();
if (pds.size() > 0) {
if (labeledOutput)
*m_nBestStream << "tm: ";
out << "tm: ";
vector<PhraseDictionaryFeature*>::iterator iter;
for (iter = pds.begin(); iter != pds.end(); ++iter) {
vector<float> scores = path.GetScoreBreakdown().GetScoresForProducer(*iter);
for (size_t j = 0; j<scores.size(); ++j)
*m_nBestStream << scores[j] << " ";
out << scores[j] << " ";
}
}
}
Expand All @@ -373,10 +408,10 @@ void IOWrapper::OutputNBestList(const MosesChart::TrellisPathList &nBestList, co
if (pd_numinputscore){

if (labeledOutput)
*m_nBestStream << "I: ";
out << "I: ";

for (size_t j = 0; j < pd_numinputscore; ++j)
*m_nBestStream << scores[j] << " ";
out << scores[j] << " ";
}


Expand All @@ -386,9 +421,9 @@ void IOWrapper::OutputNBestList(const MosesChart::TrellisPathList &nBestList, co
size_t pd_numinputscore = (*iter)->GetNumInputScores();

if (iter == pds.begin() && labeledOutput)
*m_nBestStream << "tm: ";
out << "tm: ";
for (size_t j = pd_numinputscore; j < scores.size() ; ++j)
*m_nBestStream << scores[j] << " ";
out << scores[j] << " ";
}
}
}
Expand All @@ -397,25 +432,25 @@ void IOWrapper::OutputNBestList(const MosesChart::TrellisPathList &nBestList, co

// word penalty
if (labeledOutput)
*m_nBestStream << "w: ";
*m_nBestStream << path.GetScoreBreakdown().GetScoreForProducer(system->GetWordPenaltyProducer()) << " ";
out << "w: ";
out << path.GetScoreBreakdown().GetScoreForProducer(system->GetWordPenaltyProducer()) << " ";

// generation
const vector<GenerationDictionary*> gds = system->GetGenerationDictionaries();
if (gds.size() > 0) {
if (labeledOutput)
*m_nBestStream << "g: ";
out << "g: ";
vector<GenerationDictionary*>::const_iterator iter;
for (iter = gds.begin(); iter != gds.end(); ++iter) {
vector<float> scores = path.GetScoreBreakdown().GetScoresForProducer(*iter);
for (size_t j = 0; j<scores.size(); j++) {
*m_nBestStream << scores[j] << " ";
out << scores[j] << " ";
}
}
}

// total
*m_nBestStream << "||| " << path.GetTotalScore();
out << "||| " << path.GetTotalScore();

/*
if (includeAlignment) {
Expand All @@ -437,9 +472,17 @@ void IOWrapper::OutputNBestList(const MosesChart::TrellisPathList &nBestList, co
}
*/

*m_nBestStream << endl;
out << endl;
}

*m_nBestStream<<std::flush;
out <<std::flush;

assert(m_nBestOutputCollector);
m_nBestOutputCollector->Write(translationId, out.str());
}

void IOWrapper::FixPrecision(std::ostream &stream, size_t size)
{
stream.setf(std::ios::fixed);
stream.precision(size);
}
13 changes: 9 additions & 4 deletions moses-chart-cmd/src/IOWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "FactorTypeSet.h"
#include "TranslationSystem.h"
#include "TrellisPathList.h"
#include "OutputCollector.h"
#include "../../moses-chart/src/ChartHypothesis.h"

namespace Moses
Expand All @@ -66,7 +67,10 @@ class IOWrapper
std::string m_inputFilePath;
std::istream *m_inputStream;
bool m_surpressSingleBestOutput;

Moses::OutputCollector *m_nBestOutputCollector;
Moses::OutputCollector *m_searchGraphOutputCollector;
Moses::OutputCollector *m_singleBestOutputCollector;

public:
IOWrapper(const std::vector<Moses::FactorType> &inputFactorOrder
, const std::vector<Moses::FactorType> &outputFactorOrder
Expand All @@ -79,12 +83,13 @@ class IOWrapper
Moses::InputType* GetInput(Moses::InputType *inputType);
void OutputBestHypo(const MosesChart::Hypothesis *hypo, long translationId, bool reportSegmentation, bool reportAllFactors);
void OutputBestHypo(const std::vector<const Moses::Factor*>& mbrBestHypo, long translationId, bool reportSegmentation, bool reportAllFactors);
void OutputNBestList(const MosesChart::TrellisPathList &nBestList, const Moses::TranslationSystem* system, long translationId);
void OutputNBestList(const MosesChart::TrellisPathList &nBestList, const MosesChart::Hypothesis *bestHypo, const Moses::TranslationSystem* system, long translationId);
void Backtrack(const MosesChart::Hypothesis *hypo);

void ResetTranslationId() { m_translationId = 0; }

std::ostream &GetOutputSearchGraphStream()
{ return *m_outputSearchGraphStream; }
Moses::OutputCollector *GetSearchGraphOutputCollector()
{ return m_searchGraphOutputCollector; }

static void FixPrecision(std::ostream &, size_t size=3);
};
19 changes: 12 additions & 7 deletions moses-chart-cmd/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,8 @@ int main(int argc, char* argv[])
TRACE_ERR(endl);
}

cout.setf(std::ios::fixed);
cout.precision(3);
cerr.setf(std::ios::fixed);
cerr.precision(3);
IOWrapper::FixPrecision(cout);
IOWrapper::FixPrecision(cerr);

// load data structures
Parameter parameter;
Expand Down Expand Up @@ -187,7 +185,8 @@ int main(int argc, char* argv[])
assert(!staticData.UseMBR());

if (!staticData.UseMBR()){
ioWrapper->OutputBestHypo(manager.GetBestHypothesis(), source->GetTranslationId(),
const MosesChart::Hypothesis *bestHypo = manager.GetBestHypothesis();
ioWrapper->OutputBestHypo(bestHypo, source->GetTranslationId(),
staticData.GetReportSegmentation(),
staticData.GetReportAllFactors()
);
Expand All @@ -200,13 +199,19 @@ int main(int argc, char* argv[])
VERBOSE(2,"WRITING " << nBestSize << " TRANSLATION ALTERNATIVES TO " << staticData.GetNBestFilePath() << endl);
MosesChart::TrellisPathList nBestList;
manager.CalcNBest(nBestSize, nBestList,staticData.GetDistinctNBest());
ioWrapper->OutputNBestList(nBestList, &system, source->GetTranslationId());
ioWrapper->OutputNBestList(nBestList, bestHypo, &system, source->GetTranslationId());

IFVERBOSE(2) { PrintUserTime("N-Best Hypotheses Generation Time:"); }
}

if (staticData.GetOutputSearchGraph())
manager.GetSearchGraph(source->GetTranslationId(), ioWrapper->GetOutputSearchGraphStream());
{
std::ostringstream out;
manager.GetSearchGraph(source->GetTranslationId(), out);
OutputCollector *oc = ioWrapper->GetSearchGraphOutputCollector();
assert(oc);
oc->Write(source->GetTranslationId(), out.str());
}

}
else {
Expand Down
Loading

0 comments on commit ea6921c

Please sign in to comment.