00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef moses_ScoreComponentCollection_h
00023 #define moses_ScoreComponentCollection_h
00024
00025 #include <numeric>
00026 #include "util/check.hh"
00027
00028 #include "LMList.h"
00029 #include "ScoreProducer.h"
00030 #include "ScoreIndexManager.h"
00031 #include "TypeDef.h"
00032 #include "Util.h"
00033
00034 namespace Moses
00035 {
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 class ScoreComponentCollection
00056 {
00057 friend std::ostream& operator<<(std::ostream& os, const ScoreComponentCollection& rhs);
00058 friend class ScoreIndexManager;
00059 private:
00060 std::vector<float> m_scores;
00061 const ScoreIndexManager* m_sim;
00062
00063 public:
00065 ScoreComponentCollection();
00066
00068 ScoreComponentCollection(const ScoreComponentCollection& rhs)
00069 : m_scores(rhs.m_scores)
00070 , m_sim(rhs.m_sim)
00071 {}
00072
00073 inline size_t size() const {
00074 return m_scores.size();
00075 }
00076 const float& operator[](size_t x) const {
00077 return m_scores[x];
00078 }
00079
00081 void ZeroAll() {
00082 for (std::vector<float>::iterator i=m_scores.begin(); i!=m_scores.end(); ++i)
00083 *i = 0.0f;
00084 }
00085
00087 void PlusEquals(const ScoreComponentCollection& rhs) {
00088 CHECK(m_scores.size() >= rhs.m_scores.size());
00089 const size_t l = rhs.m_scores.size();
00090 for (size_t i=0; i<l; i++) {
00091 m_scores[i] += rhs.m_scores[i];
00092 }
00093 }
00094
00096 void MinusEquals(const ScoreComponentCollection& rhs) {
00097 CHECK(m_scores.size() >= rhs.m_scores.size());
00098 const size_t l = rhs.m_scores.size();
00099 for (size_t i=0; i<l; i++) {
00100 m_scores[i] -= rhs.m_scores[i];
00101 }
00102 }
00103
00107 void PlusEquals(const ScoreProducer* sp, const std::vector<float>& scores) {
00108 CHECK(scores.size() == sp->GetNumScoreComponents());
00109 size_t i = m_sim->GetBeginIndex(sp->GetScoreBookkeepingID());
00110 for (std::vector<float>::const_iterator vi = scores.begin();
00111 vi != scores.end(); ++vi) {
00112 m_scores[i++] += *vi;
00113 }
00114 }
00115
00119 void PlusEquals(const ScoreProducer* sp, const ScoreComponentCollection& scores) {
00120 size_t i = m_sim->GetBeginIndex(sp->GetScoreBookkeepingID());
00121 const size_t end = m_sim->GetEndIndex(sp->GetScoreBookkeepingID());
00122 for (; i < end; ++i) {
00123 m_scores[i] += scores.m_scores[i];
00124 }
00125 }
00126
00130 void PlusEquals(const ScoreProducer* sp, float score) {
00131 CHECK(1 == sp->GetNumScoreComponents());
00132 const size_t i = m_sim->GetBeginIndex(sp->GetScoreBookkeepingID());
00133 m_scores[i] += score;
00134 }
00135
00136 void Assign(const ScoreProducer* sp, const std::vector<float>& scores) {
00137 CHECK(scores.size() == sp->GetNumScoreComponents());
00138 size_t i = m_sim->GetBeginIndex(sp->GetScoreBookkeepingID());
00139 for (std::vector<float>::const_iterator vi = scores.begin();
00140 vi != scores.end(); ++vi) {
00141 m_scores[i++] = *vi;
00142 }
00143 }
00144
00145 void Assign(const ScoreComponentCollection ©) {
00146 m_scores = copy.m_scores;
00147 }
00148
00152 void Assign(const ScoreProducer* sp, float score) {
00153 CHECK(1 == sp->GetNumScoreComponents());
00154 const size_t i = m_sim->GetBeginIndex(sp->GetScoreBookkeepingID());
00155 m_scores[i] = score;
00156 }
00157
00160 float InnerProduct(const std::vector<float>& rhs) const {
00161 return std::inner_product(m_scores.begin(), m_scores.end(), rhs.begin(), 0.0f);
00162 }
00163
00164 float PartialInnerProduct(const ScoreProducer* sp, const std::vector<float>& rhs) const {
00165 std::vector<float> lhs = GetScoresForProducer(sp);
00166 CHECK(lhs.size() == rhs.size());
00167 return std::inner_product(lhs.begin(), lhs.end(), rhs.begin(), 0.0f);
00168 }
00169
00171 std::vector<float> GetScoresForProducer(const ScoreProducer* sp) const {
00172 size_t id = sp->GetScoreBookkeepingID();
00173 const size_t begin = m_sim->GetBeginIndex(id);
00174 const size_t end = m_sim->GetEndIndex(id);
00175 std::vector<float> res(end-begin);
00176 size_t j = 0;
00177 for (size_t i = begin; i < end; i++) {
00178 res[j++] = m_scores[i];
00179 }
00180 return res;
00181 }
00182
00185 float GetScoreForProducer(const ScoreProducer* sp) const {
00186 size_t id = sp->GetScoreBookkeepingID();
00187 const size_t begin = m_sim->GetBeginIndex(id);
00188 #ifndef NDEBUG
00189 const size_t end = m_sim->GetEndIndex(id);
00190 CHECK(end-begin == 1);
00191 #endif
00192 return m_scores[begin];
00193 }
00194
00195 float GetWeightedScore() const;
00196
00197 void ZeroAllLM(const LMList& lmList);
00198 void PlusEqualsAllLM(const LMList& lmList, const ScoreComponentCollection& rhs);
00199
00200 };
00201
00202 inline std::ostream& operator<<(std::ostream& os, const ScoreComponentCollection& rhs)
00203 {
00204 os << "<<" << rhs.m_scores[0];
00205 for (size_t i=1; i<rhs.m_scores.size(); i++)
00206 os << ", " << rhs.m_scores[i];
00207 return os << ">>";
00208 }
00209
00210
00211 }
00212 #endif