00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00028 #pragma once
00029
00030 #include <string>
00031 #include <map>
00032 #include <vector>
00033
00034 #include <boost/shared_ptr.hpp>
00035
00036 #include "util/exception.hh"
00037
00038 #include "ExtractionPhrasePair.h"
00039
00040 namespace MosesTraining
00041 {
00042
00043 struct MaybeLog {
00044 MaybeLog(bool useLog, float negativeLog):
00045 m_useLog(useLog), m_negativeLog(negativeLog) {}
00046
00047 inline float operator() (float a) const {
00048 return m_useLog ? m_negativeLog*log(a) : a;
00049 }
00050
00051 float m_useLog;
00052 float m_negativeLog;
00053 };
00054
00055 class ScoreFeatureArgumentException : public util::Exception
00056 {
00057 public:
00058 ScoreFeatureArgumentException() throw() {
00059 *this << "Unable to configure features: ";
00060 }
00061 ~ScoreFeatureArgumentException() throw() {}
00062 };
00063
00065 struct ScoreFeatureContext {
00066 ScoreFeatureContext(
00067 const ExtractionPhrasePair &thePhrasePair,
00068 const MaybeLog& theMaybeLog
00069 ) :
00070 phrasePair(thePhrasePair),
00071 maybeLog(theMaybeLog) {
00072 }
00073
00074 const ExtractionPhrasePair &phrasePair;
00075 MaybeLog maybeLog;
00076 };
00077
00082 class ScoreFeature
00083 {
00084 public:
00085
00089 virtual void addPropertiesToPhrasePair(ExtractionPhrasePair &phrasePair,
00090 float count,
00091 int sentenceId) const {};
00092
00094 virtual void add(const ScoreFeatureContext& context,
00095 std::vector<float>& denseValues,
00096 std::map<std::string,float>& sparseValues) const = 0;
00097
00098 virtual ~ScoreFeature() {}
00099
00100 };
00101
00102 typedef boost::shared_ptr<ScoreFeature> ScoreFeaturePtr;
00103 class ScoreFeatureManager
00104 {
00105 public:
00106 ScoreFeatureManager():
00107 m_includeSentenceId(false) {}
00108
00110 const std::string& usage() const;
00111
00113 void configure(const std::vector<std::string> args);
00114
00118 void addPropertiesToPhrasePair(ExtractionPhrasePair &phrasePair,
00119 float count,
00120 int sentenceId) const;
00121
00123 void addFeatures(const ScoreFeatureContext& context,
00124 std::vector<float>& denseValues,
00125 std::map<std::string,float>& sparseValues) const;
00126
00127 const std::vector<ScoreFeaturePtr>& getFeatures() const {
00128 return m_features;
00129 }
00130
00132 bool includeSentenceId() const {
00133 return m_includeSentenceId;
00134 }
00135
00136 private:
00137 std::vector<ScoreFeaturePtr> m_features;
00138 bool m_includeSentenceId;
00139 };
00140
00141 }
00142
00143