2008-05-14 12:14:13 +04:00
|
|
|
/*
|
|
|
|
* ScoreData.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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ScoreData.h"
|
2012-03-10 14:04:43 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
#include <iostream>
|
2012-03-10 14:04:43 +04:00
|
|
|
#include <fstream>
|
2008-05-14 20:31:22 +04:00
|
|
|
#include "Scorer.h"
|
2008-05-14 12:14:13 +04:00
|
|
|
#include "Util.h"
|
2011-11-12 06:44:39 +04:00
|
|
|
#include "FileStream.h"
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
using namespace std;
|
|
|
|
|
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
|
|
|
|
2012-03-10 12:28:38 +04:00
|
|
|
ScoreData::ScoreData(Scorer* scorer) :
|
|
|
|
m_scorer(scorer)
|
2008-05-14 14:51:42 +04:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
m_score_type = m_scorer->getName();
|
2011-11-12 03:58:23 +04:00
|
|
|
// This is not dangerous: we don't use the this pointer in SetScoreData.
|
2012-03-10 12:12:34 +04:00
|
|
|
m_scorer->setScoreData(this);
|
|
|
|
m_num_scores = m_scorer->NumberOfScores();
|
|
|
|
// TRACE_ERR("ScoreData: m_num_scores: " << m_num_scores << std::endl);
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void ScoreData::save(ostream* os, bool bin)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
for (scoredata_t::iterator i = m_array.begin();
|
|
|
|
i != m_array.end(); ++i) {
|
|
|
|
i->save(os, m_score_type, bin);
|
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 ScoreData::save(const string &file, bool bin)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
if (file.empty()) return;
|
2012-03-10 14:04:43 +04:00
|
|
|
TRACE_ERR("saving the array into " << file << endl);
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
// matches a stream with a file. Opens the file.
|
2012-03-10 14:04:43 +04:00
|
|
|
ofstream ofs(file.c_str(), ios::out);
|
|
|
|
ostream* os = &ofs;
|
|
|
|
save(os, bin);
|
|
|
|
ofs.close();
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void ScoreData::save(bool bin)
|
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
save(&cout, bin);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void ScoreData::load(istream* is)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
ScoreArray entry;
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
while (!is->eof()) {
|
|
|
|
if (!is->good()) {
|
|
|
|
cerr << "ERROR ScoreData::load inFile.good()" << endl;
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
|
|
|
entry.clear();
|
2012-03-10 14:04:43 +04:00
|
|
|
entry.load(is);
|
2011-02-24 15:42:19 +03:00
|
|
|
if (entry.size() == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
add(entry);
|
|
|
|
}
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 14:04:43 +04:00
|
|
|
void ScoreData::load(const string &file)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2012-03-10 14:04:43 +04:00
|
|
|
TRACE_ERR("loading score data from " << file << endl);
|
|
|
|
inputfilestream input_stream(file); // matches a stream with a file. Opens the file
|
|
|
|
if (!input_stream) {
|
2011-02-24 15:42:19 +03:00
|
|
|
throw runtime_error("Unable to open score file: " + file);
|
|
|
|
}
|
2012-03-10 14:04:43 +04:00
|
|
|
istream* is = &input_stream;
|
|
|
|
load(is);
|
|
|
|
input_stream.close();
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
void ScoreData::add(ScoreArray& e)
|
|
|
|
{
|
|
|
|
if (exists(e.getIndex())) { // array at position e.getIndex() already exists
|
|
|
|
//enlarge array at position e.getIndex()
|
|
|
|
size_t pos = getIndex(e.getIndex());
|
2012-03-10 12:12:34 +04:00
|
|
|
m_array.at(pos).merge(e);
|
2011-02-24 15:42:19 +03:00
|
|
|
} else {
|
2012-03-10 12:12:34 +04:00
|
|
|
m_array.push_back(e);
|
2011-02-24 15:42:19 +03:00
|
|
|
setIndex();
|
|
|
|
}
|
2008-05-27 20:50:52 +04:00
|
|
|
}
|
|
|
|
|
2012-12-06 20:39:22 +04:00
|
|
|
void ScoreData::add(const ScoreStats& e, int sent_idx)
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
|
|
|
if (exists(sent_idx)) { // array at position e.getIndex() already exists
|
2011-11-12 03:58:23 +04:00
|
|
|
// Enlarge array at position e.getIndex()
|
2011-02-24 15:42:19 +03:00
|
|
|
size_t pos = getIndex(sent_idx);
|
2011-11-12 04:24:19 +04:00
|
|
|
// TRACE_ERR("Inserting in array " << sent_idx << std::endl);
|
2012-03-10 12:12:34 +04:00
|
|
|
m_array.at(pos).add(e);
|
2011-11-12 04:24:19 +04:00
|
|
|
// TRACE_ERR("size: " << size() << " -> " << a.size() << std::endl);
|
2011-02-24 15:42:19 +03:00
|
|
|
} else {
|
2011-11-12 04:24:19 +04:00
|
|
|
// TRACE_ERR("Creating a new entry in the array" << std::endl);
|
2011-02-24 15:42:19 +03:00
|
|
|
ScoreArray a;
|
2012-03-10 12:12:34 +04:00
|
|
|
a.NumberOfScores(m_num_scores);
|
2011-02-24 15:42:19 +03:00
|
|
|
a.add(e);
|
|
|
|
a.setIndex(sent_idx);
|
2012-03-10 12:12:34 +04:00
|
|
|
size_t idx = m_array.size();
|
|
|
|
m_array.push_back(a);
|
|
|
|
m_index_to_array_name[idx] = sent_idx;
|
|
|
|
m_array_name_to_index[sent_idx]=idx;
|
2011-11-12 04:24:19 +04:00
|
|
|
// TRACE_ERR("size: " << size() << " -> " << a.size() << std::endl);
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
|
|
|
}
|
2008-05-27 20:50:52 +04:00
|
|
|
|
2011-11-12 06:26:13 +04:00
|
|
|
bool ScoreData::check_consistency() const
|
2008-05-27 20:50:52 +04:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
if (m_array.size() == 0)
|
2011-02-24 15:42:19 +03:00
|
|
|
return true;
|
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
for (scoredata_t::const_iterator i = m_array.begin(); i != m_array.end(); ++i)
|
2011-02-24 15:42:19 +03:00
|
|
|
if (!i->check_consistency()) return false;
|
|
|
|
|
|
|
|
return true;
|
2008-05-27 20:50:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScoreData::setIndex()
|
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
size_t j = 0;
|
|
|
|
for (scoredata_t::iterator i = m_array.begin(); i != m_array.end(); ++i) {
|
|
|
|
m_index_to_array_name[j] = i->getIndex();
|
|
|
|
m_array_name_to_index[i->getIndex()]=j;
|
2011-02-24 15:42:19 +03:00
|
|
|
j++;
|
|
|
|
}
|
2008-06-24 23:27:18 +04:00
|
|
|
}
|
2012-06-30 23:23:45 +04:00
|
|
|
|
|
|
|
}
|