2010-01-28 13:39:43 +03:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "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-02-03 22:27:55 +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
|
|
|
vector<FactorType> f_factors, e_factors;
|
|
|
|
string filePath;
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
else if (args[0] == "input-factor") {
|
|
|
|
f_factors =Tokenize<FactorType>(args[1]);
|
|
|
|
}
|
|
|
|
else if (args[0] == "output-factor") {
|
|
|
|
e_factors =Tokenize<FactorType>(args[1]);
|
|
|
|
}
|
|
|
|
else if (args[0] == "path") {
|
|
|
|
filePath = args[1];
|
|
|
|
}
|
2013-02-19 21:31:11 +04:00
|
|
|
else {
|
|
|
|
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-01-15 22:32:13 +04:00
|
|
|
case LexicalReorderingConfiguration::FE:
|
|
|
|
case LexicalReorderingConfiguration::E:
|
|
|
|
m_factorsE = e_factors;
|
|
|
|
if(m_factorsE.empty()) {
|
|
|
|
UserMessage::Add("TL factor mask for lexical reordering is unexpectedly empty");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(m_configuration->GetCondition() == LexicalReorderingConfiguration::E)
|
|
|
|
break; // else fall through
|
|
|
|
case LexicalReorderingConfiguration::F:
|
|
|
|
m_factorsF = f_factors;
|
|
|
|
if(m_factorsF.empty()) {
|
|
|
|
UserMessage::Add("SL factor mask for lexical reordering is unexpectedly empty");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UserMessage::Add("Unknown conditioning option!");
|
2011-02-24 16:14:42 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2010-01-28 13:39:43 +03:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
m_table = LexicalReorderingTable::LoadAvailable(filePath, m_factorsF, m_factorsE, std::vector<FactorType>());
|
2013-01-15 22:32:13 +04: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;
|
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-02 15:15:26 +04:00
|
|
|
void LexicalReordering::Evaluate(const TargetPhrase &targetPhrase
|
|
|
|
, ScoreComponentCollection &scoreBreakdown
|
2013-05-02 17:55:26 +04:00
|
|
|
, ScoreComponentCollection &estimatedFutureScore) const
|
2013-05-02 15:15:26 +04:00
|
|
|
{
|
2013-05-02 20:07:03 +04:00
|
|
|
|
2013-05-02 15:15:26 +04:00
|
|
|
}
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|