2008-05-14 12:14:13 +04:00
|
|
|
/*
|
|
|
|
* FeatureStats.cpp
|
2012-02-20 03:29:53 +04:00
|
|
|
* mert - Minimum Error Rate Training
|
2008-05-14 12:14:13 +04:00
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-11-14 10:15:30 +04:00
|
|
|
#include "Util.h"
|
2008-05-14 12:14:13 +04:00
|
|
|
#include "ScoreStats.h"
|
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
namespace {
|
|
|
|
const int kAvailableSize = 8;
|
|
|
|
} // namespace
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2008-05-16 00:32:37 +04:00
|
|
|
ScoreStats::ScoreStats()
|
2012-03-10 12:12:34 +04:00
|
|
|
: m_available_size(kAvailableSize), m_entries(0),
|
|
|
|
m_array(new ScoreStatsType[m_available_size]) {}
|
2011-11-12 13:12:07 +04:00
|
|
|
|
|
|
|
ScoreStats::ScoreStats(const size_t size)
|
2012-03-10 12:12:34 +04:00
|
|
|
: m_available_size(size), m_entries(size),
|
|
|
|
m_array(new ScoreStatsType[m_available_size])
|
2008-06-05 21:03:54 +04:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
memset(m_array, 0, GetArraySizeWithBytes());
|
2011-11-12 13:12:07 +04:00
|
|
|
}
|
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
ScoreStats::~ScoreStats()
|
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
if (m_array) {
|
|
|
|
delete [] m_array;
|
|
|
|
m_array = NULL;
|
2011-11-12 17:04:22 +04:00
|
|
|
}
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
void ScoreStats::Copy(const ScoreStats &stats)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
m_available_size = stats.available();
|
|
|
|
m_entries = stats.size();
|
|
|
|
m_array = new ScoreStatsType[m_available_size];
|
|
|
|
memcpy(m_array, stats.getArray(), GetArraySizeWithBytes());
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
ScoreStats::ScoreStats(const ScoreStats &stats)
|
2008-06-05 21:03:54 +04:00
|
|
|
{
|
2011-11-12 13:12:07 +04:00
|
|
|
Copy(stats);
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-06-05 21:03:54 +04:00
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
ScoreStats& ScoreStats::operator=(const ScoreStats &stats)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
delete [] m_array;
|
2011-11-12 13:12:07 +04:00
|
|
|
Copy(stats);
|
|
|
|
return *this;
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
void ScoreStats::expand()
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
m_available_size *= 2;
|
|
|
|
scorestats_t buf = new ScoreStatsType[m_available_size];
|
|
|
|
memcpy(buf, m_array, GetArraySizeWithBytes());
|
|
|
|
delete [] m_array;
|
|
|
|
m_array = buf;
|
2008-06-05 21:03:54 +04:00
|
|
|
}
|
2008-05-23 15:48:16 +04:00
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
void ScoreStats::add(ScoreStatsType v)
|
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
if (isfull()) expand();
|
2012-03-10 12:12:34 +04:00
|
|
|
m_array[m_entries++]=v;
|
2008-06-05 21:03:54 +04:00
|
|
|
}
|
|
|
|
|
2012-02-26 08:44:47 +04:00
|
|
|
void ScoreStats::set(const std::string& str)
|
2008-06-05 21:03:54 +04:00
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
reset();
|
2012-02-26 08:44:47 +04:00
|
|
|
vector<string> out;
|
|
|
|
Tokenize(str.c_str(), ' ', &out);
|
|
|
|
for (vector<string>::const_iterator it = out.begin();
|
|
|
|
it != out.end(); ++it) {
|
|
|
|
add(ConvertStringToScoreStatsType(*it));
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
void ScoreStats::loadbin(std::ifstream& inFile)
|
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
inFile.read((char*)m_array, GetArraySizeWithBytes());
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2008-06-05 21:03:54 +04:00
|
|
|
|
2008-05-14 12:14:13 +04:00
|
|
|
void ScoreStats::loadtxt(std::ifstream& inFile)
|
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
std::string theString;
|
2011-02-24 15:42:19 +03:00
|
|
|
std::getline(inFile, theString);
|
|
|
|
set(theString);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScoreStats::loadtxt(const std::string &file)
|
|
|
|
{
|
2011-11-12 04:24:19 +04:00
|
|
|
// TRACE_ERR("loading the stats from " << file << std::endl);
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
std::ifstream inFile(file.c_str(), std::ios::in); // matches a stream with a file. Opens the file
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
loadtxt(inFile);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ScoreStats::savetxt(const std::string &file)
|
|
|
|
{
|
2011-11-12 04:24:19 +04:00
|
|
|
// TRACE_ERR("saving the stats into " << file << std::endl);
|
2008-05-14 12:14:13 +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 12:14:13 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
savetxt(outFile);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ScoreStats::savetxt(std::ofstream& outFile)
|
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
outFile << *this;
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
void ScoreStats::savebin(std::ofstream& outFile)
|
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
outFile.write((char*)m_array, GetArraySizeWithBytes());
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2008-06-05 21:03:54 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
ostream& operator<<(ostream& o, const ScoreStats& e)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i< e.size(); i++)
|
|
|
|
o << e.get(i) << " ";
|
|
|
|
return o;
|
2008-05-27 20:50:52 +04:00
|
|
|
}
|
2011-12-12 17:48:42 +04:00
|
|
|
|
|
|
|
//ADDED_BY_TS
|
|
|
|
bool operator==(const ScoreStats& s1, const ScoreStats& s2) {
|
|
|
|
size_t size = s1.size();
|
|
|
|
|
|
|
|
if (size != s2.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t k=0; k < size; k++) {
|
|
|
|
if (s1.get(k) != s2.get(k))
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-26 08:44:47 +04:00
|
|
|
|
2011-12-12 17:48:42 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//END_ADDED
|