allow specification of default scores for lexicalized reordering model

This commit is contained in:
Philipp Koehn 2014-09-21 07:43:44 +01:00
parent e9db2fe4aa
commit 33fbbc5faa
3 changed files with 33 additions and 1 deletions

View File

@ -15,6 +15,7 @@ LexicalReordering::LexicalReordering(const std::string &line)
std::cerr << "Initializing LexicalReordering.." << std::endl;
map<string,string> sparseArgs;
m_haveDefaultScores = false;
for (size_t i = 0; i < m_args.size(); ++i) {
const vector<string> &args = m_args[i];
@ -30,6 +31,12 @@ LexicalReordering::LexicalReordering(const std::string &line)
m_filePath = args[1];
} else if (args[0].substr(0,7) == "sparse-") {
sparseArgs[args[0].substr(7)] = args[1];
} else if (args[0] == "default-scores") {
vector<string> tokens = Tokenize(args[1],",");
for(size_t i=0; i<tokens.size(); i++) {
m_defaultScores.push_back( TransformScore( Scan<float>(tokens[i]) ) );
}
m_haveDefaultScores = true;
} else {
UTIL_THROW(util::Exception,"Unknown argument " + args[0]);
}
@ -52,6 +59,13 @@ LexicalReordering::LexicalReordering(const std::string &line)
UTIL_THROW(util::Exception,"Unknown conditioning option!");
}
// sanity check: number of default scores
if (m_haveDefaultScores) {
if(m_defaultScores.size() != m_configuration->GetNumScoreComponents()) {
UTIL_THROW(util::Exception,"wrong number of default scores (" << m_defaultScores.size() << ") for lexicalized reordering model (expected " << m_configuration->GetNumScoreComponents() << ")");
}
}
m_configuration->ConfigureSparse(sparseArgs, this);
}

View File

@ -66,6 +66,8 @@ public:
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedFutureScore) const
{}
bool GetHaveDefaultScores() { return m_haveDefaultScores; }
float GetDefaultScore( size_t i ) { return m_defaultScores[i]; }
private:
bool DecodeCondition(std::string s);
@ -82,6 +84,8 @@ private:
//bool m_oneScorePerDirection;
std::vector<FactorType> m_factorsE, m_factorsF;
std::string m_filePath;
bool m_haveDefaultScores;
Scores m_defaultScores;
};
}

View File

@ -139,18 +139,32 @@ void LexicalReorderingState::CopyScores(ScoreComponentCollection* accum, const
if (m_direction != LexicalReorderingConfiguration::Backward) relevantOpt = m_prevOption;
const Scores *cachedScores = relevantOpt->GetLexReorderingScores(m_configuration.GetScoreProducer());
// look up applicable score from vectore of scores
if(cachedScores) {
Scores scores(m_configuration.GetScoreProducer()->GetNumScoreComponents(),0);
const Scores &scoreSet = *cachedScores;
if(m_configuration.CollapseScores())
if(m_configuration.CollapseScores()) {
scores[m_offset] = scoreSet[m_offset + reoType];
}
else {
std::fill(scores.begin() + m_offset, scores.begin() + m_offset + m_configuration.GetNumberOfTypes(), 0);
scores[m_offset + reoType] = scoreSet[m_offset + reoType];
}
accum->PlusEquals(m_configuration.GetScoreProducer(), scores);
}
// else: use default scores (if specified)
else if (m_configuration.GetScoreProducer()->GetHaveDefaultScores()) {
Scores scores(m_configuration.GetScoreProducer()->GetNumScoreComponents(),0);
if(m_configuration.CollapseScores()) {
scores[m_offset] = m_configuration.GetScoreProducer()->GetDefaultScore(m_offset + reoType);
}
else {
scores[m_offset + reoType] = m_configuration.GetScoreProducer()->GetDefaultScore(m_offset + reoType);
}
accum->PlusEquals(m_configuration.GetScoreProducer(), scores);
}
// note: if no default score, no cost
const SparseReordering* sparse = m_configuration.GetSparseReordering();
if (sparse) sparse->CopyScores(*relevantOpt, m_prevOption, input, reoType, m_direction, accum);