Skip to content

Commit

Permalink
Use TranslationTask objects to perform sentence decoding in moses-chart.
Browse files Browse the repository at this point in the history
git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@3846 1f5c12ca-751b-0410-a591-d2e778427230
  • Loading branch information
pjwilliams committed Jan 25, 2011
1 parent 9a4d6d4 commit 46bc5bc
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 77 deletions.
147 changes: 73 additions & 74 deletions moses-chart-cmd/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -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();

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

TranslationTask *task = new TranslationTask(source, *ioWrapper);
source = NULL; // TranslationTask is responsible for deleting source.
task->Run();
delete task;
}

delete ioWrapper;

IFVERBOSE(1)
Expand Down
2 changes: 1 addition & 1 deletion moses-cmd/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 2 additions & 0 deletions moses/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ libmoses_la_HEADERS = \
StaticData.h \
TargetPhrase.h \
TargetPhraseCollection.h \
ThreadPool.h \
Timer.h \
TranslationOption.h \
TranslationOptionCollection.h \
Expand Down Expand Up @@ -217,6 +218,7 @@ libmoses_la_SOURCES = \
StaticData.cpp \
TargetPhrase.cpp \
TargetPhraseCollection.cpp \
ThreadPool.cpp \
Timer.cpp \
TranslationOption.cpp \
TranslationOptionCollection.cpp \
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions moses-cmd/src/ThreadPool.h → moses/src/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 46bc5bc

Please sign in to comment.