2008-05-14 11:57:45 +04:00
|
|
|
/*
|
|
|
|
* FeatureStats.cpp
|
|
|
|
* met - Minimum Error Training
|
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include "FeatureStats.h"
|
|
|
|
|
2008-06-05 11:23:34 +04:00
|
|
|
#define AVAILABLE_ 8;
|
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-05-16 00:32:37 +04:00
|
|
|
FeatureStats::FeatureStats()
|
2008-06-05 11:23:34 +04:00
|
|
|
{
|
|
|
|
available_ = AVAILABLE_;
|
|
|
|
entries_ = 0;
|
2008-06-05 15:42:00 +04:00
|
|
|
array_ = new FeatureStatsType[available_];
|
2008-06-05 11:23:34 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
FeatureStats::~FeatureStats()
|
|
|
|
{
|
2008-06-05 15:42:00 +04:00
|
|
|
delete array_;
|
2008-06-05 11:23:34 +04:00
|
|
|
};
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-06-05 11:23:34 +04:00
|
|
|
FeatureStats::FeatureStats(const FeatureStats &stats)
|
|
|
|
{
|
|
|
|
available_ = stats.available();
|
|
|
|
entries_ = stats.size();
|
2008-06-05 15:42:00 +04:00
|
|
|
array_ = new FeatureStatsType[available_];
|
2008-06-05 21:03:54 +04:00
|
|
|
memcpy(array_,stats.getArray(),featbytes_);
|
2008-06-05 11:23:34 +04:00
|
|
|
};
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-05-16 00:32:37 +04:00
|
|
|
FeatureStats::FeatureStats(const size_t size)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2008-06-05 11:23:34 +04:00
|
|
|
available_ = size;
|
|
|
|
entries_ = size;
|
2008-06-05 15:42:00 +04:00
|
|
|
array_ = new FeatureStatsType[available_];
|
2008-06-05 21:03:54 +04:00
|
|
|
memset(array_,0,featbytes_);
|
2008-05-14 11:57:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
FeatureStats::FeatureStats(std::string &theString)
|
|
|
|
{
|
|
|
|
set(theString);
|
|
|
|
}
|
|
|
|
|
2008-06-05 11:23:34 +04:00
|
|
|
void FeatureStats::expand()
|
|
|
|
{
|
|
|
|
available_*=2;
|
|
|
|
featstats_t t_ = new FeatureStatsType[available_];
|
2008-06-05 21:03:54 +04:00
|
|
|
memcpy(t_,array_,featbytes_);
|
2008-06-05 15:42:00 +04:00
|
|
|
delete array_;
|
|
|
|
array_=t_;
|
2008-06-05 11:23:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureStats::add(FeatureStatsType v)
|
|
|
|
{
|
|
|
|
if (isfull()) expand();
|
2008-06-05 15:42:00 +04:00
|
|
|
array_[entries_++]=v;
|
2008-06-05 11:23:34 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
void FeatureStats::set(std::string &theString)
|
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
std::string substring, stringBuf;
|
2008-06-05 11:23:34 +04:00
|
|
|
reset();
|
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
while (!theString.empty()){
|
2008-06-05 11:23:34 +04:00
|
|
|
getNextPound(theString, substring);
|
|
|
|
add(ATOFST(substring.c_str()));
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-05 11:23:34 +04:00
|
|
|
|
|
|
|
void FeatureStats::loadbin(std::ifstream& inFile)
|
|
|
|
{
|
2008-06-05 21:03:54 +04:00
|
|
|
inFile.read((char*) array_, featbytes_);
|
2008-06-05 11:23:34 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
void FeatureStats::loadtxt(std::ifstream& inFile)
|
|
|
|
{
|
2008-06-05 11:23:34 +04:00
|
|
|
std::string theString;
|
2008-05-14 11:57:45 +04:00
|
|
|
std::getline(inFile, theString);
|
|
|
|
set(theString);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureStats::loadtxt(const std::string &file)
|
|
|
|
{
|
2008-06-05 11:23:34 +04:00
|
|
|
// TRACE_ERR("loading the stats from " << file << std::endl);
|
2008-05-14 11:57:45 +04:00
|
|
|
|
|
|
|
std::ifstream inFile(file.c_str(), std::ios::in); // matches a stream with a file. Opens the file
|
|
|
|
|
|
|
|
loadtxt(inFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FeatureStats::savetxt(const std::string &file)
|
|
|
|
{
|
|
|
|
// TRACE_ERR("saving the stats into " << file << std::endl);
|
|
|
|
|
|
|
|
std::ofstream outFile(file.c_str(), std::ios::out); // matches a stream with a file. Opens the file
|
|
|
|
|
|
|
|
savetxt(outFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FeatureStats::savetxt(std::ofstream& outFile)
|
|
|
|
{
|
2008-06-05 11:23:34 +04:00
|
|
|
// TRACE_ERR("saving the stats" << std::endl);
|
2008-06-04 12:33:37 +04:00
|
|
|
outFile << *this;
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-06-03 12:56:37 +04:00
|
|
|
void FeatureStats::savebin(std::ofstream& outFile)
|
|
|
|
{
|
2008-06-05 21:03:54 +04:00
|
|
|
outFile.write((char*) array_, featbytes_);
|
2008-06-03 12:56:37 +04:00
|
|
|
}
|
2008-05-14 11:57:45 +04:00
|
|
|
|
|
|
|
FeatureStats& FeatureStats::operator=(const FeatureStats &stats)
|
|
|
|
{
|
2008-06-05 15:42:00 +04:00
|
|
|
delete array_;
|
2008-06-05 11:23:34 +04:00
|
|
|
available_ = stats.available();
|
|
|
|
entries_ = stats.size();
|
2008-06-05 15:42:00 +04:00
|
|
|
array_ = new FeatureStatsType[available_];
|
2008-06-05 21:03:54 +04:00
|
|
|
memcpy(array_,stats.getArray(),featbytes_);
|
2008-05-14 11:57:45 +04:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
/**write the whole object to a stream*/
|
|
|
|
ostream& operator<<(ostream& o, const FeatureStats& e){
|
2008-06-05 11:23:34 +04:00
|
|
|
for (size_t i=0; i< e.size(); i++)
|
|
|
|
o << e.get(i) << " ";
|
2008-05-27 20:50:52 +04:00
|
|
|
return o;
|
|
|
|
}
|