Removed requirement that the number of probabilities cached in a

translation model for a given model be equal to the number of
feature functions provided by the model so collapseff lexical
reordering models work.


git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/branches/hierarchical-reo@2921 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
chardmeier 2010-02-20 09:55:02 +00:00
parent 435539d4ed
commit ac942f8358
7 changed files with 45 additions and 33 deletions

View File

@ -502,11 +502,5 @@ std::string Hypothesis::GetTargetPhraseStringRep() const
return GetTargetPhraseStringRep(allFactors);
}
const ScoreComponentCollection &Hypothesis::GetCachedReorderingScore() const
{
return m_transOpt->GetReorderingScore();
}
}

View File

@ -273,8 +273,6 @@ public:
return s_HypothesesCreated;
}
const ScoreComponentCollection &GetCachedReorderingScore() const;
const TranslationOption &GetTranslationOption() const
{ return *m_transOpt; }
};

View File

@ -116,9 +116,14 @@ LexicalReorderingState *LexicalReorderingConfiguration::CreateLexicalReorderingS
void LexicalReorderingState::CopyScores(Scores& scores, const TranslationOption &topt, ReorderingType reoType) const {
// don't call this on a bidirectional object
assert(m_direction == LexicalReorderingConfiguration::Backward || m_direction == LexicalReorderingConfiguration::Forward);
const Scores &scoreSet = (m_direction == LexicalReorderingConfiguration::Backward) ?
topt.GetReorderingScore().GetScoresForProducer(m_configuration.GetScoreProducer()) : m_prevScore;
const Scores *cachedScores = (m_direction == LexicalReorderingConfiguration::Backward) ?
topt.GetCachedScores(m_configuration.GetScoreProducer()) : m_prevScore;
// No scores available. TODO: Using a good prior distribution would be nicer.
if(cachedScores == NULL)
return;
const Scores &scoreSet = *cachedScores;
if(m_configuration.CollapseScores())
scores[m_offset] = scoreSet[m_offset + reoType];
else {
@ -134,11 +139,19 @@ 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 Scores *other) const {
if(m_prevScore == other)
return 0;
if(other == 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(m_prevScore[i] < other[i])
if(my[i] < their[i])
return -1;
else if(m_prevScore[i] > other[i])
else if(my[i] > their[i])
return 1;
return 0;

View File

@ -81,11 +81,11 @@ class LexicalReorderingState : public FFState {
// 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;
Scores m_prevScore;
const Scores *m_prevScore;
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.GetReorderingScore().GetScoresForProducer(m_configuration.GetScoreProducer())) {}
m_prevScore(topt.GetCachedScores(m_configuration.GetScoreProducer())) {}
inline LexicalReorderingState(const LexicalReorderingConfiguration &config, LexicalReorderingConfiguration::Direction dir, size_t offset)
: m_configuration(config), m_direction(dir), m_offset(offset) {}
@ -93,7 +93,7 @@ class LexicalReorderingState : public FFState {
// 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 Scores *other) const;
//constants for the different type of reorderings (corresponding to indexes in the table file)
static const ReorderingType M = 0; // monotonic

View File

@ -93,7 +93,7 @@ TranslationOption::TranslationOption(const TranslationOption &copy)
, m_sourceWordsRange(copy.m_sourceWordsRange)
, m_futureScore(copy.m_futureScore)
, m_scoreBreakdown(copy.m_scoreBreakdown)
, m_reordering(copy.m_reordering)
, m_cachedScores(copy.m_cachedScores)
{}
TranslationOption::TranslationOption(const TranslationOption &copy, const WordsRange &sourceWordsRange)
@ -103,7 +103,7 @@ TranslationOption::TranslationOption(const TranslationOption &copy, const WordsR
, m_sourceWordsRange(sourceWordsRange)
, m_futureScore(copy.m_futureScore)
, m_scoreBreakdown(copy.m_scoreBreakdown)
, m_reordering(copy.m_reordering)
, m_cachedScores(copy.m_cachedScores)
{}
void TranslationOption::MergeNewFeatures(const Phrase& phrase, const ScoreComponentCollection& score, const std::vector<FactorType>& featuresToAdd)
@ -165,10 +165,9 @@ ostream& operator<<(ostream& out, const TranslationOption& possibleTranslation)
return out;
}
void TranslationOption::CacheReorderingProb(const LexicalReordering &lexreordering
, const Scores &score)
void TranslationOption::CacheScores(const ScoreProducer &producer, const Scores &score)
{
m_reordering.Assign(&lexreordering, score);
m_cachedScores[&producer] = new Scores(score);
}
}

View File

@ -21,6 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#pragma once
#include <map>
#include <vector>
#include "WordsBitmap.h"
#include "WordsRange.h"
@ -71,7 +72,9 @@ protected:
//! TargetPhrase may be shorter than the n-gram order. But, if it is
//! possible to estimate, it is included here.
ScoreComponentCollection m_scoreBreakdown;
ScoreComponentCollection m_reordering;
typedef std::map<const ScoreProducer *, const Scores *> _ScoreCacheMap;
_ScoreCacheMap m_cachedScores;
public:
/** constructor. Used by initial translation step */
@ -92,6 +95,8 @@ public:
~TranslationOption()
{
delete m_sourcePhrase;
for(_ScoreCacheMap::const_iterator it = m_cachedScores.begin(); it != m_cachedScores.end(); ++it)
delete it->second;
}
/** returns true if all feature types in featuresToCheck are compatible between the two phrases */
@ -157,28 +162,32 @@ public:
return m_futureScore;
}
/** return true if the source phrase translates into nothing */
/** return true if the source phrase translates into nothing */
inline bool IsDeletionOption() const
{
return m_targetPhrase.GetSize() == 0;
}
{
return m_targetPhrase.GetSize() == 0;
}
/** returns detailed component scores */
inline const ScoreComponentCollection &GetScoreBreakdown() const
{
return m_scoreBreakdown;
}
/** returns detailed component scores */
inline const ScoreComponentCollection &GetReorderingScore() const
/** returns cached scores */
inline const Scores *GetCachedScores(const ScoreProducer *scoreProducer) const
{
return m_reordering;
_ScoreCacheMap::const_iterator it = m_cachedScores.find(scoreProducer);
if(it == m_cachedScores.end())
return NULL;
else
return it->second;
}
/** Calculate future score and n-gram score of this trans option, plus the score breakdowns */
void CalcScore();
void CacheReorderingProb(const LexicalReordering &lexreordering
, const Scores &score);
void CacheScores(const ScoreProducer &scoreProducer, const Scores &score);
TO_STRING();
};

View File

@ -642,9 +642,8 @@ void TranslationOptionCollection::CacheLexReordering()
{
Scores score = lexreordering.GetProb(*sourcePhrase
, transOpt.GetTargetPhrase());
// TODO should have better handling of unknown reordering entries
if (!score.empty())
transOpt.CacheReorderingProb(lexreordering, score);
transOpt.CacheScores(lexreordering, score);
}
}
}