00001
00002
00003
00004 #pragma once
00005 #include "moses/TranslationModel/UG/mm/ug_bitext.h"
00006 #include "util/exception.hh"
00007 #include "boost/format.hpp"
00008
00009
00010 namespace sapt
00011 {
00012
00013
00014 template<typename Token>
00015 class
00016 PhraseScorer
00017 {
00018 protected:
00019 int m_index;
00020 int m_num_feats;
00021 std::string m_tag;
00022 std::vector<std::string> m_feature_names;
00023 public:
00024
00025
00026 virtual ~PhraseScorer() {}
00027
00028 virtual void
00029 operator()(Bitext<Token> const& pt, PhrasePair<Token>& pp,
00030 std::vector<float> * dest=NULL) const = 0;
00031
00032 void
00033 setIndex(int const i) { m_index = i; }
00034
00035 int
00036 getIndex() const { return m_index; }
00037
00038 int
00039 fcnt() const { return m_num_feats; }
00040
00041 std::vector<std::string> const &
00042 fnames() const { return m_feature_names; }
00043
00044 std::string const &
00045 fname(int i) const
00046 {
00047 if (i < 0) i += m_num_feats;
00048 UTIL_THROW_IF2(i < 0 || i >= m_num_feats,
00049 "Feature name index out of range at " << HERE);
00050 return m_feature_names.at(i);
00051 }
00052
00053 virtual
00054 bool
00055 isLogVal(int i) const { return true; };
00056
00057
00058 virtual
00059 bool
00060 isIntegerValued(int i) const { return false; };
00061
00062
00063 virtual
00064 bool
00065 allowPooling() const { return true; }
00066
00067
00068
00069 virtual
00070 void
00071 load() { }
00072
00073 };
00074
00075
00076 template<typename Token>
00077 class
00078 SingleRealValuedParameterPhraseScorerFamily
00079 : public PhraseScorer<Token>
00080 {
00081 protected:
00082 std::vector<float> m_x;
00083
00084 virtual
00085 void
00086 init(std::string const specs)
00087 {
00088 UTIL_THROW_IF2(this->m_tag.size() == 0,
00089 "m_tag must be initialized in constructor");
00090 UTIL_THROW_IF2(specs.size() == 0,"empty specification string!");
00091 UTIL_THROW_IF2(this->m_feature_names.size(),
00092 "PhraseScorer can only be initialized once!");
00093 this->m_index = -1;
00094 float x; char c;
00095 for (std::istringstream buf(specs); buf>>x; buf>>c)
00096 {
00097 this->m_x.push_back(x);
00098 std::string fname = (boost::format("%s-%.2f") % this->m_tag % x).str();
00099 this->m_feature_names.push_back(fname);
00100 }
00101 this->m_num_feats = this->m_x.size();
00102 }
00103 };
00104 }
00105