2014-09-30 15:25:36 +04:00
|
|
|
#include "TranslationTask.h"
|
|
|
|
#include "moses/StaticData.h"
|
|
|
|
#include "moses/Sentence.h"
|
|
|
|
#include "moses/IOWrapper.h"
|
|
|
|
#include "moses/TranslationAnalysis.h"
|
|
|
|
#include "moses/TypeDef.h"
|
|
|
|
#include "moses/Util.h"
|
|
|
|
#include "moses/InputType.h"
|
|
|
|
#include "moses/OutputCollector.h"
|
2014-10-10 16:19:02 +04:00
|
|
|
#include "moses/Incremental.h"
|
2014-09-30 15:25:36 +04:00
|
|
|
#include "mbr.h"
|
|
|
|
|
2014-11-08 20:36:32 +03:00
|
|
|
#include "moses/Syntax/S2T/Parsers/RecursiveCYKPlusParser/RecursiveCYKPlusParser.h"
|
|
|
|
#include "moses/Syntax/S2T/Parsers/Scope3Parser/Parser.h"
|
|
|
|
|
2014-10-10 16:19:02 +04:00
|
|
|
#include "util/exception.hh"
|
|
|
|
|
2014-09-30 15:25:36 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2014-10-09 15:52:06 +04:00
|
|
|
namespace Moses
|
2014-09-30 15:25:36 +04:00
|
|
|
{
|
|
|
|
|
2014-12-06 00:33:59 +03:00
|
|
|
TranslationTask::TranslationTask(InputType* source, Moses::IOWrapper &ioWrapper, int pbOrChart)
|
2014-10-10 16:19:02 +04:00
|
|
|
: m_source(source)
|
|
|
|
, m_ioWrapper(ioWrapper)
|
2014-12-06 00:33:59 +03:00
|
|
|
, m_pbOrChart(pbOrChart)
|
2014-09-30 15:25:36 +04:00
|
|
|
{}
|
|
|
|
|
2014-09-30 15:47:28 +04:00
|
|
|
TranslationTask::~TranslationTask() {
|
|
|
|
delete m_source;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:19:02 +04:00
|
|
|
void TranslationTask::Run()
|
|
|
|
{
|
|
|
|
switch (m_pbOrChart)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
RunPb();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
RunChart();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UTIL_THROW(util::Exception, "Unknown value: " << m_pbOrChart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TranslationTask::RunPb()
|
|
|
|
{
|
2014-09-30 15:25:36 +04:00
|
|
|
// shorthand for "global data"
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
|
|
|
|
// input sentence
|
|
|
|
Sentence sentence;
|
|
|
|
|
|
|
|
// report wall time spent on translation
|
|
|
|
Timer translationTime;
|
|
|
|
translationTime.start();
|
|
|
|
|
|
|
|
// report thread number
|
|
|
|
#if defined(WITH_THREADS) && defined(BOOST_HAS_PTHREADS)
|
2014-10-07 22:16:30 +04:00
|
|
|
TRACE_ERR("Translating line " << m_source->GetTranslationId() << " in thread id " << pthread_self() << endl);
|
2014-09-30 15:25:36 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// execute the translation
|
|
|
|
// note: this executes the search, resulting in a search graph
|
|
|
|
// we still need to apply the decision rule (MAP, MBR, ...)
|
|
|
|
Timer initTime;
|
|
|
|
initTime.start();
|
2014-10-07 22:16:30 +04:00
|
|
|
Manager manager(*m_source,staticData.GetSearchAlgorithm());
|
|
|
|
VERBOSE(1, "Line " << m_source->GetTranslationId() << ": Initialize search took " << initTime << " seconds total" << endl);
|
2014-12-05 20:59:53 +03:00
|
|
|
manager.Decode();
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
// we are done with search, let's look what we got
|
|
|
|
Timer additionalReportingTime;
|
|
|
|
additionalReportingTime.start();
|
|
|
|
|
|
|
|
// output word graph
|
2014-12-05 20:23:08 +03:00
|
|
|
manager.OutputWordGraph(m_ioWrapper.GetWordGraphCollector());
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
// output search graph
|
2014-12-05 20:23:08 +03:00
|
|
|
manager.OutputSearchGraph(m_ioWrapper.GetSearchGraphOutputCollector());
|
2014-09-30 15:25:36 +04:00
|
|
|
|
2014-12-05 22:40:51 +03:00
|
|
|
manager.OutputSearchGraphSLF();
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
// Output search graph in hypergraph format for Kenneth Heafield's lazy hypergraph decoder
|
2014-12-06 00:33:59 +03:00
|
|
|
manager.OutputSearchGraphHypergraph();
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
additionalReportingTime.stop();
|
|
|
|
|
|
|
|
// apply decision rule and output best translation(s)
|
2014-10-01 20:43:51 +04:00
|
|
|
if (m_ioWrapper.GetSingleBestOutputCollector()) {
|
2014-09-30 15:25:36 +04:00
|
|
|
ostringstream out;
|
|
|
|
ostringstream debug;
|
2014-12-02 22:09:10 +03:00
|
|
|
FixPrecision(debug,PRECISION);
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
// all derivations - send them to debug stream
|
|
|
|
if (staticData.PrintAllDerivations()) {
|
|
|
|
additionalReportingTime.start();
|
2014-10-07 22:16:30 +04:00
|
|
|
manager.PrintAllDerivations(m_source->GetTranslationId(), debug);
|
2014-09-30 15:25:36 +04:00
|
|
|
additionalReportingTime.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer decisionRuleTime;
|
|
|
|
decisionRuleTime.start();
|
|
|
|
|
|
|
|
// MAP decoding: best hypothesis
|
|
|
|
const Hypothesis* bestHypo = NULL;
|
|
|
|
if (!staticData.UseMBR()) {
|
|
|
|
bestHypo = manager.GetBestHypothesis();
|
|
|
|
if (bestHypo) {
|
|
|
|
if (StaticData::Instance().GetOutputHypoScore()) {
|
|
|
|
out << bestHypo->GetTotalScore() << ' ';
|
|
|
|
}
|
|
|
|
if (staticData.IsPathRecoveryEnabled()) {
|
2014-10-13 15:46:58 +04:00
|
|
|
m_ioWrapper.OutputInput(out, bestHypo);
|
2014-09-30 15:25:36 +04:00
|
|
|
out << "||| ";
|
|
|
|
}
|
2014-11-28 21:29:03 +03:00
|
|
|
|
2014-11-28 21:33:45 +03:00
|
|
|
const PARAM_VEC *params = staticData.GetParameter().GetParam("print-id");
|
2014-11-28 21:29:03 +03:00
|
|
|
if (params && params->size() && Scan<bool>(params->at(0)) ) {
|
2014-09-30 15:25:36 +04:00
|
|
|
out << m_source->GetTranslationId() << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (staticData.GetReportSegmentation() == 2) {
|
|
|
|
manager.GetOutputLanguageModelOrder(out, bestHypo);
|
|
|
|
}
|
2014-10-13 15:46:58 +04:00
|
|
|
m_ioWrapper.OutputBestSurface(
|
2014-09-30 15:25:36 +04:00
|
|
|
out,
|
|
|
|
bestHypo,
|
|
|
|
staticData.GetOutputFactorOrder(),
|
|
|
|
staticData.GetReportSegmentation(),
|
|
|
|
staticData.GetReportAllFactors());
|
|
|
|
if (staticData.PrintAlignmentInfo()) {
|
|
|
|
out << "||| ";
|
2014-10-13 15:46:58 +04:00
|
|
|
m_ioWrapper.OutputAlignment(out, bestHypo);
|
2014-09-30 15:25:36 +04:00
|
|
|
}
|
|
|
|
|
2014-12-03 15:05:35 +03:00
|
|
|
manager.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector());
|
|
|
|
|
2014-09-30 15:25:36 +04:00
|
|
|
IFVERBOSE(1) {
|
|
|
|
debug << "BEST TRANSLATION: " << *bestHypo << endl;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VERBOSE(1, "NO BEST TRANSLATION" << endl);
|
|
|
|
}
|
|
|
|
|
|
|
|
out << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// MBR decoding (n-best MBR, lattice MBR, consensus)
|
|
|
|
else {
|
|
|
|
// we first need the n-best translations
|
|
|
|
size_t nBestSize = staticData.GetMBRSize();
|
|
|
|
if (nBestSize <= 0) {
|
|
|
|
cerr << "ERROR: negative size for number of MBR candidate translations not allowed (option mbr-size)" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
TrellisPathList nBestList;
|
|
|
|
manager.CalcNBest(nBestSize, nBestList,true);
|
|
|
|
VERBOSE(2,"size of n-best: " << nBestList.GetSize() << " (" << nBestSize << ")" << endl);
|
|
|
|
IFVERBOSE(2) {
|
|
|
|
PrintUserTime("calculated n-best list for (L)MBR decoding");
|
|
|
|
}
|
|
|
|
|
|
|
|
// lattice MBR
|
|
|
|
if (staticData.UseLatticeMBR()) {
|
2014-10-01 21:21:57 +04:00
|
|
|
if (m_ioWrapper.GetNBestOutputCollector()) {
|
2014-09-30 15:25:36 +04:00
|
|
|
//lattice mbr nbest
|
|
|
|
vector<LatticeMBRSolution> solutions;
|
|
|
|
size_t n = min(nBestSize, staticData.GetNBestSize());
|
|
|
|
getLatticeMBRNBest(manager,nBestList,solutions,n);
|
|
|
|
ostringstream out;
|
2014-10-13 15:46:58 +04:00
|
|
|
m_ioWrapper.OutputLatticeMBRNBest(out, solutions,m_source->GetTranslationId());
|
2014-10-07 22:16:30 +04:00
|
|
|
m_ioWrapper.GetNBestOutputCollector()->Write(m_source->GetTranslationId(), out.str());
|
2014-09-30 15:25:36 +04:00
|
|
|
} else {
|
|
|
|
//Lattice MBR decoding
|
|
|
|
vector<Word> mbrBestHypo = doLatticeMBR(manager,nBestList);
|
2014-10-13 15:46:58 +04:00
|
|
|
m_ioWrapper.OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(), staticData.GetReportSegmentation(),
|
2014-09-30 15:25:36 +04:00
|
|
|
staticData.GetReportAllFactors(),out);
|
|
|
|
IFVERBOSE(2) {
|
|
|
|
PrintUserTime("finished Lattice MBR decoding");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// consensus decoding
|
|
|
|
else if (staticData.UseConsensusDecoding()) {
|
|
|
|
const TrellisPath &conBestHypo = doConsensusDecoding(manager,nBestList);
|
2014-10-13 15:46:58 +04:00
|
|
|
m_ioWrapper.OutputBestHypo(conBestHypo, m_source->GetTranslationId(),
|
2014-09-30 15:25:36 +04:00
|
|
|
staticData.GetReportSegmentation(),
|
|
|
|
staticData.GetReportAllFactors(),out);
|
2014-10-13 15:46:58 +04:00
|
|
|
m_ioWrapper.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), conBestHypo);
|
2014-09-30 15:25:36 +04:00
|
|
|
IFVERBOSE(2) {
|
|
|
|
PrintUserTime("finished Consensus decoding");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// n-best MBR decoding
|
|
|
|
else {
|
|
|
|
const TrellisPath &mbrBestHypo = doMBR(nBestList);
|
2014-10-13 15:46:58 +04:00
|
|
|
m_ioWrapper.OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(),
|
2014-09-30 15:25:36 +04:00
|
|
|
staticData.GetReportSegmentation(),
|
|
|
|
staticData.GetReportAllFactors(),out);
|
2014-10-13 15:46:58 +04:00
|
|
|
m_ioWrapper.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), mbrBestHypo);
|
2014-09-30 15:25:36 +04:00
|
|
|
IFVERBOSE(2) {
|
|
|
|
PrintUserTime("finished MBR decoding");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// report best translation to output collector
|
2014-10-07 22:16:30 +04:00
|
|
|
m_ioWrapper.GetSingleBestOutputCollector()->Write(m_source->GetTranslationId(),out.str(),debug.str());
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
decisionRuleTime.stop();
|
2014-10-07 22:16:30 +04:00
|
|
|
VERBOSE(1, "Line " << m_source->GetTranslationId() << ": Decision rule took " << decisionRuleTime << " seconds total" << endl);
|
2014-09-30 15:25:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
additionalReportingTime.start();
|
|
|
|
|
|
|
|
// output n-best list
|
2014-12-02 17:38:03 +03:00
|
|
|
manager.OutputNBest(m_ioWrapper.GetNBestOutputCollector());
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
//lattice samples
|
2014-12-02 17:58:44 +03:00
|
|
|
manager.OutputLatticeSamples(m_ioWrapper.GetLatticeSamplesCollector());
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
// detailed translation reporting
|
2014-12-04 19:46:14 +03:00
|
|
|
manager.OutputDetailedTranslationReport(m_ioWrapper.GetDetailedTranslationCollector());
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
//list of unknown words
|
2014-12-04 19:25:19 +03:00
|
|
|
manager.OutputUnknowns(m_ioWrapper.GetUnknownsCollector());
|
2014-09-30 15:25:36 +04:00
|
|
|
|
|
|
|
// report additional statistics
|
|
|
|
manager.CalcDecoderStatistics();
|
2014-10-07 22:16:30 +04:00
|
|
|
VERBOSE(1, "Line " << m_source->GetTranslationId() << ": Additional reporting took " << additionalReportingTime << " seconds total" << endl);
|
|
|
|
VERBOSE(1, "Line " << m_source->GetTranslationId() << ": Translation took " << translationTime << " seconds total" << endl);
|
2014-09-30 15:25:36 +04:00
|
|
|
IFVERBOSE(2) {
|
|
|
|
PrintUserTime("Sentence Decoding Time:");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-10 16:19:02 +04:00
|
|
|
void TranslationTask::RunChart()
|
|
|
|
{
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
const size_t translationId = m_source->GetTranslationId();
|
|
|
|
|
|
|
|
VERBOSE(2,"\nTRANSLATING(" << translationId << "): " << *m_source);
|
|
|
|
|
2014-11-08 20:36:32 +03:00
|
|
|
if (staticData.UseS2TDecoder()) {
|
|
|
|
S2TParsingAlgorithm algorithm = staticData.GetS2TParsingAlgorithm();
|
|
|
|
if (algorithm == RecursiveCYKPlus) {
|
|
|
|
typedef Syntax::S2T::EagerParserCallback Callback;
|
|
|
|
typedef Syntax::S2T::RecursiveCYKPlusParser<Callback> Parser;
|
|
|
|
DecodeS2T<Parser>();
|
|
|
|
} else if (algorithm == Scope3) {
|
|
|
|
typedef Syntax::S2T::StandardParserCallback Callback;
|
|
|
|
typedef Syntax::S2T::Scope3Parser<Callback> Parser;
|
|
|
|
DecodeS2T<Parser>();
|
|
|
|
} else {
|
|
|
|
UTIL_THROW2("ERROR: unhandled S2T parsing algorithm");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:19:02 +04:00
|
|
|
if (staticData.GetSearchAlgorithm() == ChartIncremental) {
|
|
|
|
Incremental::Manager manager(*m_source);
|
2014-12-05 20:59:53 +03:00
|
|
|
manager.Decode();
|
|
|
|
const std::vector<search::Applied> &nbest = manager.GetNBest();
|
2014-10-10 16:19:02 +04:00
|
|
|
if (!nbest.empty()) {
|
|
|
|
m_ioWrapper.OutputBestHypo(nbest[0], translationId);
|
2014-12-03 20:04:10 +03:00
|
|
|
|
|
|
|
manager.OutputDetailedTranslationReport(m_ioWrapper.GetDetailedTranslationCollector());
|
2014-12-04 21:35:19 +03:00
|
|
|
manager.OutputDetailedTreeFragmentsTranslationReport(m_ioWrapper.GetDetailTreeFragmentsOutputCollector());
|
2014-12-03 20:04:10 +03:00
|
|
|
|
2014-10-10 16:19:02 +04:00
|
|
|
} else {
|
|
|
|
m_ioWrapper.OutputBestNone(translationId);
|
|
|
|
}
|
2014-12-02 22:09:10 +03:00
|
|
|
|
|
|
|
manager.OutputNBest(m_ioWrapper.GetNBestOutputCollector());
|
|
|
|
|
2014-10-10 16:19:02 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChartManager manager(*m_source);
|
2014-12-05 20:59:53 +03:00
|
|
|
manager.Decode();
|
2014-10-10 16:19:02 +04:00
|
|
|
|
|
|
|
UTIL_THROW_IF2(staticData.UseMBR(), "Cannot use MBR");
|
|
|
|
|
|
|
|
// Output search graph in hypergraph format for Kenneth Heafield's lazy hypergraph decoder
|
2014-12-06 00:33:59 +03:00
|
|
|
manager.OutputSearchGraphHypergraph();
|
2014-10-10 16:19:02 +04:00
|
|
|
|
|
|
|
// 1-best
|
|
|
|
const ChartHypothesis *bestHypo = manager.GetBestHypothesis();
|
|
|
|
m_ioWrapper.OutputBestHypo(bestHypo, translationId);
|
|
|
|
IFVERBOSE(2) {
|
|
|
|
PrintUserTime("Best Hypothesis Generation Time:");
|
|
|
|
}
|
|
|
|
|
2014-12-03 15:05:35 +03:00
|
|
|
manager.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector());
|
2014-12-03 20:04:10 +03:00
|
|
|
manager.OutputDetailedTranslationReport(m_ioWrapper.GetDetailedTranslationCollector());
|
2014-12-04 21:35:19 +03:00
|
|
|
manager.OutputDetailedTreeFragmentsTranslationReport(m_ioWrapper.GetDetailTreeFragmentsOutputCollector());
|
2014-12-04 19:25:19 +03:00
|
|
|
manager.OutputUnknowns(m_ioWrapper.GetUnknownsCollector());
|
2014-10-10 16:19:02 +04:00
|
|
|
|
|
|
|
// n-best
|
2014-12-02 20:40:53 +03:00
|
|
|
manager.OutputNBest(m_ioWrapper.GetNBestOutputCollector());
|
2014-10-10 16:19:02 +04:00
|
|
|
|
2014-12-05 20:23:08 +03:00
|
|
|
manager.OutputSearchGraph(m_ioWrapper.GetSearchGraphOutputCollector());
|
2014-10-10 16:19:02 +04:00
|
|
|
|
|
|
|
IFVERBOSE(2) {
|
|
|
|
PrintUserTime("Sentence Decoding Time:");
|
|
|
|
}
|
|
|
|
manager.CalcDecoderStatistics();
|
|
|
|
}
|
|
|
|
|
2014-09-30 15:25:36 +04:00
|
|
|
}
|