mosesdecoder/contrib/other-builds/moses2/SCFG/Manager.cpp

134 lines
3.4 KiB
C++
Raw Normal View History

2016-02-26 15:26:32 +03:00
/*
* Manager.cpp
*
* Created on: 23 Oct 2015
* Author: hieu
*/
#include <boost/foreach.hpp>
#include <vector>
#include <sstream>
#include "Manager.h"
2016-03-03 16:04:27 +03:00
#include "InputPath.h"
2016-04-17 09:16:58 +03:00
#include "Hypothesis.h"
2016-04-27 11:31:01 +03:00
#include "../PhraseBased/Sentence.h"
2016-03-03 16:04:27 +03:00
#include "../System.h"
#include "../TranslationModel/PhraseTable.h"
2016-02-26 15:26:32 +03:00
using namespace std;
namespace Moses2
{
namespace SCFG
{
2016-03-31 23:00:16 +03:00
Manager::Manager(System &sys, const TranslationTask &task,
const std::string &inputStr, long translationId) :
ManagerBase(sys, task, inputStr, translationId)
2016-02-26 15:51:50 +03:00
{
}
2016-02-26 15:26:32 +03:00
2016-02-26 15:51:50 +03:00
Manager::~Manager()
{
2016-02-26 15:26:32 +03:00
}
void Manager::Decode()
{
2016-03-31 23:00:16 +03:00
// init pools etc
2016-04-16 16:56:15 +03:00
//cerr << "START InitPools()" << endl;
2016-03-31 23:00:16 +03:00
InitPools();
2016-04-16 16:56:15 +03:00
//cerr << "START ParseInput()" << endl;
2016-03-31 23:00:16 +03:00
ParseInput(true);
2016-02-26 15:35:24 +03:00
2016-04-16 20:59:15 +03:00
size_t inputSize = GetInput().GetSize();
2016-04-16 16:56:15 +03:00
//cerr << "size=" << size << endl;
2016-03-02 00:41:32 +03:00
2016-03-31 23:00:16 +03:00
m_inputPaths.Init(GetInput(), *this);
2016-04-16 16:56:15 +03:00
//cerr << "CREATED m_inputPaths" << endl;
2016-03-02 00:41:32 +03:00
2016-04-16 20:59:15 +03:00
m_stacks.Init(*this, inputSize);
2016-04-16 16:56:15 +03:00
//cerr << "CREATED m_stacks" << endl;
2016-03-01 02:28:24 +03:00
2016-04-16 20:59:15 +03:00
for (int startPos = inputSize - 1; startPos >= 0; --startPos) {
2016-03-31 23:00:16 +03:00
InitActiveChart(startPos);
2016-03-02 00:41:32 +03:00
2016-04-16 20:59:15 +03:00
for (int phraseSize = 1; phraseSize < (inputSize - startPos + 1); ++phraseSize) {
2016-04-27 00:18:19 +03:00
SubPhrase<Moses2::Word> sub = m_input->GetSubPhrase(startPos, phraseSize);
2016-04-16 16:56:15 +03:00
//cerr << "sub=" << sub << endl;
2016-04-17 09:16:58 +03:00
Lookup(startPos, phraseSize);
2016-04-16 20:59:15 +03:00
Decode(startPos, phraseSize);
2016-03-31 23:00:16 +03:00
}
}
2016-04-17 21:33:21 +03:00
m_stacks.OutputStacks();
2016-02-26 15:26:32 +03:00
}
2016-03-02 00:41:32 +03:00
void Manager::InitActiveChart(size_t pos)
{
2016-04-15 15:38:01 +03:00
InputPath &path = *m_inputPaths.GetMatrix().GetValue(pos, 0);
2016-04-16 16:56:15 +03:00
//cerr << "pos=" << pos << " path=" << path << endl;
2016-03-31 23:00:16 +03:00
size_t numPt = system.mappings.size();
2016-04-16 16:56:15 +03:00
//cerr << "numPt=" << numPt << endl;
2016-03-31 23:00:16 +03:00
for (size_t i = 0; i < numPt; ++i) {
2016-04-14 17:55:13 +03:00
const PhraseTable &pt = *system.mappings[i];
2016-04-16 16:56:15 +03:00
//cerr << "START InitActiveChart" << endl;
2016-04-14 17:55:13 +03:00
pt.InitActiveChart(path);
2016-04-16 16:56:15 +03:00
//cerr << "FINISHED InitActiveChart" << endl;
2016-03-31 23:00:16 +03:00
}
2016-04-15 15:38:01 +03:00
}
2016-04-17 09:16:58 +03:00
void Manager::Lookup(size_t startPos, size_t size)
2016-04-15 15:38:01 +03:00
{
2016-04-16 20:59:15 +03:00
InputPath &path = *m_inputPaths.GetMatrix().GetValue(startPos, size);
cerr << "path=" << path << endl;
2016-04-15 15:38:01 +03:00
size_t numPt = system.mappings.size();
2016-04-16 16:56:15 +03:00
//cerr << "numPt=" << numPt << endl;
2016-04-14 17:55:13 +03:00
2016-04-15 15:38:01 +03:00
for (size_t i = 0; i < numPt; ++i) {
const PhraseTable &pt = *system.mappings[i];
pt.Lookup(GetPool(), *this, m_stacks, path);
2016-04-15 15:38:01 +03:00
}
2016-04-16 16:56:15 +03:00
/*
2016-04-16 16:56:15 +03:00
size_t tpsNum = path.targetPhrases.GetSize();
if (tpsNum) {
2016-04-17 21:33:21 +03:00
cerr << tpsNum << " " << path << endl;
2016-04-16 16:56:15 +03:00
}
*/
2016-03-02 00:41:32 +03:00
}
2016-04-17 09:16:58 +03:00
void Manager::Decode(size_t startPos, size_t size)
{
2016-04-20 22:22:57 +03:00
//cerr << "size=" << size << " " << startPos << endl;
2016-04-17 09:16:58 +03:00
InputPath &path = *m_inputPaths.GetMatrix().GetValue(startPos, size);
2016-04-17 13:01:01 +03:00
Stack &stack = m_stacks.GetStack(startPos, size);
Recycler<HypothesisBase*> &hypoRecycler = GetHypoRecycle();
2016-04-20 19:13:05 +03:00
boost::unordered_map<SCFG::SymbolBind, SCFG::TargetPhrases>::const_iterator iterOuter;
for (iterOuter = path.targetPhrases.begin(); iterOuter != path.targetPhrases.end(); ++iterOuter) {
2016-04-20 22:22:57 +03:00
const SCFG::SymbolBind &symbolBind = iterOuter->first;
2016-04-20 19:13:05 +03:00
const SCFG::TargetPhrases &tps = iterOuter->second;
2016-04-20 22:22:57 +03:00
//cerr << "symbolBind=" << symbolBind << " " << tps.GetSize() << endl;
2016-04-20 19:13:05 +03:00
SCFG::TargetPhrases::const_iterator iter;
for (iter = tps.begin(); iter != tps.end(); ++iter) {
const SCFG::TargetPhraseImpl &tp = **iter;
SCFG::Hypothesis *hypo = new SCFG::Hypothesis(GetPool(), system);
2016-04-20 22:22:57 +03:00
hypo->Init(*this, path, symbolBind, tp);
2016-04-20 19:13:05 +03:00
StackAdd added = stack.Add(hypo, hypoRecycler, arcLists);
2016-04-20 22:22:57 +03:00
//cerr << "added=" << added.added << " " << (const Phrase&) tp << endl;
2016-04-20 19:13:05 +03:00
}
2016-04-17 09:16:58 +03:00
}
}
2016-02-26 15:26:32 +03:00
}
}