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 PScorePbwd : public PhraseScorer<Token>
00014 {
00015 float conf;
00016 std::string denom;
00017
00018 public:
00019 virtual ~PScorePbwd(){};
00020 PScorePbwd(float const c, std::string d)
00021 {
00022 this->m_index = -1;
00023 conf = c;
00024 denom = d;
00025 size_t checksum = d.size();
00026 BOOST_FOREACH(char const& x, denom)
00027 {
00028 if (x == '+') { --checksum; continue; }
00029 if (x != 'g' && x != 's' && x != 'r' && x != 'b') continue;
00030 std::string s = (boost::format("pbwd-%c%.3f") % x % c).str();
00031 this->m_feature_names.push_back(s);
00032 }
00033 this->m_num_feats = this->m_feature_names.size();
00034 UTIL_THROW_IF2(this->m_feature_names.size() != checksum,
00035 "Unknown parameter in specification '"
00036 << d << "' for Pbwd phrase scorer at " << HERE);
00037 }
00038
00039 void
00040 operator()(Bitext<Token> const& bt,
00041 PhrasePair<Token>& pp,
00042 std::vector<float> * dest = NULL) const
00043 {
00044 if (!dest) dest = &pp.fvals;
00045
00046
00047 size_t i = this->m_index;
00048 BOOST_FOREACH(char const& x, denom)
00049 {
00050 uint32_t m2 = pp.raw2;
00051 if (x == 'g' || x == 'b') m2 = round(m2 * float(pp.good1) / pp.raw1);
00052 else if (x == 's') m2 = round(m2 * float(pp.sample1) / pp.raw1);
00053
00054 (*dest)[i] = log(lbop(std::max(m2, pp.joint), pp.joint,conf));
00055 if (x == 'b') (*dest)[i] += log(pp.cum_bias) - log(pp.joint);
00056 ++i;
00057 }
00058 }
00059 };
00060 }
00061