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"
|
2012-05-10 02:51:05 +04:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
namespace
|
|
|
|
{
|
2011-11-12 13:12:07 +04:00
|
|
|
const int kAvailableSize = 8;
|
|
|
|
} // namespace
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
|
2008-05-16 00:32:37 +04:00
|
|
|
ScoreStats::ScoreStats()
|
2013-05-29 21:16:15 +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)
|
2013-05-29 21:16:15 +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()
|
|
|
|
{
|
2014-02-26 14:43:37 +04:00
|
|
|
delete [] m_array;
|
|
|
|
m_array = NULL;
|
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-03-10 14:04:43 +04:00
|
|
|
void ScoreStats::set(const 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
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void ScoreStats::loadbin(istream* is)
|
2008-06-05 21:03:54 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
is->read(reinterpret_cast<char*>(m_array),
|
|
|
|
static_cast<streamsize>(GetArraySizeWithBytes()));
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2008-06-05 21:03:54 +04:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void ScoreStats::loadtxt(istream* is)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
string line;
|
|
|
|
getline(*is, line);
|
|
|
|
set(line);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void ScoreStats::loadtxt(const string &file)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
ifstream ifs(file.c_str(), ios::in); // matches a stream with a file. Opens the file
|
|
|
|
if (!ifs) {
|
|
|
|
cerr << "Failed to open " << file << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
istream* is = &ifs;
|
|
|
|
loadtxt(is);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void ScoreStats::savetxt(const string &file)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
ofstream ofs(file.c_str(), ios::out); // matches a stream with a file. Opens the file
|
|
|
|
ostream* os = &ofs;
|
|
|
|
savetxt(os);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void ScoreStats::savetxt(ostream* os)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
*os << *this;
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void ScoreStats::savetxt()
|
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
savetxt(&cout);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void ScoreStats::savebin(ostream* os)
|
2008-06-05 21:03:54 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
os->write(reinterpret_cast<char*>(m_array),
|
|
|
|
static_cast<streamsize>(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
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
bool operator==(const ScoreStats& s1, const ScoreStats& s2)
|
|
|
|
{
|
2011-12-12 17:48:42 +04:00
|
|
|
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;
|
|
|
|
}
|
2012-06-30 23:23:45 +04:00
|
|
|
|
2014-02-26 14:43:37 +04:00
|
|
|
}
|