2008-05-14 12:14:13 +04:00
|
|
|
/*
|
|
|
|
* ScoreData.cpp
|
|
|
|
* met - Minimum Error Training
|
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include "ScoreData.h"
|
2008-05-14 20:31:22 +04:00
|
|
|
#include "Scorer.h"
|
2008-05-14 12:14:13 +04:00
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
2008-05-14 14:51:42 +04:00
|
|
|
ScoreData::ScoreData(Scorer& ptr):
|
2008-05-16 00:32:37 +04:00
|
|
|
theScorer(&ptr)
|
2008-05-14 14:51:42 +04:00
|
|
|
{
|
2008-05-14 16:23:58 +04:00
|
|
|
score_type = theScorer->getName();
|
2008-05-15 12:35:56 +04:00
|
|
|
TRACE_ERR("score_type:" << score_type << std::endl);
|
2008-05-16 00:49:49 +04:00
|
|
|
theScorer->setScoreData(this);//this is not dangerous: we dont use the this pointer in SetScoreData
|
2008-05-14 14:51:42 +04:00
|
|
|
};
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2008-05-15 12:35:56 +04:00
|
|
|
void ScoreData::save(std::ofstream& outFile, bool bin)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
|
|
|
ScoreArray entry;
|
|
|
|
for (vector<ScoreArray>::iterator i = array_.begin(); i !=array_.end(); i++)
|
2008-05-15 12:35:56 +04:00
|
|
|
(*i).save(outFile);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2008-05-15 12:35:56 +04:00
|
|
|
void ScoreData::save(const std::string &file, bool bin)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2008-05-15 12:35:56 +04:00
|
|
|
if (file.empty()) return;
|
2008-05-14 12:14:13 +04:00
|
|
|
TRACE_ERR("saving the array into " << file << std::endl);
|
|
|
|
|
|
|
|
std::ofstream outFile(file.c_str(), std::ios::out); // matches a stream with a file. Opens the file
|
|
|
|
|
|
|
|
ScoreStats entry;
|
|
|
|
|
2008-05-15 12:35:56 +04:00
|
|
|
save(outFile, bin);
|
|
|
|
|
|
|
|
outFile.close();
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2008-05-15 12:35:56 +04:00
|
|
|
void ScoreData::load(ifstream& inFile)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
|
|
|
ScoreArray entry;
|
|
|
|
|
|
|
|
int iter=0;
|
|
|
|
while (!inFile.eof()){
|
|
|
|
TRACE_ERR("iter " << iter << " size " << size() << std::endl);
|
|
|
|
|
|
|
|
entry.clear();
|
|
|
|
entry.loadtxt(inFile);
|
|
|
|
|
|
|
|
if (entry.size() == 0){
|
|
|
|
TRACE_ERR("no more data" << std::endl);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(entry);
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-15 12:35:56 +04:00
|
|
|
void ScoreData::load(const std::string &file)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2008-05-15 18:13:32 +04:00
|
|
|
TRACE_ERR("loading score data from " << file << std::endl);
|
2008-05-14 12:14:13 +04:00
|
|
|
|
|
|
|
std::ifstream inFile(file.c_str(), std::ios::in); // matches a stream with a file. Opens the file
|
|
|
|
|
2008-05-15 18:13:32 +04:00
|
|
|
if (!inFile) {
|
|
|
|
throw runtime_error("Unable to open score file: " + file);
|
|
|
|
}
|
2008-05-15 12:35:56 +04:00
|
|
|
load(inFile);
|
2008-05-14 12:14:13 +04:00
|
|
|
|
|
|
|
inFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScoreData::loadnbest(const std::string &file)
|
|
|
|
{
|
|
|
|
TRACE_ERR("loading nbest from " << file << std::endl);
|
|
|
|
|
|
|
|
ScoreStats entry;
|
|
|
|
int sentence_index;
|
|
|
|
int nextPound;
|
|
|
|
|
|
|
|
std::ifstream inFile(file.c_str(), std::ios::in); // matches a stream with a file. Opens the file
|
|
|
|
|
|
|
|
|
|
|
|
while (!inFile.eof()){
|
|
|
|
|
|
|
|
std::string substring, subsubstring, stringBuf;
|
2008-05-16 00:32:37 +04:00
|
|
|
std::string theSentence;
|
2008-05-14 12:14:13 +04:00
|
|
|
std::string::size_type loc;
|
|
|
|
|
|
|
|
std::getline(inFile, stringBuf);
|
|
|
|
if (stringBuf.empty()) continue;
|
|
|
|
|
|
|
|
// TRACE_ERR("Reading: " << stringBuf << std::endl);
|
|
|
|
|
|
|
|
nextPound = getNextPound(stringBuf, substring, "|||"); //first field
|
|
|
|
sentence_index = atoi(substring.c_str());
|
|
|
|
nextPound = getNextPound(stringBuf, substring, "|||"); //second field
|
|
|
|
theSentence = substring;
|
|
|
|
|
|
|
|
|
|
|
|
entry.clear();
|
|
|
|
|
2008-05-14 16:23:58 +04:00
|
|
|
theScorer->prepareStats(sentence_index, theSentence,entry);
|
2008-05-14 12:14:13 +04:00
|
|
|
|
|
|
|
add(entry,sentence_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
inFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-16 00:44:24 +04:00
|
|
|
void ScoreData::add(const ScoreStats& e, int sent_idx){
|
2008-05-14 12:14:13 +04:00
|
|
|
if (exists(sent_idx)){
|
|
|
|
array_.at(sent_idx).add(e);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
ScoreArray a;
|
|
|
|
a.add(e);
|
|
|
|
a.setIndex(sent_idx);
|
|
|
|
add(a);
|
|
|
|
}
|
|
|
|
}
|