2008-05-14 11:57:45 +04:00
|
|
|
/*
|
|
|
|
* FeatureArray.cpp
|
2012-02-20 03:29:53 +04:00
|
|
|
* mert - Minimum Error Rate Training
|
2008-05-14 11:57:45 +04:00
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
#include <iostream>
|
2012-03-10 14:04:43 +04:00
|
|
|
#include <fstream>
|
2008-05-14 11:57:45 +04:00
|
|
|
#include "FeatureArray.h"
|
2011-11-12 06:44:39 +04:00
|
|
|
#include "FileStream.h"
|
2008-05-14 11:57:45 +04:00
|
|
|
#include "Util.h"
|
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
using namespace std;
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2012-12-06 20:39:22 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
|
2011-11-12 13:17:31 +04:00
|
|
|
FeatureArray::FeatureArray()
|
2013-05-29 21:16:15 +04:00
|
|
|
: m_index(0), m_num_features(0) {}
|
2011-11-12 13:17:31 +04:00
|
|
|
|
|
|
|
FeatureArray::~FeatureArray() {}
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void FeatureArray::savetxt(ostream* os)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
*os << FEATURES_TXT_BEGIN << " " << m_index << " " << m_array.size()
|
2013-05-29 21:16:15 +04:00
|
|
|
<< " " << m_num_features << " " << m_features << endl;
|
2012-03-10 12:12:34 +04:00
|
|
|
for (featarray_t::iterator i = m_array.begin(); i != m_array.end(); ++i) {
|
2012-03-10 14:04:43 +04:00
|
|
|
i->savetxt(os);
|
|
|
|
*os << endl;
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2012-03-10 14:04:43 +04:00
|
|
|
*os << FEATURES_TXT_END << endl;
|
2008-05-14 15:05:32 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void FeatureArray::savebin(ostream* os)
|
2008-05-14 15:05:32 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
*os << FEATURES_BIN_BEGIN << " " << m_index << " " << m_array.size()
|
2013-05-29 21:16:15 +04:00
|
|
|
<< " " << m_num_features << " " << m_features << endl;
|
2012-03-10 12:12:34 +04:00
|
|
|
for (featarray_t::iterator i = m_array.begin(); i != m_array.end(); ++i)
|
2012-03-10 14:04:43 +04:00
|
|
|
i->savebin(os);
|
2008-06-03 12:56:37 +04:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
*os << FEATURES_BIN_END << endl;
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 15:28:54 +04:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void FeatureArray::save(ostream* os, bool bin)
|
2008-05-14 15:28:54 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
if (size() <= 0) return;
|
|
|
|
if (bin) {
|
|
|
|
savebin(os);
|
|
|
|
} else {
|
|
|
|
savetxt(os);
|
|
|
|
}
|
2008-05-14 15:28:54 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void FeatureArray::save(const string &file, bool bin)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
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();
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void FeatureArray::save(bool bin)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
save(&cout, bin);
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
2008-06-05 11:23:34 +04:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void FeatureArray::loadbin(istream* is, size_t n)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
FeatureStats entry(m_num_features);
|
2012-03-10 14:04:43 +04:00
|
|
|
for (size_t i = 0 ; i < n; i++) {
|
|
|
|
entry.loadbin(is);
|
2011-02-24 15:42:19 +03:00
|
|
|
add(entry);
|
|
|
|
}
|
2008-06-05 11:23:34 +04:00
|
|
|
}
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2012-05-25 00:11:35 +04:00
|
|
|
void FeatureArray::loadtxt(istream* is, const SparseVector& sparseWeights, size_t n)
|
2008-06-05 11:23:34 +04:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
FeatureStats entry(m_num_features);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
|
|
|
for (size_t i=0 ; i < n; i++) {
|
2012-05-25 00:11:35 +04:00
|
|
|
entry.loadtxt(is, sparseWeights);
|
2011-02-24 15:42:19 +03:00
|
|
|
add(entry);
|
|
|
|
}
|
2008-06-05 11:23:34 +04:00
|
|
|
}
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2012-05-25 00:11:35 +04:00
|
|
|
void FeatureArray::load(istream* is, const SparseVector& sparseWeights)
|
2008-06-05 11:23:34 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
size_t number_of_entries = 0;
|
|
|
|
bool binmode = false;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
string substring, stringBuf;
|
|
|
|
string::size_type loc;
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
getline(*is, stringBuf);
|
|
|
|
if (!is->good()) {
|
2011-02-24 15:42:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stringBuf.empty()) {
|
|
|
|
if ((loc = stringBuf.find(FEATURES_TXT_BEGIN)) == 0) {
|
2012-03-10 14:04:43 +04:00
|
|
|
binmode = false;
|
2011-02-24 15:42:19 +03:00
|
|
|
} else if ((loc = stringBuf.find(FEATURES_BIN_BEGIN)) == 0) {
|
2012-03-10 14:04:43 +04:00
|
|
|
binmode = true;
|
2011-02-24 15:42:19 +03:00
|
|
|
} else {
|
|
|
|
TRACE_ERR("ERROR: FeatureArray::load(): Wrong header");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
getNextPound(stringBuf, substring);
|
|
|
|
getNextPound(stringBuf, substring);
|
2012-12-06 20:39:22 +04:00
|
|
|
m_index = atoi(substring.c_str());
|
2011-02-24 15:42:19 +03:00
|
|
|
getNextPound(stringBuf, substring);
|
2008-05-27 20:50:52 +04:00
|
|
|
number_of_entries = atoi(substring.c_str());
|
2011-02-24 15:42:19 +03:00
|
|
|
getNextPound(stringBuf, substring);
|
2012-03-10 12:12:34 +04:00
|
|
|
m_num_features = atoi(substring.c_str());
|
|
|
|
m_features = stringBuf;
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
if (binmode) {
|
|
|
|
loadbin(is, number_of_entries);
|
|
|
|
} else {
|
2012-05-25 00:11:35 +04:00
|
|
|
loadtxt(is, sparseWeights, number_of_entries);
|
2012-03-10 14:04:43 +04:00
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
getline(*is, stringBuf);
|
2011-02-24 15:42:19 +03:00
|
|
|
if (!stringBuf.empty()) {
|
2012-03-10 14:04:43 +04:00
|
|
|
if ((loc = stringBuf.find(FEATURES_TXT_END)) != 0 &&
|
|
|
|
(loc = stringBuf.find(FEATURES_BIN_END)) != 0) {
|
2011-02-24 15:42:19 +03:00
|
|
|
TRACE_ERR("ERROR: FeatureArray::load(): Wrong footer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 18:16:05 +04:00
|
|
|
void FeatureArray::merge(FeatureArray& e)
|
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
//dummy implementation
|
2012-03-10 14:04:43 +04:00
|
|
|
for (size_t i = 0; i < e.size(); i++)
|
2011-02-24 15:42:19 +03:00
|
|
|
add(e.get(i));
|
2008-05-14 18:16:05 +04:00
|
|
|
}
|
2008-05-27 20:50:52 +04:00
|
|
|
|
2011-11-12 06:26:13 +04:00
|
|
|
bool FeatureArray::check_consistency() const
|
2008-05-27 20:50:52 +04:00
|
|
|
{
|
2011-11-12 06:26:13 +04:00
|
|
|
const size_t sz = NumberOfFeatures();
|
2011-02-24 15:42:19 +03:00
|
|
|
if (sz == 0)
|
|
|
|
return true;
|
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
for (featarray_t::const_iterator i = m_array.begin(); i != m_array.end(); i++) {
|
2011-11-12 06:26:13 +04:00
|
|
|
if (i->size() != sz)
|
2011-02-24 15:42:19 +03:00
|
|
|
return false;
|
2011-11-12 06:26:13 +04:00
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
return true;
|
2008-05-27 20:50:52 +04:00
|
|
|
}
|
2012-06-30 23:23:45 +04:00
|
|
|
|
|
|
|
}
|