00001
00002
00003 #include <iostream>
00004 #include <iomanip>
00005 #include <string>
00006 #include <cstdio>
00007 #include "util/check.hh"
00008 #include "Util.h"
00009 #include "StaticData.h"
00010 #include "ScoreIndexManager.h"
00011 #include "ScoreProducer.h"
00012 #include "ScoreComponentCollection.h"
00013
00014 namespace Moses
00015 {
00016 using namespace std;
00017
00018 void ScoreIndexManager::AddScoreProducer(const ScoreProducer* sp)
00019 {
00020
00021 const_cast<ScoreProducer*>(sp)->CreateScoreBookkeepingID();
00022 CHECK(m_begins.size() == (sp->GetScoreBookkeepingID()));
00023
00024 m_producers.push_back(sp);
00025
00026 m_begins.push_back(m_last);
00027 size_t numScoreCompsProduced = sp->GetNumScoreComponents();
00028 CHECK(numScoreCompsProduced > 0);
00029 m_last += numScoreCompsProduced;
00030 m_ends.push_back(m_last);
00031 VERBOSE(3,"Added ScoreProducer(" << sp->GetScoreBookkeepingID()
00032 << " " << sp->GetScoreProducerDescription()
00033 << ") index=" << m_begins.back() << "-" << m_ends.back()-1 << std::endl);
00034
00035 }
00036
00037 void ScoreIndexManager::PrintLabeledScores(std::ostream& os, const ScoreComponentCollection& scores) const
00038 {
00039 std::vector<float> weights(scores.m_scores.size(), 1.0f);
00040 PrintLabeledWeightedScores(os, scores, weights);
00041 }
00042
00043 void ScoreIndexManager::PrintLabeledWeightedScores(std::ostream& os, const ScoreComponentCollection& scores, const std::vector<float>& weights) const
00044 {
00045 CHECK(m_featureShortNames.size() == weights.size());
00046 string lastName = "";
00047 for (size_t i = 0; i < m_featureShortNames.size(); ++i) {
00048 if (i>0) {
00049 os << " ";
00050 }
00051 if (lastName != m_featureShortNames[i]) {
00052 os << m_featureShortNames[i] << ": ";
00053 lastName = m_featureShortNames[i];
00054 }
00055 os << weights[i] * scores[i];
00056 }
00057 }
00058
00059 void ScoreIndexManager::InitFeatureNames()
00060 {
00061 m_featureNames.clear();
00062 m_featureShortNames.clear();
00063 size_t cur_i = 0;
00064 size_t cur_scoreType = 0;
00065 while (cur_i < m_last) {
00066 size_t nis_idx = 0;
00067 bool add_idx = (m_producers[cur_scoreType]->GetNumInputScores() > 1);
00068 while (nis_idx < m_producers[cur_scoreType]->GetNumInputScores()) {
00069 ostringstream os;
00070 os << m_producers[cur_scoreType]->GetScoreProducerDescription(nis_idx);
00071 if (add_idx)
00072 os << '_' << (nis_idx+1);
00073 m_featureNames.push_back(os.str());
00074 nis_idx++;
00075 cur_i++;
00076 }
00077
00078 int ind = 1;
00079 add_idx = (m_ends[cur_scoreType] - cur_i > 1);
00080 while (cur_i < m_ends[cur_scoreType]) {
00081 ostringstream os;
00082 os << m_producers[cur_scoreType]->GetScoreProducerDescription(nis_idx+ind-1);
00083 if (add_idx)
00084 os << '_' << ind;
00085 m_featureNames.push_back(os.str());
00086 m_featureShortNames.push_back( m_producers[cur_scoreType]->GetScoreProducerWeightShortName(nis_idx+ind-1) );
00087 ++cur_i;
00088 ++ind;
00089 }
00090 cur_scoreType++;
00091 }
00092 }
00093
00094 #ifdef HAVE_PROTOBUF
00095 void ScoreIndexManager::SerializeFeatureNamesToPB(hgmert::Hypergraph* hg) const
00096 {
00097 for (size_t i = 0; i < m_featureNames.size(); ++i) {
00098 hg->add_feature_names(m_featureNames[i]);
00099 }
00100 }
00101 #endif
00102
00103 void ScoreIndexManager::InitWeightVectorFromFile(const std::string& fnam, vector<float>* m_allWeights) const
00104 {
00105 CHECK(m_allWeights->size() == m_featureNames.size());
00106 ifstream in(fnam.c_str());
00107 CHECK(in.good());
00108 char buf[2000];
00109 map<string, double> name2val;
00110 while (!in.eof()) {
00111 in.getline(buf, 2000);
00112 if (strlen(buf) == 0) continue;
00113 if (buf[0] == '#') continue;
00114 istringstream is(buf);
00115 string fname;
00116 double val;
00117 is >> fname >> val;
00118 map<string, double>::iterator i = name2val.find(fname);
00119 CHECK(i == name2val.end());
00120 name2val[fname] = val;
00121 }
00122 CHECK(m_allWeights->size() == m_featureNames.size());
00123 for (size_t i = 0; i < m_featureNames.size(); ++i) {
00124 map<string, double>::iterator iter = name2val.find(m_featureNames[i]);
00125 if (iter == name2val.end()) {
00126 cerr << "No weight found found for feature: " << m_featureNames[i] << endl;
00127 abort();
00128 }
00129 (*m_allWeights)[i] = iter->second;
00130 }
00131 }
00132
00133 std::ostream& operator<<(std::ostream& os, const ScoreIndexManager& sim)
00134 {
00135 for (size_t i = 0; i < sim.m_featureNames.size(); ++i) {
00136 os << sim.m_featureNames[i] << endl;
00137 }
00138 os << endl;
00139 return os;
00140 }
00141
00142 }
00143