Stub out sparse reordering class

This commit is contained in:
Barry Haddow 2014-05-27 11:05:56 +01:00
parent 1bd851411f
commit 244d9cd824
6 changed files with 92 additions and 18 deletions

View File

@ -14,6 +14,7 @@ LexicalReordering::LexicalReordering(const std::string &line)
{
std::cerr << "Initializing LexicalReordering.." << std::endl;
map<string,string> sparseArgs;
for (size_t i = 0; i < m_args.size(); ++i) {
const vector<string> &args = m_args[i];
@ -27,6 +28,8 @@ LexicalReordering::LexicalReordering(const std::string &line)
m_factorsE =Tokenize<FactorType>(args[1]);
} else if (args[0] == "path") {
m_filePath = args[1];
} else if (args[0].substr(0,7) == "sparse-") {
sparseArgs[args[0].substr(7)] = args[1];
} else {
throw "Unknown argument " + args[0];
}
@ -48,6 +51,10 @@ LexicalReordering::LexicalReordering(const std::string &line)
default:
throw "Unknown conditioning option!";
}
if (sparseArgs.size()) {
m_sparse.reset(new SparseReordering(sparseArgs));
}
}
LexicalReordering::~LexicalReordering()

View File

@ -3,17 +3,20 @@
#include <string>
#include <vector>
#include <boost/scoped_ptr.hpp>
#include "moses/Factor.h"
#include "moses/Phrase.h"
#include "moses/TypeDef.h"
#include "moses/Util.h"
#include "moses/WordsRange.h"
#include "LexicalReorderingState.h"
#include "LexicalReorderingTable.h"
#include "moses/FF/StatefulFeatureFunction.h"
#include "util/exception.hh"
#include "LexicalReorderingState.h"
#include "LexicalReorderingTable.h"
#include "SparseReordering.h"
namespace Moses
{
@ -79,6 +82,7 @@ private:
//bool m_oneScorePerDirection;
std::vector<FactorType> m_factorsE, m_factorsF;
std::string m_filePath;
boost::scoped_ptr<SparseReordering> m_sparse;
};
}

View File

