Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpjwilliams <pjwilliams@1f5c12ca-751b-0410-a591-d2e778427230>2011-01-25 18:17:17 +0300
committerpjwilliams <pjwilliams@1f5c12ca-751b-0410-a591-d2e778427230>2011-01-25 18:17:17 +0300
commit99bbfe938bc379762183f1eceb1e18b967d702ee (patch)
treea55026e1b127990d3ca8871bdeb7bca3570418f3 /moses-chart-cmd
parent67b30ea0c70213783d0b2e4576d9dd747bf12ff8 (diff)
Use OutputCollector to write moses-chart output.
git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@3843 1f5c12ca-751b-0410-a591-d2e778427230
Diffstat (limited to 'moses-chart-cmd')
-rw-r--r--moses-chart-cmd/src/IOWrapper.cpp115
-rw-r--r--moses-chart-cmd/src/IOWrapper.h13
-rw-r--r--moses-chart-cmd/src/Main.cpp19
3 files changed, 100 insertions, 47 deletions
diff --git a/moses-chart-cmd/src/IOWrapper.cpp b/moses-chart-cmd/src/IOWrapper.cpp
index dfcd37b0c..92e9f065a 100644
--- a/moses-chart-cmd/src/IOWrapper.cpp
+++ b/moses-chart-cmd/src/IOWrapper.cpp
@@ -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();
@@ -88,8 +91,14 @@ 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())
{
@@ -97,6 +106,7 @@ IOWrapper::IOWrapper(const std::vector<FactorType> &inputFactorOrder
std::ofstream *file = new std::ofstream;
m_outputSearchGraphStream = file;
file->open(fileName.c_str());
+ m_searchGraphOutputCollector = new Moses::OutputCollector(m_outputSearchGraphStream);
}
// detailed translation reporting
@@ -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)
@@ -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);
@@ -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() << " ";
}
@@ -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);
@@ -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
@@ -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();
@@ -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
@@ -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) << " ";
}
}
@@ -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] << " ";
}
}
}
@@ -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] << " ";
}
@@ -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] << " ";
}
}
}
@@ -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) {
@@ -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);
+}
diff --git a/moses-chart-cmd/src/IOWrapper.h b/moses-chart-cmd/src/IOWrapper.h
index b40080bd5..1c1108cd3 100644
--- a/moses-chart-cmd/src/IOWrapper.h
+++ b/moses-chart-cmd/src/IOWrapper.h
@@ -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
@@ -66,7 +67,10 @@ protected:
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
@@ -79,12 +83,13 @@ public:
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);
};
diff --git a/moses-chart-cmd/src/Main.cpp b/moses-chart-cmd/src/Main.cpp
index 78473b709..a681a9b6b 100644
--- a/moses-chart-cmd/src/Main.cpp
+++ b/moses-chart-cmd/src/Main.cpp
@@ -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;
@@ -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()
);
@@ -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 {