EvaluateWhenApplied()

This commit is contained in:
Hieu Hoang 2015-11-05 16:35:31 +00:00
parent 656b484442
commit b93b13eabf
10 changed files with 52 additions and 43 deletions

View File

@ -23,9 +23,11 @@ struct DistortionState_traditional : public Moses::FFState {
// uninitialised
}
DistortionState_traditional(const Moses::Range& wr, int fg)
: range(wr), first_gap(fg)
{}
void Set(const Moses::Range& wr, int fg)
{
range = wr;
first_gap = fg;
}
size_t hash() const {
return range.GetEndPos();
@ -83,10 +85,11 @@ Distortion::EvaluateInIsolation(const System &system,
{
}
Moses::FFState* Distortion::EvaluateWhenApplied(const Manager &mgr,
void Distortion::EvaluateWhenApplied(const Manager &mgr,
const Hypothesis &hypo,
const Moses::FFState &prevState,
Scores &scores) const
Scores &scores,
Moses::FFState &state) const
{
MemPool &pool = mgr.GetPool();
@ -99,11 +102,8 @@ Moses::FFState* Distortion::EvaluateWhenApplied(const Manager &mgr,
scores.PlusEquals(mgr.GetSystem(), *this, distortionScore);
DistortionState_traditional* res = new (pool.Allocate<DistortionState_traditional>()) DistortionState_traditional(
hypo.GetRange(),
hypo.GetBitmap().GetFirstGapPos());
return res;
DistortionState_traditional &stateCast = static_cast<DistortionState_traditional&>(state);
stateCast.Set(hypo.GetRange(), hypo.GetBitmap().GetFirstGapPos());
}
SCORE Distortion::CalculateDistortionScore(const Moses::Range &prev, const Moses::Range &curr, const int FirstGap) const

View File

@ -28,10 +28,11 @@ public:
Scores &scores,
Scores *estimatedScores) const;
virtual Moses::FFState* EvaluateWhenApplied(const Manager &mgr,
virtual void EvaluateWhenApplied(const Manager &mgr,
const Hypothesis &hypo,
const Moses::FFState &prevState,
Scores &scores) const;
Scores &scores,
Moses::FFState &state) const;
protected:
SCORE CalculateDistortionScore(const Moses::Range &prev, const Moses::Range &curr, const int FirstGap) const;

View File

@ -59,10 +59,12 @@ SkeletonStatefulFF::EvaluateInIsolation(const System &system,
{
}
Moses::FFState* SkeletonStatefulFF::EvaluateWhenApplied(const Manager &mgr,
void SkeletonStatefulFF::EvaluateWhenApplied(const Manager &mgr,
const Hypothesis &hypo,
const Moses::FFState &prevState,
Scores &scores) const
Scores &scores,
Moses::FFState &state) const
{
SkeletonState &stateCast = static_cast<SkeletonState&>(state);
stateCast.targetLen = hypo.GetTargetPhrase().GetSize();
}

View File

@ -25,10 +25,11 @@ public:
Scores &scores,
Scores *estimatedScores) const;
virtual Moses::FFState* EvaluateWhenApplied(const Manager &mgr,
virtual void EvaluateWhenApplied(const Manager &mgr,
const Hypothesis &hypo,
const Moses::FFState &prevState,
Scores &scores) const;
Scores &scores,
Moses::FFState &state) const;
};

View File

@ -30,10 +30,11 @@ public:
//! return the state associated with the empty hypothesis for a given sentence
virtual void EmptyHypothesisState(Moses::FFState &state, const Manager &mgr, const PhraseImpl &input) const = 0;
virtual Moses::FFState* EvaluateWhenApplied(const Manager &mgr,
virtual void EvaluateWhenApplied(const Manager &mgr,
const Hypothesis &hypo,
const Moses::FFState &prevState,
Scores &scores) const = 0;
Scores &scores,
Moses::FFState &state) const = 0;
protected:
size_t m_statefulInd;

View File

@ -144,21 +144,22 @@ KENLM::EvaluateInIsolation(const System &system,
}
}
Moses::FFState* KENLM::EvaluateWhenApplied(const Manager &mgr,
void KENLM::EvaluateWhenApplied(const Manager &mgr,
const Hypothesis &hypo,
const Moses::FFState &prevState,
Scores &scores) const
Scores &scores,
Moses::FFState &state) const
{
KenLMState &stateCast = static_cast<KenLMState&>(state);
const System &system = mgr.GetSystem();
MemPool &pool = mgr.GetPool();
const lm::ngram::State &in_state = static_cast<const KenLMState&>(prevState).state;
KenLMState *ret = new (pool.Allocate<KenLMState>()) KenLMState();
if (!hypo.GetTargetPhrase().GetSize()) {
ret->state = in_state;
return ret;
stateCast.state = in_state;
return;
}
const std::size_t begin = hypo.GetCurrTargetWordsRange().GetStartPos();
@ -168,7 +169,7 @@ Moses::FFState* KENLM::EvaluateWhenApplied(const Manager &mgr,
std::size_t position = begin;
typename Model::State aux_state;
typename Model::State *state0 = &ret->state, *state1 = &aux_state;
typename Model::State *state0 = &stateCast.state, *state1 = &aux_state;
float score = m_ngram->Score(in_state, TranslateID(hypo.GetWord(position)), *state0);
++position;
@ -181,15 +182,15 @@ Moses::FFState* KENLM::EvaluateWhenApplied(const Manager &mgr,
// Score end of sentence.
std::vector<lm::WordIndex> indices(m_ngram->Order() - 1);
const lm::WordIndex *last = LastIDs(hypo, &indices.front());
score += m_ngram->FullScoreForgotState(&indices.front(), last, m_ngram->GetVocabulary().EndSentence(), ret->state).prob;
score += m_ngram->FullScoreForgotState(&indices.front(), last, m_ngram->GetVocabulary().EndSentence(), stateCast.state).prob;
} else if (adjust_end < end) {
// Get state after adding a long phrase.
std::vector<lm::WordIndex> indices(m_ngram->Order() - 1);
const lm::WordIndex *last = LastIDs(hypo, &indices.front());
m_ngram->GetState(&indices.front(), last, ret->state);
} else if (state0 != &ret->state) {
m_ngram->GetState(&indices.front(), last, stateCast.state);
} else if (state0 != &stateCast.state) {
// Short enough phrase that we can just reuse the state.
ret->state = *state0;
stateCast.state = *state0;
}
score = Moses::TransformLMScore(score);
@ -203,8 +204,6 @@ Moses::FFState* KENLM::EvaluateWhenApplied(const Manager &mgr,
} else {
scores.PlusEquals(system, *this, score);
}
return ret;
}
void KENLM::CalcScore(const Phrase &phrase, float &fullScore, float &ngramScore, std::size_t &oovCount) const

View File

@ -34,10 +34,11 @@ public:
Scores &scores,
Scores *estimatedScores) const;
virtual Moses::FFState* EvaluateWhenApplied(const Manager &mgr,
virtual void EvaluateWhenApplied(const Manager &mgr,
const Hypothesis &hypo,
const Moses::FFState &prevState,
Scores &scores) const;
Scores &scores,
Moses::FFState &state) const;
void SetParameter(const std::string& key, const std::string& value);

