2008-05-14 11:57:45 +04:00
|
|
|
/*
|
|
|
|
* FeatureArray.cpp
|
|
|
|
* met - Minimum Error Training
|
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
|
2011-11-12 13:17:31 +04:00
|
|
|
FeatureArray::FeatureArray()
|
|
|
|
: idx(""), number_of_features(0), _sparse_flag(false) {}
|
|
|
|
|
|
|
|
FeatureArray::~FeatureArray() {}
|
2008-05-14 11:57:45 +04:00
|
|
|
|
|
|
|
void FeatureArray::savetxt(std::ofstream& outFile)
|
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
outFile << FEATURES_TXT_BEGIN << " " << idx << " " << array_.size()
|
|
|
|
<< " " << number_of_features << " " << features << std::endl;
|
|
|
|
for (featarray_t::iterator i = array_.begin(); i !=array_.end(); i++) {
|
|
|
|
i->savetxt(outFile);
|
|
|
|
outFile << std::endl;
|
|
|
|
}
|
|
|
|
outFile << FEATURES_TXT_END << std::endl;
|
2008-05-14 15:05:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureArray::savebin(std::ofstream& outFile)
|
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
outFile << FEATURES_BIN_BEGIN << " " << idx << " " << array_.size()
|
|
|
|
<< " " << number_of_features << " " << features << std::endl;
|
2008-06-05 11:23:34 +04:00
|
|
|
for (featarray_t::iterator i = array_.begin(); i !=array_.end(); i++)
|
2011-02-24 15:42:19 +03:00
|
|
|
i->savebin(outFile);
|
2008-06-03 12:56:37 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
outFile << FEATURES_BIN_END << std::endl;
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 15:28:54 +04:00
|
|
|
|
|
|
|
void FeatureArray::save(std::ofstream& inFile, bool bin)
|
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
if (size()>0)
|
|
|
|
(bin)?savebin(inFile):savetxt(inFile);
|
2008-05-14 15:28:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureArray::save(const std::string &file, bool bin)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
std::ofstream outFile(file.c_str(), std::ios::out); // matches a stream with a file. Opens the file
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
save(outFile);
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
outFile.close();
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-06-05 11:23:34 +04:00
|
|
|
void FeatureArray::loadbin(ifstream& inFile, size_t n)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
FeatureStats entry(number_of_features);
|
2008-06-05 11:23:34 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
for (size_t i=0 ; i < n; i++) {
|
|
|
|
entry.loadbin(inFile);
|
|
|
|
add(entry);
|
|
|
|
}
|
2008-06-05 11:23:34 +04:00
|
|
|
}
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-06-05 11:23:34 +04:00
|
|
|
void FeatureArray::loadtxt(ifstream& inFile, size_t n)
|
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
FeatureStats entry(number_of_features);
|
|
|
|
|
|
|
|
for (size_t i=0 ; i < n; i++) {
|
|
|
|
entry.loadtxt(inFile);
|
|
|
|
add(entry);
|
2011-09-07 20:37:33 +04:00
|
|
|
if (entry.getSparse().size()>0)
|
|
|
|
_sparse_flag = true;
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2008-06-05 11:23:34 +04:00
|
|
|
}
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-06-05 11:23:34 +04:00
|
|
|
void FeatureArray::load(ifstream& inFile)
|
|
|
|
{
|
|
|
|
size_t number_of_entries=0;
|
2011-02-24 15:42:19 +03:00
|
|
|
bool binmode=false;
|
|
|
|
|
|
|
|
std::string substring, stringBuf;
|
2008-05-27 20:50:52 +04:00
|
|
|
std::string::size_type loc;
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
std::getline(inFile, stringBuf);
|
|
|
|
if (!inFile.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);
|
2008-05-27 20:50:52 +04:00
|
|
|
idx = substring;
|
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);
|
2008-06-05 11:23:34 +04:00
|
|
|
number_of_features = atoi(substring.c_str());
|
2011-02-24 15:42:19 +03:00
|
|
|
features = stringBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
(binmode)?loadbin(inFile, number_of_entries):loadtxt(inFile, number_of_entries);
|
|
|
|
|
|
|
|
std::getline(inFile, 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;
|
|
|
|
}
|
|
|
|
}
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-06-05 11:23:34 +04:00
|
|
|
void FeatureArray::load(const std::string &file)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
TRACE_ERR("loading data from " << file << std::endl);
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
inputfilestream inFile(file); // matches a stream with a file. Opens the file
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
load((ifstream&) inFile);
|
2008-05-14 15:28:54 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
inFile.close();
|
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
|
|
|
|
for (size_t i=0; i<e.size(); i++)
|
|
|
|
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;
|
|
|
|
|
2011-11-12 06:26:13 +04:00
|
|
|
for (featarray_t::const_iterator i = array_.begin(); i != array_.end(); i++) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|