2015-03-21 20:09:41 +03:00
|
|
|
// -*- c++ -*-
|
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>
|
2013-02-22 00:03:35 +04:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2014-07-25 00:23:08 +04:00
|
|
|
#include "moses/FeatureVector.h"
|
2013-05-24 21:02:49 +04:00
|
|
|
#include "moses/TypeDef.h"
|
2015-12-08 15:56:55 +03:00
|
|
|
#include "moses/parameters/AllOptions.h"
|
2015-03-21 20:09:41 +03:00
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
namespace Moses
|
|
|
|
{
|
2009-02-06 18:43:06 +03:00
|
|
|
|
2015-12-08 15:56:55 +03:00
|
|
|
class AllOptions;
|
2013-05-28 13:51:28 +04:00
|
|
|
class Phrase;
|
2009-02-06 18:43:06 +03:00
|
|
|
class TargetPhrase;
|
2015-01-06 15:56:13 +03:00
|
|
|
class TranslationOptionList;
|
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;
|
2010-02-11 20:08:24 +03:00
|
|
|
class InputType;
|
2009-02-06 18:43:06 +03:00
|
|
|
class ScoreComponentCollection;
|
2015-10-25 16:07:25 +03:00
|
|
|
class Bitmap;
|
2015-10-25 16:37:59 +03:00
|
|
|
class Range;
|
2013-05-30 15:41:08 +04:00
|
|
|
class FactorMask;
|
2013-08-13 23:47:03 +04:00
|
|
|
class InputPath;
|
2014-05-08 20:51:45 +04:00
|
|
|
class StackVec;
|
2014-10-20 15:17:07 +04:00
|
|
|
class DistortionScoreProducer;
|
2015-04-30 08:05:11 +03:00
|
|
|
class TranslationTask;
|
2012-09-21 14:56:01 +04:00
|
|
|
|
2012-06-27 03:45:02 +04:00
|
|
|
/** base class for all feature functions.
|
|
|
|
*/
|
2013-02-22 00:03:35 +04:00
|
|
|
class FeatureFunction
|
2011-02-24 16:14:42 +03:00
|
|
|
{
|
2013-02-22 00:03:35 +04:00
|
|
|
protected:
|
2012-12-31 04:57:21 +04:00
|
|
|
/**< all the score producers in this run */
|
2013-11-15 20:43:39 +04:00
|
|
|
static std::vector<FeatureFunction*> s_staticColl;
|
2009-02-06 18:43:06 +03:00
|
|
|
|
2013-02-22 00:03:35 +04:00
|
|
|
std::string m_description, m_argLine;
|
|
|
|
std::vector<std::vector<std::string> > m_args;
|
2013-05-09 15:05:19 +04:00
|
|
|
bool m_tuneable;
|
2015-01-21 19:07:50 +03:00
|
|
|
bool m_requireSortingAfterSourceContext;
|
2014-12-03 23:04:26 +03:00
|
|
|
size_t m_verbosity;
|
2013-02-22 00:03:35 +04:00
|
|
|
size_t m_numScoreComponents;
|
2015-04-26 18:46:36 +03:00
|
|
|
size_t m_index; // index into vector covering ALL feature function values
|
2015-02-09 16:52:05 +03:00
|
|
|
std::vector<bool> m_tuneableComponents;
|
|
|
|
size_t m_numTuneableComponents;
|
2015-12-10 06:17:36 +03:00
|
|
|
AllOptions::ptr m_options;
|
2013-02-22 00:03:35 +04:00
|
|
|
//In case there's multiple producers with the same description
|
|
|
|
static std::multiset<std::string> description_counts;
|
|
|
|
|
2015-11-24 21:28:38 +03:00
|
|
|
public:
|
|
|
|
static void Register(FeatureFunction* ff);
|
2015-04-29 22:16:52 +03:00
|
|
|
private:
|
|
|
|
// void Initialize(const std::string &line);
|
2013-10-29 21:46:03 +04:00
|
|
|
void ParseLine(const std::string &line);
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2009-02-06 18:43:06 +03:00
|
|
|
public:
|
2013-05-29 21:16:15 +04:00
|
|
|
static const std::vector<FeatureFunction*>& GetFeatureFunctions() {
|
2013-11-15 20:43:39 +04:00
|
|
|
return s_staticColl;
|
2013-05-29 21:16:15 +04:00
|
|
|
}
|
2014-10-20 15:17:07 +04:00
|
|
|
|
2013-06-07 20:32:01 +04:00
|
|
|
static FeatureFunction &FindFeatureFunction(const std::string& name);
|
2014-01-01 00:15:43 +04:00
|
|
|
static void Destroy();
|
2012-12-31 04:57:21 +04:00
|
|
|
|
2015-11-06 15:45:22 +03:00
|
|
|
FeatureFunction(const std::string &line, bool registerNow);
|
|
|
|
FeatureFunction(size_t numScoreComponents, const std::string &line, bool registerNow = true);
|
2013-05-29 21:16:15 +04:00
|
|
|
virtual bool IsStateless() const = 0;
|
2009-02-06 18:43:06 +03:00
|
|
|
virtual ~FeatureFunction();
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2013-06-09 21:48:09 +04:00
|
|
|
//! override to load model files
|
2015-12-10 06:17:36 +03:00
|
|
|
virtual void Load(AllOptions::ptr const& opts) {
|
|
|
|
m_options = opts;
|
|
|
|
}
|
|
|
|
|
2015-12-12 03:00:41 +03:00
|
|
|
AllOptions::ptr const&
|
2015-12-10 06:17:36 +03:00
|
|
|
options() const {
|
|
|
|
return m_options;
|
2013-06-10 21:11:55 +04:00
|
|
|
}
|
2013-05-31 23:21:02 +04:00
|
|
|
|
2013-02-22 00:03:35 +04:00
|
|
|
static void ResetDescriptionCounts() {
|
|
|
|
description_counts.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns the number of scores that a subclass produces.
|
|
|
|
//! For example, a language model conventionally produces 1, a translation table some arbitrary number, etc
|
2013-05-29 21:16:15 +04:00
|
|
|
size_t GetNumScoreComponents() const {
|
|
|
|
return m_numScoreComponents;
|
|
|
|
}
|
2013-02-22 00:03:35 +04:00
|
|
|
|
|
|
|
//! returns a string description of this producer
|
2013-05-29 21:16:15 +04:00
|
|
|
const std::string& GetScoreProducerDescription() const {
|
|
|
|
return m_description;
|
|
|
|
}
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2014-07-25 00:23:08 +04:00
|
|
|
FName GetFeatureName(const std::string& name) const {
|
|
|
|
return FName(GetScoreProducerDescription(), name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-09 21:48:09 +04:00
|
|
|
//! if false, then this feature is not displayed in the n-best list.
|
|
|
|
// use with care
|
2013-05-29 21:16:15 +04:00
|
|
|
virtual bool IsTuneable() const {
|
|
|
|
return m_tuneable;
|
|
|
|
}
|
2015-01-21 19:07:50 +03:00
|
|
|
|
2015-02-09 16:52:05 +03:00
|
|
|
virtual bool HasTuneableComponents() const {
|
|
|
|
return m_numTuneableComponents;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool IsTuneableComponent(size_t i) const {
|
|
|
|
if (m_numTuneableComponents == m_numScoreComponents) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return m_tuneableComponents[i];
|
|
|
|
}
|
|
|
|
|
2015-01-21 19:07:50 +03:00
|
|
|
virtual bool RequireSortingAfterSourceContext() const {
|
|
|
|
return m_requireSortingAfterSourceContext;
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:35:50 +04:00
|
|
|
virtual std::vector<float> DefaultWeights() const;
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2015-04-26 18:46:36 +03:00
|
|
|
size_t GetIndex() const;
|
|
|
|
size_t SetIndex(size_t const idx);
|
2015-03-26 21:25:54 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void
|
2015-03-27 02:37:29 +03:00
|
|
|
CleanUpAfterSentenceProcessing(InputType const& source) { }
|
2015-03-26 21:25:54 +03:00
|
|
|
|
|
|
|
public:
|
2013-06-09 21:48:09 +04:00
|
|
|
//! Called before search and collecting of translation options
|
2015-04-30 08:05:11 +03:00
|
|
|
virtual void
|
2015-05-19 04:35:39 +03:00
|
|
|
InitializeForInput(ttasksptr const& ttask) { };
|
2013-02-22 00:03:35 +04:00
|
|
|
|
|
|
|
// clean up temporary memory, called after processing each sentence
|
2015-04-30 08:05:11 +03:00
|
|
|
virtual void
|
|
|
|
CleanUpAfterSentenceProcessing(ttasksptr const& ttask);
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2015-03-26 21:25:54 +03:00
|
|
|
const std::string &
|
2015-05-02 13:45:24 +03:00
|
|
|
GetArgLine() const {
|
|
|
|
return m_argLine;
|
|
|
|
}
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2013-06-09 15:47:27 +04:00
|
|
|
// given a target phrase containing only factors specified in mask
|
|
|
|
// return true if the feature function can be evaluated
|
2013-05-30 15:41:08 +04:00
|
|
|
virtual bool IsUseable(const FactorMask &mask) const = 0;
|
2013-05-30 14:25:57 +04:00
|
|
|
|
2015-03-26 21:25:54 +03:00
|
|
|
// used by stateless ff and stateful ff. Calculate initial score
|
|
|
|
// estimate during loading of phrase table
|
|
|
|
//
|
|
|
|
// source phrase is the substring that the phrase table uses to look
|
|
|
|
// up the target phrase,
|
|
|
|
//
|
2013-08-08 15:26:25 +04:00
|
|
|
// may have more factors than actually need, but not guaranteed.
|
2015-03-26 21:25:54 +03:00
|
|
|
// For SCFG decoding, the source contains non-terminals, NOT the raw
|
|
|
|
// source from the input sentence
|
2015-04-30 08:05:11 +03:00
|
|
|
virtual void
|
2015-03-26 21:25:54 +03:00
|
|
|
EvaluateInIsolation(const Phrase &source, const TargetPhrase &targetPhrase,
|
2015-05-02 13:45:24 +03:00
|
|
|
ScoreComponentCollection& scoreBreakdown,
|
2015-11-04 18:10:45 +03:00
|
|
|
ScoreComponentCollection& estimatedScores) const = 0;
|
2015-04-30 08:05:11 +03:00
|
|
|
|
2015-03-21 20:09:41 +03:00
|
|
|
// for context-dependent processing
|
|
|
|
static void SetupAll(TranslationTask const& task);
|
|
|
|
virtual void Setup(TranslationTask const& task) const { };
|
2014-08-02 01:41:24 +04:00
|
|
|
|
2013-08-08 20:35:42 +04:00
|
|
|
// This method is called once all the translation options are retrieved from the phrase table, and
|
|
|
|
// just before search.
|
2013-08-13 23:47:03 +04:00
|
|
|
// 'inputPath' is guaranteed to be the raw substring from the input. No factors were added or taken away
|
2014-06-02 21:17:39 +04:00
|
|
|
// 'stackVec' is a vector of chart cells that the RHS non-terms cover.
|
|
|
|
// It is guaranteed to be in the same order as the non-terms in the source phrase.
|
|
|
|
// For pb models, stackvec is NULL.
|
2015-11-04 18:10:45 +03:00
|
|
|
// No FF should set estimatedScores in both overloads!
|
2014-07-10 02:06:54 +04:00
|
|
|
virtual void EvaluateWithSourceContext(const InputType &input
|
2015-01-14 14:07:42 +03:00
|
|
|
, const InputPath &inputPath
|
|
|
|
, const TargetPhrase &targetPhrase
|
|
|
|
, const StackVec *stackVec
|
|
|
|
, ScoreComponentCollection &scoreBreakdown
|
2015-11-04 18:10:45 +03:00
|
|
|
, ScoreComponentCollection *estimatedScores = NULL) const = 0;
|
2015-01-14 14:07:42 +03:00
|
|
|
|
2015-01-06 12:46:31 +03:00
|
|
|
// This method is called once all the translation options are retrieved from the phrase table, and
|
|
|
|
// just before search.
|
|
|
|
// 'inputPath' is guaranteed to be the raw substring from the input. No factors were added or taken away
|
|
|
|
// 'stackVec' is a vector of chart cells that the RHS non-terms cover.
|
|
|
|
// It is guaranteed to be in the same order as the non-terms in the source phrase.
|
|
|
|
// For pb models, stackvec is NULL.
|
2015-11-04 18:10:45 +03:00
|
|
|
// No FF should set estimatedScores in both overloads!
|
2015-01-06 15:56:13 +03:00
|
|
|
virtual void EvaluateTranslationOptionListWithSourceContext(const InputType &input
|
2015-01-14 14:07:42 +03:00
|
|
|
, const TranslationOptionList &translationOptionList) const = 0;
|
2013-06-07 20:32:01 +04:00
|
|
|
|
2013-06-20 16:25:02 +04:00
|
|
|
virtual void SetParameter(const std::string& key, const std::string& value);
|
2013-06-20 16:06:03 +04:00
|
|
|
virtual void ReadParameters();
|
2015-02-09 16:52:05 +03:00
|
|
|
virtual void SetTuneableComponents(const std::string& value);
|
2009-02-06 18:43:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|