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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
* FeatureStats.cpp
* mert - Minimum Error Rate Training
*
* Created by Nicola Bertoldi on 13/05/08.
*
*/
#include "FeatureStats.h"
#include <fstream>
#include <cmath>
#include <boost/functional/hash.hpp>
#include "Util.h"
using namespace std;
namespace {
const int kAvailableSize = 8;
} // namespace
SparseVector::name2id_t SparseVector::m_name_to_id;
SparseVector::id2name_t SparseVector::m_id_to_name;
FeatureStatsType SparseVector::get(const string& name) const {
name2id_t::const_iterator name2id_iter = m_name_to_id.find(name);
if (name2id_iter == m_name_to_id.end()) return 0;
size_t id = name2id_iter->second;
return get(id);
}
FeatureStatsType SparseVector::get(size_t id) const {
fvector_t::const_iterator fvector_iter = m_fvector.find(id);
if (fvector_iter == m_fvector.end()) return 0;
return fvector_iter->second;
}
void SparseVector::set(const string& name, FeatureStatsType value) {
name2id_t::const_iterator name2id_iter = m_name_to_id.find(name);
size_t id = 0;
if (name2id_iter == m_name_to_id.end()) {
id = m_id_to_name.size();
m_id_to_name.push_back(name);
m_name_to_id[name] = id;
} else {
id = name2id_iter->second;
}
m_fvector[id] = value;
}
void SparseVector::write(ostream& out, const string& sep) const {
for (fvector_t::const_iterator i = m_fvector.begin(); i != m_fvector.end(); ++i) {
if (abs(i->second) < 0.00001) continue;
string name = m_id_to_name[i->first];
out << name << sep << i->second << " ";
}
}
void SparseVector::clear() {
m_fvector.clear();
}
SparseVector& SparseVector::operator-=(const SparseVector& rhs) {
//All the elements that have values in *this
for (fvector_t::iterator i = m_fvector.begin(); i != m_fvector.end(); ++i) {
m_fvector[i->first] = i->second - rhs.get(i->first);
}
//Any elements in rhs, that have no value in *this
for (fvector_t::const_iterator i = rhs.m_fvector.begin();
i != rhs.m_fvector.end(); ++i) {
if (m_fvector.find(i->first) == m_fvector.end()) {
m_fvector[i->first] = -(i->second);
}
}
return *this;
}
SparseVector operator-(const SparseVector& lhs, const SparseVector& rhs) {
SparseVector res(lhs);
res -= rhs;
return res;
}
std::vector<std::size_t> SparseVector::feats() const {
std::vector<std::size_t> toRet;
for(fvector_t::const_iterator iter = m_fvector.begin();
iter!=m_fvector.end();
iter++) {
toRet.push_back(iter->first);
}
return toRet;
}
std::size_t SparseVector::encode(const std::string& name) {
name2id_t::const_iterator name2id_iter = m_name_to_id.find(name);
size_t id = 0;
if (name2id_iter == m_name_to_id.end()) {
id = m_id_to_name.size();
m_id_to_name.push_back(name);
m_name_to_id[name] = id;
} else {
id = name2id_iter->second;
}
return id;
}
std::string SparseVector::decode(std::size_t id) {
return m_id_to_name[id];
}
bool operator==(SparseVector const& item1, SparseVector const& item2) {
return item1.m_fvector==item2.m_fvector;
}
std::size_t hash_value(SparseVector const& item) {
boost::hash<SparseVector::fvector_t> hasher;
return hasher(item.m_fvector);
}
FeatureStats::FeatureStats()
: m_available_size(kAvailableSize), m_entries(0),
m_array(new FeatureStatsType[m_available_size]) {}
FeatureStats::FeatureStats(const size_t size)
: m_available_size(size), m_entries(size),
m_array(new FeatureStatsType[m_available_size])
{
memset(m_array, 0, GetArraySizeWithBytes());
}
FeatureStats::FeatureStats(string &theString)
: m_available_size(0), m_entries(0), m_array(NULL)
{
set(theString);
}
FeatureStats::~FeatureStats()
{
if (m_array) {
delete [] m_array;
m_array = NULL;
}
}
void FeatureStats::Copy(const FeatureStats &stats)
{
m_available_size = stats.available();
m_entries = stats.size();
m_array = new FeatureStatsType[m_available_size];
memcpy(m_array, stats.getArray(), GetArraySizeWithBytes());
m_map = stats.getSparse();
}
FeatureStats::FeatureStats(const FeatureStats &stats)
{
Copy(stats);
}
FeatureStats& FeatureStats::operator=(const FeatureStats &stats)
{
delete [] m_array;
Copy(stats);
return *this;
}
void FeatureStats::expand()
{
m_available_size *= 2;
featstats_t t_ = new FeatureStatsType[m_available_size];
memcpy(t_, m_array, GetArraySizeWithBytes());
delete [] m_array;
m_array = t_;
}
void FeatureStats::add(FeatureStatsType v)
{
if (isfull()) expand();
m_array[m_entries++]=v;
}
void FeatureStats::addSparse(const string& name, FeatureStatsType v)
{
m_map.set(name,v);
}
void FeatureStats::set(string &theString)
{
string substring, stringBuf;
reset();
while (!theString.empty()) {
getNextPound(theString, substring);
// regular feature
if (substring.find(":") == string::npos) {
add(ConvertStringToFeatureStatsType(substring));
}
// sparse feature
else {
size_t separator = substring.find_last_of(":");
addSparse(substring.substr(0,separator), atof(substring.substr(separator+1).c_str()) );
}
}
}
void FeatureStats::loadbin(istream* is)
{
is->read(reinterpret_cast<char*>(m_array),
static_cast<streamsize>(GetArraySizeWithBytes()));
}
void FeatureStats::loadtxt(istream* is)
{
string line;
getline(*is, line);
set(line);
}
void FeatureStats::loadtxt(const string &file)
{
ifstream ifs(file.c_str(), ios::in);
if (!ifs) {
cerr << "Failed to open " << file << endl;
exit(1);
}
istream* is = &ifs;
loadtxt(is);
}
void FeatureStats::savetxt(const string &file)
{
ofstream ofs(file.c_str(), ios::out);
ostream* os = &ofs;
savetxt(os);
}
void FeatureStats::savetxt(ostream* os)
{
*os << *this;
}
void FeatureStats::savetxt() {
savetxt(&cout);
}
void FeatureStats::savebin(ostream* os)
{
os->write(reinterpret_cast<char*>(m_array),
static_cast<streamsize>(GetArraySizeWithBytes()));
}
ostream& operator<<(ostream& o, const FeatureStats& e)
{
// print regular features
for (size_t i=0; i< e.size(); i++) {
o << e.get(i) << " ";
}
// sparse features
e.getSparse().write(o,"");
return o;
}
//ADEED_BY_TS
bool operator==(const FeatureStats& f1, const FeatureStats& f2) {
size_t size = f1.size();
if (size != f2.size())
return false;
for (size_t k=0; k < size; k++) {
if (f1.get(k) != f2.get(k))
return false;
}
return true;
}
//END_ADDED
|