00001 #include "moses/PP/NonTermContextProperty.h"
00002 #include <string>
00003 #include <cassert>
00004 #include "moses/Util.h"
00005 #include "moses/FactorCollection.h"
00006
00007 using namespace std;
00008
00009 namespace Moses
00010 {
00011 NonTermContextProperty::NonTermContextProperty()
00012 {
00013 }
00014
00015 NonTermContextProperty::~NonTermContextProperty()
00016 {
00017
00018 }
00019
00020 void NonTermContextProperty::ProcessValue(const std::string &value)
00021 {
00022 vector<string> toks;
00023 Tokenize(toks, value);
00024
00025 FactorCollection &fc = FactorCollection::Instance();
00026
00027 size_t numNT = Scan<size_t>(toks[0]);
00028 m_probStores.resize(numNT);
00029
00030 size_t ind = 1;
00031 while (ind < toks.size()) {
00032 vector<const Factor *> factors;
00033
00034 for (size_t nt = 0; nt < numNT; ++nt) {
00035 size_t ntInd = Scan<size_t>(toks[ind]);
00036 assert(nt == ntInd);
00037 ++ind;
00038
00039 for (size_t contextInd = 0; contextInd < 4; ++contextInd) {
00040
00041 const Factor *factor = fc.AddFactor(toks[ind], false);
00042 factors.push_back(factor);
00043 ++ind;
00044 }
00045 }
00046
00047
00048
00049 float count = Scan<float>(toks[ind]);
00050 ++ind;
00051
00052 for (size_t i = 0; i < factors.size(); ++i) {
00053 size_t ntInd = i / 4;
00054 size_t contextInd = i % 4;
00055 const Factor *factor = factors[i];
00056 AddToMap(ntInd, contextInd, factor, count);
00057 }
00058 }
00059 }
00060
00061 void NonTermContextProperty::AddToMap(size_t ntIndex, size_t index, const Factor *factor, float count)
00062 {
00063 if (ntIndex <= m_probStores.size()) {
00064 m_probStores.resize(ntIndex + 1);
00065 }
00066
00067 ProbStore &probStore = m_probStores[ntIndex];
00068 probStore.AddToMap(index, factor, count);
00069 }
00070
00071 float NonTermContextProperty::GetProb(size_t ntInd,
00072 size_t contextInd,
00073 const Factor *factor,
00074 float smoothConstant) const
00075 {
00076 UTIL_THROW_IF2(ntInd >= m_probStores.size(), "Invalid nt index=" << ntInd);
00077 const ProbStore &probStore = m_probStores[ntInd];
00078 float ret = probStore.GetProb(contextInd, factor, smoothConstant);
00079 return ret;
00080 }
00081
00083
00084 void NonTermContextProperty::ProbStore::AddToMap(size_t index, const Factor *factor, float count)
00085 {
00086 Map &map = m_vec[index];
00087
00088 Map::iterator iter = map.find(factor);
00089 if (iter == map.end()) {
00090 map[factor] = count;
00091 } else {
00092 float &currCount = iter->second;
00093 currCount += count;
00094 }
00095
00096 m_totalCount += count;
00097 }
00098
00099
00100 float NonTermContextProperty::ProbStore::GetProb(size_t contextInd,
00101 const Factor *factor,
00102 float smoothConstant) const
00103 {
00104 float count = GetCount(contextInd, factor, smoothConstant);
00105 float total = GetTotalCount(contextInd, smoothConstant);
00106 float ret = count / total;
00107 return ret;
00108 }
00109
00110 float NonTermContextProperty::ProbStore::GetCount(size_t contextInd,
00111 const Factor *factor,
00112 float smoothConstant) const
00113 {
00114 const Map &map = m_vec[contextInd];
00115
00116 float count = smoothConstant;
00117 Map::const_iterator iter = map.find(factor);
00118 if (iter == map.end()) {
00119
00120 } else {
00121 count += iter->second;
00122 }
00123
00124 return count;
00125 }
00126
00127 float NonTermContextProperty::ProbStore::GetTotalCount(size_t contextInd, float smoothConstant) const
00128 {
00129 const Map &map = m_vec[contextInd];
00130 return m_totalCount + smoothConstant * map.size();
00131 }
00132
00133
00134 }
00135