mosesdecoder/contrib/other-builds/moses2/Search/Hypothesis.h
2015-12-31 14:53:16 +00:00

136 lines
3.0 KiB
C++

/*
* Hypothesis.h
*
* Created on: 24 Oct 2015
* Author: hieu
*/
#pragma once
#include <iostream>
#include <cstddef>
#include "../legacy/FFState.h"
#include "../legacy/Bitmap.h"
#include "../Scores.h"
#include "../Phrase.h"
#include "../TargetPhrase.h"
#include "../InputPath.h"
#include "../legacy/Range.h"
namespace Moses2
{
class Manager;
class PhraseImpl;
class TargetPhrase;
class Scores;
class StatefulFeatureFunction;
class InputType;
class InputPath;
class Hypothesis {
friend std::ostream& operator<<(std::ostream &, const Hypothesis &);
Hypothesis(Manager &mgr);
public:
Manager &mgr;
static Hypothesis *Create(Manager &mgr);
virtual ~Hypothesis();
// initial, empty hypo
void Init(const InputPath &path, const TargetPhrase &tp, const Bitmap &bitmap);
void Init(const Hypothesis &prevHypo,
const InputPath &path,
const TargetPhrase &tp,
const Bitmap &bitmap,
SCORE estimatedScore);
size_t hash() const;
bool operator==(const Hypothesis &other) const;
inline const Bitmap &GetBitmap() const
{ return *m_sourceCompleted; }
inline const InputPath &GetInputPath() const
{ return *m_path; }
inline const Range &GetCurrTargetWordsRange() const {
return m_currTargetWordsRange;
}
const Scores &GetScores() const
{ return *m_scores; }
SCORE GetFutureScore() const
{ return GetScores().GetTotalScore() + m_estimatedScore; }
const TargetPhrase &GetTargetPhrase() const
{ return *m_targetPhrase; }
const FFState *GetState(size_t ind) const
{ return m_ffStates[ind]; }
void OutputToStream(std::ostream &out) const;
void EmptyHypothesisState(const InputType &input);
void EvaluateWhenApplied();
void EvaluateWhenApplied(const StatefulFeatureFunction &sfff);
void EvaluateWhenAppliedNonBatch();
const Hypothesis* GetPrevHypo() const
{ return m_prevHypo; }
/** curr - pos is relative from CURRENT hypothesis's starting index
* (ie, start of sentence would be some negative number, which is
* not allowed- USE WITH CAUTION) */
inline const Word &GetCurrWord(size_t pos) const {
return GetTargetPhrase()[pos];
}
/** recursive - pos is relative from start of sentence */
const Word &GetWord(size_t pos) const;
void Swap(Hypothesis &other);
void Prefetch() const;
protected:
const TargetPhrase *m_targetPhrase;
const Bitmap *m_sourceCompleted;
const InputPath *m_path;
const Hypothesis *m_prevHypo;
FFState **m_ffStates;
Scores *m_scores;
SCORE m_estimatedScore;
Range m_currTargetWordsRange;
};
class HypothesisFutureScoreOrderer
{
public:
bool operator()(const Hypothesis* a, const Hypothesis* b) const {
return a->GetFutureScore() > b->GetFutureScore();
}
};
class HypothesisTargetPhraseOrderer
{
public:
bool operator()(const Hypothesis* a, const Hypothesis* b) const {
PhraseOrdererLexical phraseCmp;
bool ret = phraseCmp(a->GetTargetPhrase(), b->GetTargetPhrase());
/*
std::cerr << (const Phrase&) a->GetTargetPhrase() << " ||| "
<< (const Phrase&) b->GetTargetPhrase() << " ||| "
<< ret << std::endl;
*/
return ret;
}
};
}