2010-01-28 13:39:43 +03:00
|
|
|
#include <sstream>
|
|
|
|
|
2013-05-24 21:02:49 +04:00
|
|
|
#include "moses/FF/FFState.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "LexicalReordering.h"
|
2010-01-28 13:39:43 +03:00
|
|
|
#include "LexicalReorderingState.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "StaticData.h"
|
|
|
|
|
2013-01-15 22:32:13 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2013-01-15 22:32:13 +04:00
|
|
|
LexicalReordering::LexicalReordering(const std::string &line)
|
2013-05-29 21:16:15 +04:00
|
|
|
: StatefulFeatureFunction("LexicalReordering", line)
|
2011-02-24 16:14:42 +03:00
|
|
|
{
|
2013-01-15 22:32:13 +04:00
|
|
|
std::cerr << "Initializing LexicalReordering.." << std::endl;
|
|
|
|
|
2013-02-03 22:16:42 +04:00
|
|
|
for (size_t i = 0; i < m_args.size(); ++i) {
|
|
|
|
const vector<string> &args = m_args[i];
|
|
|
|
|
|
|
|
if (args[0] == "type") {
|
|
|
|
m_configuration = new LexicalReorderingConfiguration(args[1]);
|
|
|
|
m_configuration->SetScoreProducer(this);
|
|
|
|
m_modelTypeString = m_configuration->GetModelString();
|
2013-05-29 21:16:15 +04:00
|
|
|
} else if (args[0] == "input-factor") {
|
2013-06-01 02:04:05 +04:00
|
|
|
m_factorsF =Tokenize<FactorType>(args[1]);
|
2013-05-29 21:16:15 +04:00
|
|
|
} else if (args[0] == "output-factor") {
|
2013-06-01 02:04:05 +04:00
|
|
|
m_factorsE =Tokenize<FactorType>(args[1]);
|
2013-05-29 21:16:15 +04:00
|
|
|
} else if (args[0] == "path") {
|
2013-06-01 02:04:05 +04:00
|
|
|
m_filePath = args[1];
|
2013-05-29 21:16:15 +04:00
|
|
|
} else {
|
2013-02-19 21:31:11 +04:00
|
|
|
throw "Unknown argument " + args[0];
|
|
|
|
}
|
2013-02-03 22:16:42 +04:00
|
|
|
}
|
2013-01-15 22:32:13 +04:00
|
|
|
|
2013-01-15 19:46:38 +04:00
|
|
|
switch(m_configuration->GetCondition()) {
|
2013-05-29 21:16:15 +04:00
|
|
|
case LexicalReorderingConfiguration::FE:
|
|
|
|
case LexicalReorderingConfiguration::E:
|
|
|
|
if(m_factorsE.empty()) {
|
2013-06-01 02:04:05 +04:00
|
|
|
throw "TL factor mask for lexical reordering is unexpectedly empty";
|
2013-05-29 21:16:15 +04:00
|
|
|
}
|
|
|
|
if(m_configuration->GetCondition() == LexicalReorderingConfiguration::E)
|
|
|
|
break; // else fall through
|
|
|
|
case LexicalReorderingConfiguration::F:
|
|
|
|
if(m_factorsF.empty()) {
|
2013-06-01 02:04:05 +04:00
|
|
|
throw "SL factor mask for lexical reordering is unexpectedly empty";
|
2013-05-29 21:16:15 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-06-01 02:04:05 +04:00
|
|
|
throw "Unknown conditioning option!";
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
LexicalReordering::~LexicalReordering()
|
|
|
|
{
|
|
|
|
if(m_table)
|
|
|
|
delete m_table;
|
2013-01-15 19:46:38 +04:00
|
|
|
delete m_configuration;
|
2013-06-01 02:04:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LexicalReordering::Load()
|
|
|
|
{
|
|
|
|
m_table = LexicalReorderingTable::LoadAvailable(m_filePath, m_factorsF, m_factorsE, std::vector<FactorType>());
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
Scores LexicalReordering::GetProb(const Phrase& f, const Phrase& e) const
|
|
|
|
{
|
2011-11-21 14:49:26 +04:00
|
|
|
return m_table->GetScore(f, e, Phrase(ARRAY_SIZE_INCR));
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
2008-10-09 03:51:26 +04:00
|
|
|
|
2010-01-28 13:39:43 +03:00
|
|
|
FFState* LexicalReordering::Evaluate(const Hypothesis& hypo,
|
|
|
|
const FFState* prev_state,
|
2011-02-24 16:14:42 +03:00
|
|
|
ScoreComponentCollection* out) const
|
|
|
|
{
|
|
|
|
Scores score(GetNumScoreComponents(), 0);
|
|
|
|
const LexicalReorderingState *prev = dynamic_cast<const LexicalReorderingState *>(prev_state);
|
|
|
|
LexicalReorderingState *next_state = prev->Expand(hypo.GetTranslationOption(), score);
|
|
|
|
|
|
|
|
out->PlusEquals(this, score);
|
|
|
|
|
|
|
|
return next_state;
|
2009-02-06 18:43:06 +03:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
const FFState* LexicalReordering::EmptyHypothesisState(const InputType &input) const
|
|
|
|
{
|
2013-01-15 19:46:38 +04:00
|
|
|
return m_configuration->CreateLexicalReorderingState(input);
|
2009-02-06 18:43:06 +03:00
|
|
|
}
|
|
|
|
|
2013-05-30 15:41:08 +04:00
|
|
|
bool LexicalReordering::IsUseable(const FactorMask &mask) const
|
|
|
|
{
|
2013-05-30 15:51:40 +04:00
|
|
|
for (size_t i = 0; i < m_factorsE.size(); ++i) {
|
|
|
|
const FactorType &factor = m_factorsE[i];
|
|
|
|
if (!mask[factor]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2013-05-30 15:41:08 +04:00
|
|
|
|
|
|
|
}
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|