View File

@ -25,9 +25,10 @@ struct LMState : public Moses::PointerState
// uninitialised
}
LMState(MemPool &pool, void *lms, const std::vector<const Moses::Factor*> &context)
:PointerState(lms)
void Set(MemPool &pool, void *lms, const std::vector<const Moses::Factor*> &context)
{
lmstate = lms;
numWords = context.size();
lastWords = (const Moses::Factor**) pool.Allocate(sizeof(const Moses::Factor*) * numWords);
for (size_t i = 0; i < numWords; ++i) {
@ -173,10 +174,11 @@ LanguageModel::EvaluateInIsolation(const System &system,
}
}
Moses::FFState* LanguageModel::EvaluateWhenApplied(const Manager &mgr,
void LanguageModel::EvaluateWhenApplied(const Manager &mgr,
const Hypothesis &hypo,
const Moses::FFState &prevState,
Scores &scores) const
Scores &scores,
Moses::FFState &state) const
{
const LMState &prevLMState = static_cast<const LMState &>(prevState);
size_t numWords = prevLMState.numWords;
@ -220,8 +222,9 @@ Moses::FFState* LanguageModel::EvaluateWhenApplied(const Manager &mgr,
// return state
//DebugContext(context);
LMState &stateCast = static_cast<LMState&>(state);
MemPool &pool = mgr.GetPool();
return new (pool.Allocate<LMState>()) LMState(pool, fromScoring.second, context);
stateCast.Set(pool, fromScoring.second, context);
}
void LanguageModel::ShiftOrPush(std::vector<const Moses::Factor*> &context, const Moses::Factor *factor) const

View File

@ -59,10 +59,11 @@ public:
Scores &scores,
Scores *estimatedScores) const;
virtual Moses::FFState* EvaluateWhenApplied(const Manager &mgr,
virtual void EvaluateWhenApplied(const Manager &mgr,
const Hypothesis &hypo,
const Moses::FFState &prevState,
Scores &scores) const;
Scores &scores,
Moses::FFState &state) const;
protected:
std::string m_path;

View File

@ -146,9 +146,9 @@ void Hypothesis::EvaluateWhenApplied()
BOOST_FOREACH(const StatefulFeatureFunction *sfff, sfffs) {
size_t statefulInd = sfff->GetStatefulInd();
const Moses::FFState *prevState = m_prevHypo->GetState(statefulInd);
Moses::FFState *thisState = m_ffStates[statefulInd];
assert(prevState);
Moses::FFState *state = sfff->EvaluateWhenApplied(m_mgr, *this, *prevState, *m_scores);
m_ffStates[statefulInd] = state;
sfff->EvaluateWhenApplied(m_mgr, *this, *prevState, *m_scores, *thisState);
}
//cerr << *this << endl;