2010-02-24 14:15:44 +03:00
|
|
|
#ifndef moses_FeatureFunction_h
|
|
|
|
#define moses_FeatureFunction_h
|
2009-02-06 18:43:06 +03:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "ScoreProducer.h"
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
namespace Moses
|
|
|
|
{
|
2009-02-06 18:43:06 +03:00
|
|
|
|
|
|
|
class TargetPhrase;
|
2012-09-07 00:41:03 +04:00
|
|
|
class TranslationOption;
|
2009-02-06 18:43:06 +03:00
|
|
|
class Hypothesis;
|
2011-06-16 01:31:27 +04:00
|
|
|
class ChartHypothesis;
|
2009-02-06 18:43:06 +03:00
|
|
|
class FFState;
|
2010-02-11 20:08:24 +03:00
|
|
|
class InputType;
|
2009-02-06 18:43:06 +03:00
|
|
|
class ScoreComponentCollection;
|
2012-09-07 00:41:03 +04:00
|
|
|
class WordsBitmap;
|
2012-09-14 01:08:01 +04:00
|
|
|
class WordsRange;
|
2009-02-06 18:43:06 +03:00
|
|
|
|
2012-09-21 14:56:01 +04:00
|
|
|
|
2012-09-19 21:00:53 +04:00
|
|
|
/**
|
|
|
|
* Contains all that a feature function can access without affecting recombination.
|
|
|
|
* For stateless features, this is all that it can access. Currently this is not
|
|
|
|
* used for stateful features, as it would need to be retro-fitted to the LM feature.
|
|
|
|
* TODO: Expose source segmentation,lattice path.
|
2012-09-21 14:56:01 +04:00
|
|
|
* XXX Don't add anything to the context that would break recombination XXX
|
2012-09-19 21:00:53 +04:00
|
|
|
**/
|
|
|
|
class PhraseBasedFeatureContext
|
|
|
|
{
|
|
|
|
// The context either has a hypothesis (during search), or a TranslationOption and
|
|
|
|
// source sentence (during pre-calculation).
|
|
|
|
const Hypothesis* m_hypothesis;
|
|
|
|
const TranslationOption& m_translationOption;
|
|
|
|
const InputType& m_source;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PhraseBasedFeatureContext(const Hypothesis* hypothesis);
|
|
|
|
PhraseBasedFeatureContext(const TranslationOption& translationOption,
|
|
|
|
const InputType& source);
|
|
|
|
|
|
|
|
const TranslationOption& GetTranslationOption() const;
|
|
|
|
const InputType& GetSource() const;
|
|
|
|
const TargetPhrase& GetTargetPhrase() const; //convenience method
|
|
|
|
const WordsBitmap& GetWordsBitmap() const;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-09-21 14:56:01 +04:00
|
|
|
/**
|
|
|
|
* Same as PhraseBasedFeatureContext, but for chart-based Moses.
|
|
|
|
**/
|
|
|
|
class ChartBasedFeatureContext
|
|
|
|
{
|
|
|
|
//The context either has a hypothesis (during search) or a
|
|
|
|
//TargetPhrase and source sentence (during pre-calculation)
|
|
|
|
//TODO: should the context also include some info on where the TargetPhrase
|
|
|
|
//is anchored (assuming it's lexicalised), which is available at pre-calc?
|
|
|
|
const ChartHypothesis* m_hypothesis;
|
|
|
|
const TargetPhrase& m_targetPhrase;
|
|
|
|
const InputType& m_source;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ChartBasedFeatureContext(const ChartHypothesis* hypothesis);
|
|
|
|
ChartBasedFeatureContext(const TargetPhrase& targetPhrase,
|
|
|
|
const InputType& source);
|
2009-02-06 18:43:06 +03:00
|
|
|
|
2012-09-21 14:56:01 +04:00
|
|
|
const InputType& GetSource() const;
|
|
|
|
const TargetPhrase& GetTargetPhrase() const;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-06-27 03:45:02 +04:00
|
|
|
/** base class for all feature functions.
|
|
|
|
* @todo is this for pb & hiero too?
|
|
|
|
* @todo what's the diff between FeatureFunction and ScoreProducer?
|
|
|
|
*/
|
2011-02-24 16:14:42 +03:00
|
|
|
class FeatureFunction: public ScoreProducer
|
|
|
|
{
|
2009-02-06 18:43:06 +03:00
|
|
|
|
|
|
|
public:
|
2011-11-09 01:22:34 +04:00
|
|
|
FeatureFunction(const std::string& description, size_t numScoreComponents) :
|
|
|
|
ScoreProducer(description, numScoreComponents) {}
|
2009-02-06 18:43:06 +03:00
|
|
|
virtual bool IsStateless() const = 0;
|
|
|
|
virtual ~FeatureFunction();
|
2012-07-26 20:32:50 +04:00
|
|
|
|
|
|
|
float GetSparseProducerWeight() const { return 1; }
|
2009-02-06 18:43:06 +03:00
|
|
|
};
|
|
|
|
|
2012-06-27 03:45:02 +04:00
|
|
|
/** base class for all stateless feature functions.
|
|
|
|
* eg. phrase table, word penalty, phrase penalty
|
|
|
|
*/
|
2011-02-24 16:14:42 +03:00
|
|
|
class StatelessFeatureFunction: public FeatureFunction
|
|
|
|
{
|
2009-02-06 18:43:06 +03:00
|
|
|
|
|
|
|
public:
|
2011-11-09 01:22:34 +04:00
|
|
|
StatelessFeatureFunction(const std::string& description, size_t numScoreComponents) :
|
|
|
|
FeatureFunction(description, numScoreComponents) {}
|
2012-09-07 00:41:03 +04:00
|
|
|
/**
|
|
|
|
* This should be implemented for features that apply to phrase-based models.
|
|
|
|
**/
|
2012-09-19 21:00:53 +04:00
|
|
|
virtual void Evaluate(const PhraseBasedFeatureContext& context,
|
2012-09-13 21:16:13 +04:00
|
|
|
ScoreComponentCollection* accumulator) const = 0;
|
2012-04-09 23:47:51 +04:00
|
|
|
|
2012-09-21 14:56:01 +04:00
|
|
|
/**
|
|
|
|
* Same for chart-based features.
|
|
|
|
**/
|
|
|
|
virtual void EvaluateChart(const ChartBasedFeatureContext& context,
|
2012-09-13 21:16:13 +04:00
|
|
|
ScoreComponentCollection* accumulator) const = 0;
|
2009-02-06 18:43:06 +03:00
|
|
|
|
2012-09-07 00:41:03 +04:00
|
|
|
//If true, then the feature is evaluated before search begins, and stored in
|
2012-09-21 18:00:24 +04:00
|
|
|
//the TranslationOptionCollection.
|
2009-02-06 18:43:06 +03:00
|
|
|
virtual bool ComputeValueInTranslationOption() const;
|
|
|
|
|
2012-09-21 18:00:24 +04:00
|
|
|
//!If true, the feature is stored in the ttable, so gets copied into the
|
|
|
|
//TargetPhrase and does not need cached in the TranslationOption
|
|
|
|
virtual bool ComputeValueInTranslationTable() const {return false;}
|
|
|
|
|
2009-02-06 18:43:06 +03:00
|
|
|
bool IsStateless() const;
|
|
|
|
};
|
|
|
|
|
2012-06-27 03:45:02 +04:00
|
|
|
/** base class for all stateful feature functions.
|
|
|
|
* eg. LM, distortion penalty
|
|
|
|
*/
|
2011-02-24 16:14:42 +03:00
|
|
|
class StatefulFeatureFunction: public FeatureFunction
|
|
|
|
{
|
2009-02-06 18:43:06 +03:00
|
|
|
|
|
|
|
public:
|
2011-11-09 01:22:34 +04:00
|
|
|
StatefulFeatureFunction(const std::string& description, size_t numScoreComponents) :
|
|
|
|
FeatureFunction(description,numScoreComponents) {}
|
2009-02-06 18:43:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief This interface should be implemented.
|
|
|
|
* Notes: When evaluating the value of this feature function, you should avoid
|
|
|
|
* calling hypo.GetPrevHypo(). If you need something from the "previous"
|
|
|
|
* hypothesis, you should store it in an FFState object which will be passed
|
|
|
|
* in as prev_state. If you don't do this, you will get in trouble.
|
|
|
|
*/
|
|
|
|
virtual FFState* Evaluate(
|
|
|
|
const Hypothesis& cur_hypo,
|
|
|
|
const FFState* prev_state,
|
|
|
|
ScoreComponentCollection* accumulator) const = 0;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2011-06-16 01:31:27 +04:00
|
|
|
virtual FFState* EvaluateChart(
|
|
|
|
const ChartHypothesis& /* cur_hypo */,
|
2012-09-14 00:23:37 +04:00
|
|
|
int /* featureID - used to index the state in the previous hypotheses */,
|
2011-06-16 01:31:27 +04:00
|
|
|
ScoreComponentCollection* accumulator) const = 0;
|
|
|
|
|
2010-02-11 20:08:24 +03:00
|
|
|
//! return the state associated with the empty hypothesis for a given sentence
|
|
|
|
virtual const FFState* EmptyHypothesisState(const InputType &input) const = 0;
|
2009-02-06 18:43:06 +03:00
|
|
|
|
|
|
|
bool IsStateless() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|