@ -128,7 +128,8 @@ void LexicalReorderingState::CopyScores(Scores& scores, const TranslationOption
UTIL_THROW_IF2(m_direction != LexicalReorderingConfiguration::Backward && m_direction != LexicalReorderingConfiguration::Forward,
"Unknown direction: " << m_direction);
const Scores *cachedScores = (m_direction == LexicalReorderingConfiguration::Backward) ?
topt.GetLexReorderingScores(m_configuration.GetScoreProducer()) : m_prevScore;
topt.GetLexReorderingScores(m_configuration.GetScoreProducer()) :
m_prevOption->GetLexReorderingScores(m_configuration.GetScoreProducer());
// No scores available. TODO: Using a good prior distribution would be nicer.
if(cachedScores == NULL)
@ -151,23 +152,24 @@ void LexicalReorderingState::ClearScores(Scores& scores) const
std::fill(scores.begin() + m_offset, scores.begin() + m_offset + m_configuration.GetNumberOfTypes(), 0);
}
int LexicalReorderingState::ComparePrevScores(const Scores *other) const
int LexicalReorderingState::ComparePrevScores(const TranslationOption *other) const
{
if(m_prevScore == other)
const Scores* myPrevScores = m_prevOption->GetLexReorderingScores(m_configuration.GetScoreProducer());
const Scores* otherPrevScores = other->GetLexReorderingScores(m_configuration.GetScoreProducer());
if(myPrevScores == otherPrevScores)
return 0;
// The pointers are NULL if a phrase pair isn't found in the reordering table.
if(other == NULL)
if(otherPrevScores == NULL)
return -1;
if(m_prevScore == NULL)
if(myPrevScores == NULL)
return 1;
const Scores &my = *m_prevScore;
const Scores &their = *other;
for(size_t i = m_offset; i < m_offset + m_configuration.GetNumberOfTypes(); i++)
if(my[i] < their[i])
if((*myPrevScores)[i] < (*otherPrevScores)[i])
return -1;
else if(my[i] > their[i])
else if((*myPrevScores)[i] > (*otherPrevScores)[i])
return 1;
return 0;
@ -193,7 +195,7 @@ int PhraseBasedReorderingState::Compare(const FFState& o) const
UTIL_THROW_IF2(other == NULL, "Wrong state type");
if (m_prevRange == other->m_prevRange) {
if (m_direction == LexicalReorderingConfiguration::Forward) {
return ComparePrevScores(other->m_prevScore);
return ComparePrevScores(other->m_prevOption);
} else {
return 0;
}
@ -411,7 +413,7 @@ int HierarchicalReorderingForwardState::Compare(const FFState& o) const
UTIL_THROW_IF2(other == NULL, "Wrong state type");
if (m_prevRange == other->m_prevRange) {
return ComparePrevScores(other->m_prevScore);
return ComparePrevScores(other->m_prevOption);
} else if (m_prevRange < other->m_prevRange) {
return -1;
}

View File

@ -4,8 +4,9 @@
#include <vector>
#include <string>
#include "moses/Hypothesis.h"
#include "LexicalReordering.h"
//#include "LexicalReordering.h"
#include "moses/WordsRange.h"
#include "moses/WordsBitmap.h"
#include "moses/TranslationOption.h"
@ -89,28 +90,29 @@ public:
static LexicalReorderingState* CreateLexicalReorderingState(const std::vector<std::string>& config,
LexicalReorderingConfiguration::Direction dir, const InputType &input);
typedef int ReorderingType;
protected:
typedef int ReorderingType;
const LexicalReorderingConfiguration &m_configuration;
// The following is the true direction of the object, which can be Backward or Forward even if the Configuration has Bidirectional.
LexicalReorderingConfiguration::Direction m_direction;
size_t m_offset;
//forward scores are conditioned on prev option, so need to remember it
const TranslationOption *m_prevOption;
inline LexicalReorderingState(const LexicalReorderingState *prev, const TranslationOption &topt) :
m_configuration(prev->m_configuration), m_direction(prev->m_direction), m_offset(prev->m_offset),
m_prevScore(topt.GetLexReorderingScores(m_configuration.GetScoreProducer())) {}
m_prevOption(&topt) {}
inline LexicalReorderingState(const LexicalReorderingConfiguration &config, LexicalReorderingConfiguration::Direction dir, size_t offset)
: m_configuration(config), m_direction(dir), m_offset(offset), m_prevScore(NULL) {}
: m_configuration(config), m_direction(dir), m_offset(offset), m_prevOption(NULL) {}
// copy the right scores in the right places, taking into account forward/backward, offset, collapse
void CopyScores(Scores& scores, const TranslationOption& topt, ReorderingType reoType) const;
void ClearScores(Scores& scores) const;
int ComparePrevScores(const Scores *other) const;
int ComparePrevScores(const TranslationOption *other) const;
//constants for the different type of reorderings (corresponding to indexes in the table file)
static const ReorderingType M = 0; // monotonic

View File

@ -0,0 +1,25 @@
#include "SparseReordering.h"
using namespace std;
namespace Moses
{
SparseReordering::SparseReordering(const map<string,string>& config)
{
for (map<string,string>::const_iterator i = config.begin(); i != config.end(); ++i) {
cerr << i->first << " " << i->second << endl;
}
}
void SparseReordering::AddScores(
const TranslationOption& topt,
LexicalReorderingState::ReorderingType reoType,
LexicalReorderingConfiguration::Direction direction,
ScoreComponentCollection* scores) const
{
}
} //namespace

View File

@ -0,0 +1,34 @@
#ifndef moses_FF_LexicalReordering_SparseReordering_h
#define moses_FF_LexicalReordering_SparseReordering_h
/**
* Sparse reordering features for phrase-based MT, following Cherry (NAACL, 2013)
**/
#include <map>
#include <string>
#include "moses/ScoreComponentCollection.h"
#include "LexicalReorderingState.h"
namespace Moses
{
class SparseReordering
{
public:
SparseReordering(const std::map<std::string,std::string>& config);
void AddScores(const TranslationOption& topt,
LexicalReorderingState::ReorderingType reoType,
LexicalReorderingConfiguration::Direction direction,
ScoreComponentCollection* scores) const ;
};
} //namespace
#endif