mosesdecoder/mert/ScoreData.cpp
jfouet ad79af41df bug fixes
git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@1719 1f5c12ca-751b-0410-a591-d2e778427230
2008-05-15 20:49:49 +00:00

130 lines
2.7 KiB
C++

/*
* ScoreData.cpp
* met - Minimum Error Training
*
* Created by Nicola Bertoldi on 13/05/08.
*
*/
#include <fstream>
#include "ScoreData.h"
#include "Scorer.h"
#include "Util.h"
ScoreData::ScoreData(Scorer& ptr):
theScorer(&ptr)
{
score_type = theScorer->getName();
TRACE_ERR("score_type:" << score_type << std::endl);
theScorer->setScoreData(this);//this is not dangerous: we dont use the this pointer in SetScoreData
};
void ScoreData::save(std::ofstream& outFile, bool bin)
{
ScoreArray entry;
for (vector<ScoreArray>::iterator i = array_.begin(); i !=array_.end(); i++)
(*i).save(outFile);
}
void ScoreData::save(const std::string &file, bool bin)
{
if (file.empty()) return;
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;
save(outFile, bin);
outFile.close();
}
void ScoreData::load(ifstream& inFile)
{
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++;
}
}
void ScoreData::load(const std::string &file)
{
TRACE_ERR("loading score data from " << file << std::endl);
std::ifstream inFile(file.c_str(), std::ios::in); // matches a stream with a file. Opens the file
if (!inFile) {
throw runtime_error("Unable to open score file: " + file);
}
load(inFile);
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;
std::string theSentence;
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();
theScorer->prepareStats(sentence_index, theSentence,entry);
add(entry,sentence_index);
}
inFile.close();
}
void ScoreData::add(const ScoreStats& e, int sent_idx){
if (exists(sent_idx)){
array_.at(sent_idx).add(e);
}
else{
ScoreArray a;
a.add(e);
a.setIndex(sent_idx);
add(a);
}
}