File size: 3,492 Bytes
158b61b |
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 |
/*
* FeatureArray.cpp
* mert - Minimum Error Rate Training
*
* Created by Nicola Bertoldi on 13/05/08.
*
*/
#include <iostream>
#include <fstream>
#include "FeatureArray.h"
#include "FileStream.h"
#include "Util.h"
using namespace std;
namespace MosesTuning
{
FeatureArray::FeatureArray()
: m_index(0), m_num_features(0) {}
FeatureArray::~FeatureArray() {}
void FeatureArray::savetxt(ostream* os)
{
*os << FEATURES_TXT_BEGIN << " " << m_index << " " << m_array.size()
<< " " << m_num_features << " " << m_features << endl;
for (featarray_t::iterator i = m_array.begin(); i != m_array.end(); ++i) {
i->savetxt(os);
*os << endl;
}
*os << FEATURES_TXT_END << endl;
}
void FeatureArray::savebin(ostream* os)
{
*os << FEATURES_BIN_BEGIN << " " << m_index << " " << m_array.size()
<< " " << m_num_features << " " << m_features << endl;
for (featarray_t::iterator i = m_array.begin(); i != m_array.end(); ++i)
i->savebin(os);
*os << FEATURES_BIN_END << endl;
}
void FeatureArray::save(ostream* os, bool bin)
{
if (size() <= 0) return;
if (bin) {
savebin(os);
} else {
savetxt(os);
}
}
void FeatureArray::save(const string &file, bool bin)
{
ofstream ofs(file.c_str(), ios::out);
if (!ofs) {
cerr << "Failed to open " << file << endl;
exit(1);
}
ostream *os = &ofs;
save(os, bin);
ofs.close();
}
void FeatureArray::save(bool bin)
{
save(&cout, bin);
}
void FeatureArray::loadbin(istream* is, size_t n)
{
FeatureStats entry(m_num_features);
for (size_t i = 0 ; i < n; i++) {
entry.loadbin(is);
add(entry);
}
}
void FeatureArray::loadtxt(istream* is, const SparseVector& sparseWeights, size_t n)
{
FeatureStats entry(m_num_features);
for (size_t i=0 ; i < n; i++) {
entry.loadtxt(is, sparseWeights);
add(entry);
}
}
void FeatureArray::load(istream* is, const SparseVector& sparseWeights)
{
size_t number_of_entries = 0;
bool binmode = false;
string substring, stringBuf;
string::size_type loc;
getline(*is, stringBuf);
if (!is->good()) {
return;
}
if (!stringBuf.empty()) {
if ((loc = stringBuf.find(FEATURES_TXT_BEGIN)) == 0) {
binmode = false;
} else if ((loc = stringBuf.find(FEATURES_BIN_BEGIN)) == 0) {
binmode = true;
} else {
TRACE_ERR("ERROR: FeatureArray::load(): Wrong header");
return;
}
getNextPound(stringBuf, substring);
getNextPound(stringBuf, substring);
m_index = atoi(substring.c_str());
getNextPound(stringBuf, substring);
number_of_entries = atoi(substring.c_str());
getNextPound(stringBuf, substring);
m_num_features = atoi(substring.c_str());
m_features = stringBuf;
}
if (binmode) {
loadbin(is, number_of_entries);
} else {
loadtxt(is, sparseWeights, number_of_entries);
}
getline(*is, stringBuf);
if (!stringBuf.empty()) {
if ((loc = stringBuf.find(FEATURES_TXT_END)) != 0 &&
(loc = stringBuf.find(FEATURES_BIN_END)) != 0) {
TRACE_ERR("ERROR: FeatureArray::load(): Wrong footer");
return;
}
}
}
void FeatureArray::merge(FeatureArray& e)
{
//dummy implementation
for (size_t i = 0; i < e.size(); i++)
add(e.get(i));
}
bool FeatureArray::check_consistency() const
{
const size_t sz = NumberOfFeatures();
if (sz == 0)
return true;
for (featarray_t::const_iterator i = m_array.begin(); i != m_array.end(); i++) {
if (i->size() != sz)
return false;
}
return true;
}
}
|