mosesdecoder/moses/LexicalReordering.cpp

101 lines
2.8 KiB
C++
Raw Normal View History

#include <sstream>
2013-05-24 21:02:49 +04:00
#include "moses/FF/FFState.h"
#include "LexicalReordering.h"
#include "LexicalReorderingState.h"
#include "StaticData.h"
2013-01-15 22:32:13 +04:00
using namespace std;
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)
{
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") {
m_factorsF =Tokenize<FactorType>(args[1]);
2013-05-29 21:16:15 +04:00
} else if (args[0] == "output-factor") {
m_factorsE =Tokenize<FactorType>(args[1]);
2013-05-29 21:16:15 +04:00
} else if (args[0] == "path") {
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()) {
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()) {
throw "SL factor mask for lexical reordering is unexpectedly empty";
2013-05-29 21:16:15 +04:00
}
break;
default:
throw "Unknown conditioning option!";
}
}
LexicalReordering::~LexicalReordering()
{
if(m_table)
delete m_table;
2013-01-15 19:46:38 +04:00
delete m_configuration;
}
void LexicalReordering::Load()
{
m_table = LexicalReorderingTable::LoadAvailable(m_filePath, m_factorsF, m_factorsE, std::vector<FactorType>());
}
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));
}
FFState* LexicalReordering::Evaluate(const Hypothesis& hypo,
const FFState* prev_state,
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;
Feature function overhaul. Each feature function is computed in one of three ways: 1) Stateless feature functions from the phrase table/generation table: these are computed when the TranslationOption is created. They become part of the ScoreBreakdown object contained in the TranslationOption and are added to the feature value vector when a hypothesis is extended. 2) Stateless feature functions that are computed during state exploration. Currently, only WordPenalty falls into this category, but these functions implement a method Evaluate which do does not receive a Hypothesis or any contextual information. 3) Stateful feature functions: these features receive the arc information (translation option), compute some value and then return some context information. The context information created by a particular feature function is passed back to it as the previous context when a hypothesis originating at the node where the previous edge terminates is created. States in the search space may be recombined if the context information is identical. The context information must be stored in an object implementing the FFState interface. TODO: 1) the command line interface / MERT interface needs to go to named parameters that are otherwise opaque 2) StatefulFeatureFunction's Evaluate method should just take a TranslationOption and a context object. It is not good that it takes a hypothesis, because then people may be tempted to access information about the "previous" hypothesis without "declaring" this dependency. 3) Future cost estimates should be handled using feature functions. All stateful feature functions need some kind of future cost estimate. 4) Philipp's poor-man's cube pruning is broken. git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@2087 1f5c12ca-751b-0410-a591-d2e778427230
2009-02-06 18:43:06 +03:00
}
const FFState* LexicalReordering::EmptyHypothesisState(const InputType &input) const
{
2013-01-15 19:46:38 +04:00
return m_configuration->CreateLexicalReorderingState(input);
Feature function overhaul. Each feature function is computed in one of three ways: 1) Stateless feature functions from the phrase table/generation table: these are computed when the TranslationOption is created. They become part of the ScoreBreakdown object contained in the TranslationOption and are added to the feature value vector when a hypothesis is extended. 2) Stateless feature functions that are computed during state exploration. Currently, only WordPenalty falls into this category, but these functions implement a method Evaluate which do does not receive a Hypothesis or any contextual information. 3) Stateful feature functions: these features receive the arc information (translation option), compute some value and then return some context information. The context information created by a particular feature function is passed back to it as the previous context when a hypothesis originating at the node where the previous edge terminates is created. States in the search space may be recombined if the context information is identical. The context information must be stored in an object implementing the FFState interface. TODO: 1) the command line interface / MERT interface needs to go to named parameters that are otherwise opaque 2) StatefulFeatureFunction's Evaluate method should just take a TranslationOption and a context object. It is not good that it takes a hypothesis, because then people may be tempted to access information about the "previous" hypothesis without "declaring" this dependency. 3) Future cost estimates should be handled using feature functions. All stateful feature functions need some kind of future cost estimate. 4) Philipp's poor-man's cube pruning is broken. git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@2087 1f5c12ca-751b-0410-a591-d2e778427230
2009-02-06 18:43:06 +03: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;
}
}