00001 #pragma once
00002
00003 #include <string>
00004 #include <boost/thread/tss.hpp>
00005
00006 #include "vw/Classifier.h"
00007 #include "moses/TypeDef.h"
00008 #include "moses/TranslationTask.h"
00009 #include "moses/Util.h"
00010 #include "moses/FF/StatelessFeatureFunction.h"
00011
00012 namespace Moses
00013 {
00014
00015 enum VWFeatureType {
00016 vwft_source,
00017 vwft_target,
00018 vwft_targetContext
00019 };
00020
00021 class VWFeatureBase : public StatelessFeatureFunction
00022 {
00023 public:
00024 VWFeatureBase(const std::string &line, VWFeatureType featureType = vwft_source)
00025 : StatelessFeatureFunction(0, line), m_usedBy(1, "VW0"), m_featureType(featureType) {
00026
00027 m_sourceFactors.push_back(0);
00028 m_targetFactors.push_back(0);
00029 }
00030
00031 bool IsUseable(const FactorMask &mask) const {
00032 return true;
00033 }
00034
00035
00036
00037 void EvaluateInIsolation(const Phrase &source
00038 , const TargetPhrase &targetPhrase
00039 , ScoreComponentCollection &scoreBreakdown
00040 , ScoreComponentCollection &estimatedFutureScore) const {}
00041 void EvaluateWithSourceContext(const InputType &input
00042 , const InputPath &inputPath
00043 , const TargetPhrase &targetPhrase
00044 , const StackVec *stackVec
00045 , ScoreComponentCollection &scoreBreakdown
00046 , ScoreComponentCollection *estimatedFutureScore = NULL) const {}
00047 void EvaluateTranslationOptionListWithSourceContext(const InputType &input
00048 , const TranslationOptionList &translationOptionList) const {}
00049 void EvaluateWhenApplied(const Hypothesis& hypo,
00050 ScoreComponentCollection* accumulator) const {}
00051 void EvaluateWhenApplied(const ChartHypothesis &hypo,
00052 ScoreComponentCollection* accumulator) const {}
00053
00054
00055
00056 virtual void SetParameter(const std::string& key, const std::string& value) {
00057 if (key == "used-by") {
00058 ParseUsedBy(value);
00059 } else if (key == "source-factors") {
00060 Tokenize<FactorType>(m_sourceFactors, value, ",");
00061 } else if (key == "target-factors") {
00062 Tokenize<FactorType>(m_targetFactors, value, ",");
00063 } else {
00064 StatelessFeatureFunction::SetParameter(key, value);
00065 }
00066 }
00067
00068
00069 static const std::vector<VWFeatureBase*>& GetFeatures(std::string name = "VW0") {
00070 UTIL_THROW_IF2(s_features.count(name) == 0, "No features registered for parent classifier: " + name);
00071 return s_features[name];
00072 }
00073
00074
00075 static const std::vector<VWFeatureBase*>& GetSourceFeatures(std::string name = "VW0") {
00076 UTIL_THROW_IF2(s_sourceFeatures.count(name) == 0, "No source features registered for parent classifier: " + name);
00077 return s_sourceFeatures[name];
00078 }
00079
00080
00081 static const std::vector<VWFeatureBase*>& GetTargetContextFeatures(std::string name = "VW0") {
00082
00083 return s_targetContextFeatures[name];
00084 }
00085
00086
00087 static const std::vector<VWFeatureBase*>& GetTargetFeatures(std::string name = "VW0") {
00088 UTIL_THROW_IF2(s_targetFeatures.count(name) == 0, "No target features registered for parent classifier: " + name);
00089 return s_targetFeatures[name];
00090 }
00091
00092
00093 static size_t GetMaximumContextSize(std::string name = "VW0") {
00094 return s_targetContextLength[name];
00095 }
00096
00097
00098
00099 virtual void operator()(const InputType &input
00100 , const Range &sourceRange
00101 , Discriminative::Classifier &classifier
00102 , Discriminative::FeatureVector &outFeatures) const = 0;
00103
00104
00105
00106
00107 virtual void operator()(const InputType &input
00108 , const TargetPhrase &targetPhrase
00109 , Discriminative::Classifier &classifier
00110 , Discriminative::FeatureVector &outFeatures) const = 0;
00111
00112
00113
00114
00115
00116 virtual void operator()(const InputType &input
00117 , const Phrase &contextPhrase
00118 , const AlignmentInfo &alignmentInfo
00119 , Discriminative::Classifier &classifier
00120 , Discriminative::FeatureVector &outFeatures) const = 0;
00121
00122 protected:
00123 std::vector<FactorType> m_sourceFactors, m_targetFactors;
00124
00125 void UpdateRegister() {
00126 for(std::vector<std::string>::const_iterator it = m_usedBy.begin();
00127 it != m_usedBy.end(); it++) {
00128 s_features[*it].push_back(this);
00129
00130 if(m_featureType == vwft_source) {
00131 s_sourceFeatures[*it].push_back(this);
00132 } else if (m_featureType == vwft_targetContext) {
00133 s_targetContextFeatures[*it].push_back(this);
00134 UpdateContextSize(*it);
00135 } else {
00136 s_targetFeatures[*it].push_back(this);
00137 }
00138 }
00139 }
00140
00141 private:
00142 void ParseUsedBy(const std::string &usedBy) {
00143 m_usedBy.clear();
00144 Tokenize(m_usedBy, usedBy, ",");
00145 }
00146
00147 void UpdateContextSize(const std::string &usedBy);
00148
00149 std::vector<std::string> m_usedBy;
00150 VWFeatureType m_featureType;
00151 static std::map<std::string, std::vector<VWFeatureBase*> > s_features;
00152 static std::map<std::string, std::vector<VWFeatureBase*> > s_sourceFeatures;
00153 static std::map<std::string, std::vector<VWFeatureBase*> > s_targetContextFeatures;
00154 static std::map<std::string, std::vector<VWFeatureBase*> > s_targetFeatures;
00155
00156 static std::map<std::string, size_t> s_targetContextLength;
00157 };
00158
00159 }
00160