00001
00002
00003 #pragma once
00004 #include "moses/TranslationModel/UG/mm/ug_bitext.h"
00005 #include "util/exception.hh"
00006 #include "boost/format.hpp"
00007 #include "boost/foreach.hpp"
00008
00009 namespace sapt
00010 {
00011 template<typename Token>
00012 class
00013 PScorePfwd : public PhraseScorer<Token>
00014 {
00015 float conf;
00016 std::string denom;
00017
00018 public:
00019
00020 virtual ~PScorePfwd(){};
00021 PScorePfwd(float const c, std::string d)
00022 {
00023 this->m_index = -1;
00024 conf = c;
00025 denom = d;
00026 size_t checksum = d.size();
00027 BOOST_FOREACH(char const& x, denom)
00028 {
00029 if (x == '+') { --checksum; continue; }
00030 if (x != 'g' && x != 's' && x != 'r' && x != 'b') continue;
00031 std::string s = (boost::format("pfwd-%c%.3f") % x % c).str();
00032 this->m_feature_names.push_back(s);
00033 }
00034 this->m_num_feats = this->m_feature_names.size();
00035 UTIL_THROW_IF2(this->m_feature_names.size() != checksum,
00036 "Unknown parameter in specification '"
00037 << d << "' for Pfwd phrase scorer at " << HERE);
00038 }
00039
00040 void
00041 operator()(Bitext<Token> const& bt,
00042 PhrasePair<Token>& pp,
00043 std::vector<float> * dest = NULL) const
00044 {
00045 if (!dest) dest = &pp.fvals;
00046 if (pp.joint > pp.good1)
00047 {
00048 pp.joint = pp.good1;
00049
00050
00051 }
00052 size_t i = this->m_index;
00053 float g = log(lbop(pp.good1, pp.joint, conf));;
00054 BOOST_FOREACH(char const& c, this->denom)
00055 {
00056 switch (c)
00057 {
00058 case 'b':
00059 (*dest)[i++] = g + log(pp.cum_bias) - log(pp.joint);
00060 break;
00061 case 'g':
00062 (*dest)[i++] = g;
00063 break;
00064 case 's':
00065 (*dest)[i++] = log(lbop(pp.sample1, pp.joint, conf));
00066 break;
00067 case 'r':
00068 (*dest)[i++] = log(lbop(pp.raw1, pp.joint, conf));
00069 }
00070 }
00071 }
00072 };
00073 }
00074
00075