1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// $Id$
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <cassert>
#include "Util.h"
#include "StaticData.h"
#include "ScoreIndexManager.h"
#include "ScoreProducer.h"
#include "ScoreComponentCollection.h" // debugging
namespace Moses
{
using namespace std;
void ScoreIndexManager::AddScoreProducer(const ScoreProducer* sp)
{
// Producers must be inserted in the order they are created
const_cast<ScoreProducer*>(sp)->CreateScoreBookkeepingID();
assert(m_begins.size() == (sp->GetScoreBookkeepingID()));
m_producers.push_back(sp);
m_begins.push_back(m_last);
size_t numScoreCompsProduced = sp->GetNumScoreComponents();
assert(numScoreCompsProduced > 0);
m_last += numScoreCompsProduced;
m_ends.push_back(m_last);
/*VERBOSE(1,"Added ScoreProducer(" << sp->GetScoreBookkeepingID()
<< " " << sp->GetScoreProducerDescription()
<< ") index=" << m_begins.back() << "-" << m_ends.back()-1 << std::endl);
*/
}
void ScoreIndexManager::PrintLabeledScores(std::ostream& os, const ScoreComponentCollection& scores) const
{
std::vector<float> weights(scores.m_scores.size(), 1.0f);
PrintLabeledWeightedScores(os, scores, weights);
}
void ScoreIndexManager::PrintLabeledWeightedScores(std::ostream& os, const ScoreComponentCollection& scores, const std::vector<float>& weights) const
{
assert(m_featureShortNames.size() == weights.size());
string lastName = "";
for (size_t i = 0; i < m_featureShortNames.size(); ++i) {
if (i>0) {
os << " ";
}
if (lastName != m_featureShortNames[i]) {
os << m_featureShortNames[i] << ": ";
lastName = m_featureShortNames[i];
}
os << weights[i] * scores[i];
}
}
void ScoreIndexManager::InitFeatureNames()
{
m_featureNames.clear();
m_featureShortNames.clear();
size_t cur_i = 0;
size_t cur_scoreType = 0;
while (cur_i < m_last) {
size_t nis_idx = 0;
bool add_idx = (m_producers[cur_scoreType]->GetNumInputScores() > 1);
while (nis_idx < m_producers[cur_scoreType]->GetNumInputScores()) {
ostringstream os;
os << m_producers[cur_scoreType]->GetScoreProducerDescription();
if (add_idx)
os << '_' << (nis_idx+1);
m_featureNames.push_back(os.str());
nis_idx++;
cur_i++;
}
int ind = 1;
add_idx = (m_ends[cur_scoreType] - cur_i > 1);
while (cur_i < m_ends[cur_scoreType]) {
ostringstream os;
os << m_producers[cur_scoreType]->GetScoreProducerDescription();
if (add_idx)
os << '_' << ind;
m_featureNames.push_back(os.str());
m_featureShortNames.push_back( m_producers[cur_scoreType]->GetScoreProducerWeightShortName() );
++cur_i;
++ind;
}
cur_scoreType++;
}
}
#ifdef HAVE_PROTOBUF
void ScoreIndexManager::SerializeFeatureNamesToPB(hgmert::Hypergraph* hg) const
{
for (size_t i = 0; i < m_featureNames.size(); ++i) {
hg->add_feature_names(m_featureNames[i]);
}
}
#endif
void ScoreIndexManager::InitWeightVectorFromFile(const std::string& fnam, vector<float>* m_allWeights) const
{
assert(m_allWeights->size() == m_featureNames.size());
ifstream in(fnam.c_str());
assert(in.good());
char buf[2000];
map<string, double> name2val;
while (!in.eof()) {
in.getline(buf, 2000);
if (strlen(buf) == 0) continue;
if (buf[0] == '#') continue;
istringstream is(buf);
string fname;
double val;
is >> fname >> val;
map<string, double>::iterator i = name2val.find(fname);
assert(i == name2val.end()); // duplicate weight name
name2val[fname] = val;
}
assert(m_allWeights->size() == m_featureNames.size());
for (size_t i = 0; i < m_featureNames.size(); ++i) {
map<string, double>::iterator iter = name2val.find(m_featureNames[i]);
if (iter == name2val.end()) {
cerr << "No weight found found for feature: " << m_featureNames[i] << endl;
abort();
}
(*m_allWeights)[i] = iter->second;
}
}
std::ostream& operator<<(std::ostream& os, const ScoreIndexManager& sim)
{
for (size_t i = 0; i < sim.m_featureNames.size(); ++i) {
os << sim.m_featureNames[i] << endl;
}
os << endl;
return os;
}
}
|