|
#pragma once |
|
|
|
#include <string> |
|
#include <boost/thread/tss.hpp> |
|
|
|
#include "vw/Classifier.h" |
|
#include "moses/TypeDef.h" |
|
#include "moses/TranslationTask.h" |
|
#include "moses/Util.h" |
|
#include "moses/FF/StatelessFeatureFunction.h" |
|
|
|
namespace Moses |
|
{ |
|
|
|
enum VWFeatureType { |
|
vwft_source, |
|
vwft_target, |
|
vwft_targetContext |
|
}; |
|
|
|
class VWFeatureBase : public StatelessFeatureFunction |
|
{ |
|
public: |
|
VWFeatureBase(const std::string &line, VWFeatureType featureType = vwft_source) |
|
: StatelessFeatureFunction(0, line), m_usedBy(1, "VW0"), m_featureType(featureType) { |
|
|
|
m_sourceFactors.push_back(0); |
|
m_targetFactors.push_back(0); |
|
} |
|
|
|
bool IsUseable(const FactorMask &mask) const { |
|
return true; |
|
} |
|
|
|
|
|
|
|
void EvaluateInIsolation(const Phrase &source |
|
, const TargetPhrase &targetPhrase |
|
, ScoreComponentCollection &scoreBreakdown |
|
, ScoreComponentCollection &estimatedFutureScore) const {} |
|
void EvaluateWithSourceContext(const InputType &input |
|
, const InputPath &inputPath |
|
, const TargetPhrase &targetPhrase |
|
, const StackVec *stackVec |
|
, ScoreComponentCollection &scoreBreakdown |
|
, ScoreComponentCollection *estimatedFutureScore = NULL) const {} |
|
void EvaluateTranslationOptionListWithSourceContext(const InputType &input |
|
, const TranslationOptionList &translationOptionList) const {} |
|
void EvaluateWhenApplied(const Hypothesis& hypo, |
|
ScoreComponentCollection* accumulator) const {} |
|
void EvaluateWhenApplied(const ChartHypothesis &hypo, |
|
ScoreComponentCollection* accumulator) const {} |
|
|
|
|
|
|
|
virtual void SetParameter(const std::string& key, const std::string& value) { |
|
if (key == "used-by") { |
|
ParseUsedBy(value); |
|
} else if (key == "source-factors") { |
|
Tokenize<FactorType>(m_sourceFactors, value, ","); |
|
} else if (key == "target-factors") { |
|
Tokenize<FactorType>(m_targetFactors, value, ","); |
|
} else { |
|
StatelessFeatureFunction::SetParameter(key, value); |
|
} |
|
} |
|
|
|
|
|
static const std::vector<VWFeatureBase*>& GetFeatures(std::string name = "VW0") { |
|
UTIL_THROW_IF2(s_features.count(name) == 0, "No features registered for parent classifier: " + name); |
|
return s_features[name]; |
|
} |
|
|
|
|
|
static const std::vector<VWFeatureBase*>& GetSourceFeatures(std::string name = "VW0") { |
|
UTIL_THROW_IF2(s_sourceFeatures.count(name) == 0, "No source features registered for parent classifier: " + name); |
|
return s_sourceFeatures[name]; |
|
} |
|
|
|
|
|
static const std::vector<VWFeatureBase*>& GetTargetContextFeatures(std::string name = "VW0") { |
|
|
|
return s_targetContextFeatures[name]; |
|
} |
|
|
|
|
|
static const std::vector<VWFeatureBase*>& GetTargetFeatures(std::string name = "VW0") { |
|
UTIL_THROW_IF2(s_targetFeatures.count(name) == 0, "No target features registered for parent classifier: " + name); |
|
return s_targetFeatures[name]; |
|
} |
|
|
|
|
|
static size_t GetMaximumContextSize(std::string name = "VW0") { |
|
return s_targetContextLength[name]; |
|
} |
|
|
|
|
|
|
|
virtual void operator()(const InputType &input |
|
, const Range &sourceRange |
|
, Discriminative::Classifier &classifier |
|
, Discriminative::FeatureVector &outFeatures) const = 0; |
|
|
|
|
|
|
|
|
|
virtual void operator()(const InputType &input |
|
, const TargetPhrase &targetPhrase |
|
, Discriminative::Classifier &classifier |
|
, Discriminative::FeatureVector &outFeatures) const = 0; |
|
|
|
|
|
|
|
|
|
|
|
virtual void operator()(const InputType &input |
|
, const Phrase &contextPhrase |
|
, const AlignmentInfo &alignmentInfo |
|
, Discriminative::Classifier &classifier |
|
, Discriminative::FeatureVector &outFeatures) const = 0; |
|
|
|
protected: |
|
std::vector<FactorType> m_sourceFactors, m_targetFactors; |
|
|
|
void UpdateRegister() { |
|
for(std::vector<std::string>::const_iterator it = m_usedBy.begin(); |
|
it != m_usedBy.end(); it++) { |
|
s_features[*it].push_back(this); |
|
|
|
if(m_featureType == vwft_source) { |
|
s_sourceFeatures[*it].push_back(this); |
|
} else if (m_featureType == vwft_targetContext) { |
|
s_targetContextFeatures[*it].push_back(this); |
|
UpdateContextSize(*it); |
|
} else { |
|
s_targetFeatures[*it].push_back(this); |
|
} |
|
} |
|
} |
|
|
|
private: |
|
void ParseUsedBy(const std::string &usedBy) { |
|
m_usedBy.clear(); |
|
Tokenize(m_usedBy, usedBy, ","); |
|
} |
|
|
|
void UpdateContextSize(const std::string &usedBy); |
|
|
|
std::vector<std::string> m_usedBy; |
|
VWFeatureType m_featureType; |
|
static std::map<std::string, std::vector<VWFeatureBase*> > s_features; |
|
static std::map<std::string, std::vector<VWFeatureBase*> > s_sourceFeatures; |
|
static std::map<std::string, std::vector<VWFeatureBase*> > s_targetContextFeatures; |
|
static std::map<std::string, std::vector<VWFeatureBase*> > s_targetFeatures; |
|
|
|
static std::map<std::string, size_t> s_targetContextLength; |
|
}; |
|
|
|
} |
|
|
|
|