Use TranslationTask objects to perform sentence decoding in moses-chart.

git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@3846 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
pjwilliams 2011-01-25 17:15:30 +00:00
parent ec00f9a916
commit 8051c5ad35
5 changed files with 77 additions and 76 deletions

View File

@ -53,6 +53,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "TreeInput.h"
#include "TranslationAnalysis.h"
#include "mbr.h"
#include "ThreadPool.h"
#include "../../moses-chart/src/ChartManager.h"
#include "../../moses-chart/src/ChartHypothesis.h"
#include "../../moses-chart/src/ChartTrellisPathList.h"
@ -68,6 +69,72 @@ using namespace std;
using namespace Moses;
using namespace MosesChart;
/**
* Translates a sentence.
**/
class TranslationTask : public Task
{
public:
TranslationTask(InputType *source, IOWrapper &ioWrapper)
: m_source(source)
, m_ioWrapper(ioWrapper)
{}
~TranslationTask() { delete m_source; }
void Run()
{
const StaticData &staticData = StaticData::Instance();
const TranslationSystem &system = staticData.GetTranslationSystem(TranslationSystem::DEFAULT);
const size_t lineNumber = m_source->GetTranslationId();
VERBOSE(2,"\nTRANSLATING(" << lineNumber << "): " << *m_source);
MosesChart::Manager manager(*m_source, &system);
manager.ProcessSentence();
assert(!staticData.UseMBR());
// 1-best
const MosesChart::Hypothesis *bestHypo = manager.GetBestHypothesis();
m_ioWrapper.OutputBestHypo(bestHypo, lineNumber,
staticData.GetReportSegmentation(),
staticData.GetReportAllFactors());
IFVERBOSE(2) { PrintUserTime("Best Hypothesis Generation Time:"); }
// n-best
size_t nBestSize = staticData.GetNBestSize();
if (nBestSize > 0)
{
VERBOSE(2,"WRITING " << nBestSize << " TRANSLATION ALTERNATIVES TO " << staticData.GetNBestFilePath() << endl);
MosesChart::TrellisPathList nBestList;
manager.CalcNBest(nBestSize, nBestList,staticData.GetDistinctNBest());
m_ioWrapper.OutputNBestList(nBestList, bestHypo, &system, lineNumber);
IFVERBOSE(2) { PrintUserTime("N-Best Hypotheses Generation Time:"); }
}
if (staticData.GetOutputSearchGraph())
{
std::ostringstream out;
manager.GetSearchGraph(lineNumber, out);
OutputCollector *oc = m_ioWrapper.GetSearchGraphOutputCollector();
assert(oc);
oc->Write(lineNumber, 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;
IOWrapper &m_ioWrapper;
};
bool ReadInput(IOWrapper &ioWrapper, InputTypeEnum inputType, InputType*& source)
{
delete source;
@ -168,84 +235,16 @@ int main(int argc, char* argv[])
// read each sentence & decode
InputType *source=0;
size_t lineCount = 0;
while(ReadInput(*ioWrapper,staticData.GetInputType(),source))
{
// note: source is only valid within this while loop!
IFVERBOSE(1)
ResetUserTime();
VERBOSE(2,"\nTRANSLATING(" << ++lineCount << "): " << *source);
//cerr << *source << endl;
const TranslationSystem& system = staticData.GetTranslationSystem(TranslationSystem::DEFAULT);
MosesChart::Manager manager(*source, &system);
manager.ProcessSentence();
TranslationTask *task = new TranslationTask(source, *ioWrapper);
source = NULL; // TranslationTask is responsible for deleting source.
task->Run();
delete task;
}
assert(!staticData.UseMBR());
if (!staticData.UseMBR()){
const MosesChart::Hypothesis *bestHypo = manager.GetBestHypothesis();
ioWrapper->OutputBestHypo(bestHypo, source->GetTranslationId(),
staticData.GetReportSegmentation(),
staticData.GetReportAllFactors()
);
IFVERBOSE(2) { PrintUserTime("Best Hypothesis Generation Time:"); }
// n-best
size_t nBestSize = staticData.GetNBestSize();
if (nBestSize > 0)
{
VERBOSE(2,"WRITING " << nBestSize << " TRANSLATION ALTERNATIVES TO " << staticData.GetNBestFilePath() << endl);
MosesChart::TrellisPathList nBestList;
manager.CalcNBest(nBestSize, nBestList,staticData.GetDistinctNBest());
ioWrapper->OutputNBestList(nBestList, bestHypo, &system, source->GetTranslationId());
IFVERBOSE(2) { PrintUserTime("N-Best Hypotheses Generation Time:"); }
}
if (staticData.GetOutputSearchGraph())
{
std::ostringstream out;
manager.GetSearchGraph(source->GetTranslationId(), out);
OutputCollector *oc = ioWrapper->GetSearchGraphOutputCollector();
assert(oc);
oc->Write(source->GetTranslationId(), out.str());
}
}
else {
/*
size_t nBestSize = staticData.GetNBestSize();
cerr << "NBEST SIZE : " << nBestSize << endl;
assert(nBestSize > 0);
if (nBestSize > 0)
{
VERBOSE(2,"WRITING " << nBestSize << " TRANSLATION ALTERNATIVES TO " << staticData.GetNBestFilePath() << endl);
MosesChart::TrellisPathList nBestList;
manager.CalcNBest(nBestSize, nBestList,true);
std::vector<const Factor*> mbrBestHypo = doMBR(nBestList);
ioWrapper->OutputBestHypo(mbrBestHypo, source->GetTranslationId(),
staticData.GetReportSegmentation(),
staticData.GetReportAllFactors()
);
IFVERBOSE(2) { PrintUserTime("N-Best Hypotheses Generation Time:"); }
}
*/
}
/*
if (staticData.IsDetailedTranslationReportingEnabled()) {
TranslationAnalysis::PrintTranslationAnalysis(std::cerr, manager.GetBestHypothesis());
}
*/
IFVERBOSE(2) { PrintUserTime("Sentence Decoding Time:"); }
manager.CalcDecoderStatistics();
} // while(ReadInput
delete ioWrapper;
IFVERBOSE(1)

View File

@ -5,7 +5,7 @@ AM_CPPFLAGS = -W -Wall -ffor-scope -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -DUSE_H
checkplf_SOURCES = checkplf.cpp
checkplf_LDADD = $(top_builddir)/moses/src/libmoses.la -L$(top_srcdir)/OnDiskPt/src -lOnDiskPt @KENLM_LDFLAGS@ $(BOOST_THREAD_LDFLAGS) $(BOOST_THREAD_LIBS)
moses_SOURCES = Main.cpp mbr.cpp IOWrapper.cpp TranslationAnalysis.cpp LatticeMBR.cpp ThreadPool.cpp
moses_SOURCES = Main.cpp mbr.cpp IOWrapper.cpp TranslationAnalysis.cpp LatticeMBR.cpp
moses_LDADD = $(top_builddir)/moses/src/libmoses.la -L$(top_srcdir)/OnDiskPt/src -lOnDiskPt @KENLM_LDFLAGS@ $(BOOST_THREAD_LDFLAGS) $(BOOST_THREAD_LIBS)

View File

@ -91,6 +91,7 @@ libmoses_la_HEADERS = \
StaticData.h \
TargetPhrase.h \
TargetPhraseCollection.h \
ThreadPool.h \
Timer.h \
TranslationOption.h \
TranslationOptionCollection.h \
@ -217,6 +218,7 @@ libmoses_la_SOURCES = \
StaticData.cpp \
TargetPhrase.cpp \
TargetPhraseCollection.cpp \
ThreadPool.cpp \
Timer.cpp \
TranslationOption.cpp \
TranslationOptionCollection.cpp \

View File

@ -19,8 +19,8 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************/
#ifndef moses_cmd_ThreadPool_h
#define moses_cmd_ThreadPool_h
#ifndef moses_ThreadPool_h
#define moses_ThreadPool_h
#include <iostream>
#include <queue>