separate out class TranslationTask into it's on file

This commit is contained in:
Hieu Hoang 2014-09-30 12:47:28 +01:00
parent c40faed0d6
commit ebd2e72494
7 changed files with 178 additions and 128 deletions

View File

@ -101,6 +101,16 @@
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/moses-chart-cmd/Main.h</locationURI>
</link>
<link>
<name>TranslationTask.cpp</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/moses-chart-cmd/TranslationTask.cpp</locationURI>
</link>
<link>
<name>TranslationTask.h</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/moses-chart-cmd/TranslationTask.h</locationURI>
</link>
<link>
<name>mbr.cpp</name>
<type>1</type>

View File

@ -1,2 +1,2 @@
exe moses_chart : Main.cpp mbr.cpp ../moses//moses $(TOP)//boost_iostreams ..//boost_filesystem ..//z ;
exe moses_chart : Main.cpp mbr.cpp TranslationTask.cpp ../moses//moses $(TOP)//boost_iostreams ..//boost_filesystem ..//z ;

View File

@ -57,140 +57,19 @@ POSSIBILITY OF SUCH DAMAGE.
#include "moses/ThreadPool.h"
#include "moses/ChartManager.h"
#include "moses/ChartHypothesis.h"
#include "moses/Incremental.h"
#include "moses/FF/StatefulFeatureFunction.h"
#include "moses/FF/StatelessFeatureFunction.h"
#include "util/usage.hh"
#include "util/exception.hh"
#include "TranslationTask.h"
using namespace std;
using namespace Moses;
using namespace MosesChartCmd;
/**
* Translates a sentence.
**/
class TranslationTask : public Task
{
public:
TranslationTask(InputType *source, IOWrapperChart &ioWrapper,
boost::shared_ptr<HypergraphOutput<ChartManager> > hypergraphOutput)
: m_source(source)
, m_ioWrapper(ioWrapper)
, m_hypergraphOutput(hypergraphOutput) {
}
~TranslationTask() {
delete m_source;
}
void Run() {
const StaticData &staticData = StaticData::Instance();
const size_t translationId = m_source->GetTranslationId();
VERBOSE(2,"\nTRANSLATING(" << translationId << "): " << *m_source);
if (staticData.GetSearchAlgorithm() == ChartIncremental) {
Incremental::Manager manager(*m_source);
const std::vector<search::Applied> &nbest = manager.ProcessSentence();
if (!nbest.empty()) {
m_ioWrapper.OutputBestHypo(nbest[0], translationId);
if (staticData.IsDetailedTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
m_ioWrapper.OutputDetailedTranslationReport(&nbest[0], sentence, translationId);
}
if (staticData.IsDetailedTreeFragmentsTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
m_ioWrapper.OutputDetailedTreeFragmentsTranslationReport(&nbest[0], sentence, translationId);
}
} else {
m_ioWrapper.OutputBestNone(translationId);
}
if (staticData.GetNBestSize() > 0)
m_ioWrapper.OutputNBestList(nbest, translationId);
return;
}
ChartManager manager(translationId,*m_source);
manager.ProcessSentence();
UTIL_THROW_IF2(staticData.UseMBR(), "Cannot use MBR");
// Output search graph in hypergraph format for Kenneth Heafield's lazy hypergraph decoder
if (m_hypergraphOutput.get()) {
m_hypergraphOutput->Write(manager);
}
// 1-best
const ChartHypothesis *bestHypo = manager.GetBestHypothesis();
m_ioWrapper.OutputBestHypo(bestHypo, translationId);
IFVERBOSE(2) {
PrintUserTime("Best Hypothesis Generation Time:");
}
if (!staticData.GetAlignmentOutputFile().empty()) {
m_ioWrapper.OutputAlignment(translationId, bestHypo);
}
if (staticData.IsDetailedTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
m_ioWrapper.OutputDetailedTranslationReport(bestHypo, sentence, translationId);
}
if (staticData.IsDetailedTreeFragmentsTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
m_ioWrapper.OutputDetailedTreeFragmentsTranslationReport(bestHypo, sentence, translationId);
}
if (!staticData.GetOutputUnknownsFile().empty()) {
m_ioWrapper.OutputUnknowns(manager.GetParser().GetUnknownSources(),
translationId);
}
//DIMw
if (staticData.IsDetailedAllTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
size_t nBestSize = staticData.GetNBestSize();
std::vector<boost::shared_ptr<ChartKBestExtractor::Derivation> > nBestList;
manager.CalcNBest(nBestSize, nBestList, staticData.GetDistinctNBest());
m_ioWrapper.OutputDetailedAllTranslationReport(nBestList, manager, sentence, translationId);
}
// n-best
size_t nBestSize = staticData.GetNBestSize();
if (nBestSize > 0) {
VERBOSE(2,"WRITING " << nBestSize << " TRANSLATION ALTERNATIVES TO " << staticData.GetNBestFilePath() << endl);
std::vector<boost::shared_ptr<ChartKBestExtractor::Derivation> > nBestList;
manager.CalcNBest(nBestSize, nBestList,staticData.GetDistinctNBest());
m_ioWrapper.OutputNBestList(nBestList, translationId);
IFVERBOSE(2) {
PrintUserTime("N-Best Hypotheses Generation Time:");
}
}
if (staticData.GetOutputSearchGraph()) {
std::ostringstream out;
manager.OutputSearchGraphMoses( out);
OutputCollector *oc = m_ioWrapper.GetSearchGraphOutputCollector();
UTIL_THROW_IF2(oc == NULL, "File for search graph output not specified");
oc->Write(translationId, out.str());
}
IFVERBOSE(2) {
PrintUserTime("Sentence Decoding Time:");
}
manager.CalcDecoderStatistics();
}
private:
// Non-copyable: copy constructor and assignment operator not implemented.
TranslationTask(const TranslationTask &);
TranslationTask &operator=(const TranslationTask &);
InputType *m_source;
IOWrapperChart &m_ioWrapper;
boost::shared_ptr<HypergraphOutput<ChartManager> > m_hypergraphOutput;
};
bool ReadInput(IOWrapperChart &ioWrapper, InputTypeEnum inputType, InputType*& source)
{

View File

@ -0,0 +1,118 @@
#include "TranslationTask.h"
#include "moses/Sentence.h"
#include "moses/StaticData.h"
#include "moses/Incremental.h"
#include "moses/IOWrapperChart.h"
#include "moses/OutputCollector.h"
using namespace std;
using namespace Moses;
using namespace MosesChartCmd;
TranslationTask::TranslationTask(InputType *source, IOWrapperChart &ioWrapper,
boost::shared_ptr<HypergraphOutput<ChartManager> > hypergraphOutput)
: m_source(source)
, m_ioWrapper(ioWrapper)
, m_hypergraphOutput(hypergraphOutput)
{}
TranslationTask::~TranslationTask() {
delete m_source;
}
void TranslationTask::Run() {
const StaticData &staticData = StaticData::Instance();
const size_t translationId = m_source->GetTranslationId();
VERBOSE(2,"\nTRANSLATING(" << translationId << "): " << *m_source);
if (staticData.GetSearchAlgorithm() == ChartIncremental) {
Incremental::Manager manager(*m_source);
const std::vector<search::Applied> &nbest = manager.ProcessSentence();
if (!nbest.empty()) {
m_ioWrapper.OutputBestHypo(nbest[0], translationId);
if (staticData.IsDetailedTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
m_ioWrapper.OutputDetailedTranslationReport(&nbest[0], sentence, translationId);
}
if (staticData.IsDetailedTreeFragmentsTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
m_ioWrapper.OutputDetailedTreeFragmentsTranslationReport(&nbest[0], sentence, translationId);
}
} else {
m_ioWrapper.OutputBestNone(translationId);
}
if (staticData.GetNBestSize() > 0)
m_ioWrapper.OutputNBestList(nbest, translationId);
return;
}
ChartManager manager(translationId,*m_source);
manager.ProcessSentence();
UTIL_THROW_IF2(staticData.UseMBR(), "Cannot use MBR");
// Output search graph in hypergraph format for Kenneth Heafield's lazy hypergraph decoder
if (m_hypergraphOutput.get()) {
m_hypergraphOutput->Write(manager);
}
// 1-best
const ChartHypothesis *bestHypo = manager.GetBestHypothesis();
m_ioWrapper.OutputBestHypo(bestHypo, translationId);
IFVERBOSE(2) {
PrintUserTime("Best Hypothesis Generation Time:");
}
if (!staticData.GetAlignmentOutputFile().empty()) {
m_ioWrapper.OutputAlignment(translationId, bestHypo);
}
if (staticData.IsDetailedTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
m_ioWrapper.OutputDetailedTranslationReport(bestHypo, sentence, translationId);
}
if (staticData.IsDetailedTreeFragmentsTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
m_ioWrapper.OutputDetailedTreeFragmentsTranslationReport(bestHypo, sentence, translationId);
}
if (!staticData.GetOutputUnknownsFile().empty()) {
m_ioWrapper.OutputUnknowns(manager.GetParser().GetUnknownSources(),
translationId);
}
//DIMw
if (staticData.IsDetailedAllTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
size_t nBestSize = staticData.GetNBestSize();
std::vector<boost::shared_ptr<ChartKBestExtractor::Derivation> > nBestList;
manager.CalcNBest(nBestSize, nBestList, staticData.GetDistinctNBest());
m_ioWrapper.OutputDetailedAllTranslationReport(nBestList, manager, sentence, translationId);
}
// n-best
size_t nBestSize = staticData.GetNBestSize();
if (nBestSize > 0) {
VERBOSE(2,"WRITING " << nBestSize << " TRANSLATION ALTERNATIVES TO " << staticData.GetNBestFilePath() << endl);
std::vector<boost::shared_ptr<ChartKBestExtractor::Derivation> > nBestList;
manager.CalcNBest(nBestSize, nBestList,staticData.GetDistinctNBest());
m_ioWrapper.OutputNBestList(nBestList, translationId);
IFVERBOSE(2) {
PrintUserTime("N-Best Hypotheses Generation Time:");
}
}
if (staticData.GetOutputSearchGraph()) {
std::ostringstream out;
manager.OutputSearchGraphMoses( out);
OutputCollector *oc = m_ioWrapper.GetSearchGraphOutputCollector();
UTIL_THROW_IF2(oc == NULL, "File for search graph output not specified");
oc->Write(translationId, out.str());
}
IFVERBOSE(2) {
PrintUserTime("Sentence Decoding Time:");
}
manager.CalcDecoderStatistics();
}

View File

@ -0,0 +1,41 @@
#pragma once
#include <boost/smart_ptr/shared_ptr.hpp>
#include "moses/ThreadPool.h"
#include "moses/ChartManager.h"
#include "moses/HypergraphOutput.h"
namespace Moses
{
class InputType;
class OutputCollector;
}
namespace MosesChartCmd
{
class IOWrapperChart;
}
/**
* Translates a sentence.
**/
class TranslationTask : public Moses::Task
{
public:
TranslationTask(Moses::InputType *source, MosesChartCmd::IOWrapperChart &ioWrapper,
boost::shared_ptr<Moses::HypergraphOutput<Moses::ChartManager> > hypergraphOutput);
~TranslationTask();
void Run();
private:
// Non-copyable: copy constructor and assignment operator not implemented.
TranslationTask(const TranslationTask &);
TranslationTask &operator=(const TranslationTask &);
Moses::InputType *m_source;
MosesChartCmd::IOWrapperChart &m_ioWrapper;
boost::shared_ptr<Moses::HypergraphOutput<Moses::ChartManager> > m_hypergraphOutput;
};

View File

@ -35,6 +35,10 @@ TranslationTask::TranslationTask(size_t lineNumber,
m_hypergraphOutput(hypergraphOutput)
{}
TranslationTask::~TranslationTask() {
delete m_source;
}
void TranslationTask::Run() {
// shorthand for "global data"
const StaticData &staticData = StaticData::Instance();

View File

@ -34,9 +34,7 @@ public:
bool outputSearchGraphSLF,
boost::shared_ptr<Moses::HypergraphOutput<Moses::Manager> > hypergraphOutput);
~TranslationTask() {
delete m_source;
}
~TranslationTask();
/** Translate one sentence
* gets called by main function implemented at end of this source file */