2008-05-15 12:35:56 +04:00
|
|
|
/*
|
|
|
|
* Data.cpp
|
|
|
|
* met - Minimum Error Training
|
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include "Scorer.h"
|
|
|
|
#include "Data.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
|
|
|
Data::Data(Scorer& ptr):
|
2008-05-27 20:50:52 +04:00
|
|
|
theScorer(&ptr)
|
2008-05-15 12:35:56 +04:00
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
score_type = (*theScorer).getName();
|
|
|
|
TRACE_ERR("Data::score_type " << score_type << std::endl);
|
|
|
|
|
|
|
|
TRACE_ERR("Data::Scorer type from Scorer: " << theScorer->getName() << endl);
|
|
|
|
featdata=new FeatureData;
|
|
|
|
scoredata=new ScoreData(*theScorer);
|
2008-05-15 12:35:56 +04:00
|
|
|
};
|
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
void Data::loadnamesfromnbest(const std::string &file)
|
|
|
|
{
|
|
|
|
TRACE_ERR("loading names of features from nbest file " << file << std::endl);
|
|
|
|
inputfilestream inp(file); // matches a stream with a file. Opens the file
|
|
|
|
|
|
|
|
if (!inp.good())
|
|
|
|
throw runtime_error("Unable to open: " + file);
|
|
|
|
|
|
|
|
std::string substring, subsubstring, stringBuf;
|
|
|
|
std::string::size_type loc;
|
|
|
|
|
|
|
|
int nextPound;
|
|
|
|
|
|
|
|
while (getline(inp,stringBuf,'\n')){
|
|
|
|
if (stringBuf.empty()) continue;
|
|
|
|
|
|
|
|
nextPound = getNextPound(stringBuf, substring, "|||"); //first field
|
|
|
|
nextPound = getNextPound(stringBuf, substring, "|||"); //second field
|
|
|
|
nextPound = getNextPound(stringBuf, substring, "|||"); //third field
|
|
|
|
|
|
|
|
// adding features
|
|
|
|
std::string tmpname="";
|
|
|
|
size_t tmpidx=0;
|
|
|
|
while (!substring.empty()){
|
|
|
|
// TRACE_ERR("Decompounding: " << substring << std::endl);
|
|
|
|
nextPound = getNextPound(substring, subsubstring);
|
|
|
|
|
|
|
|
// string ending with ":" are skipped, because they are the names of the features
|
|
|
|
if ((loc = subsubstring.find(":")) != subsubstring.length()-1){
|
|
|
|
featname2idx_[tmpname+"_"+stringify(tmpidx)]=idx2featname_.size();
|
|
|
|
idx2featname_[idx2featname_.size()]=tmpname+"_"+stringify(tmpidx);
|
|
|
|
tmpidx++;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
tmpidx=0;
|
|
|
|
tmpname=subsubstring.substr(0,subsubstring.size() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
number_of_features=idx2featname_.size();
|
|
|
|
TRACE_ERR("number_of_features: " << number_of_features << std::endl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
inp.close();
|
|
|
|
}
|
|
|
|
|
2008-05-15 12:35:56 +04:00
|
|
|
void Data::loadnbest(const std::string &file)
|
|
|
|
{
|
|
|
|
TRACE_ERR("loading nbest from " << file << std::endl);
|
|
|
|
|
|
|
|
FeatureStats featentry;
|
|
|
|
ScoreStats scoreentry;
|
2008-05-27 20:50:52 +04:00
|
|
|
std::string sentence_index;
|
2008-05-15 12:35:56 +04:00
|
|
|
int nextPound;
|
|
|
|
|
2008-05-20 18:15:30 +04:00
|
|
|
inputfilestream inp(file); // matches a stream with a file. Opens the file
|
2008-05-15 12:35:56 +04:00
|
|
|
|
2008-05-20 18:15:30 +04:00
|
|
|
if (!inp.good())
|
|
|
|
throw runtime_error("Unable to open: " + file);
|
2008-05-15 12:35:56 +04:00
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
std::string substring, subsubstring, stringBuf;
|
2008-05-20 18:15:30 +04:00
|
|
|
std::string theSentence;
|
|
|
|
std::string::size_type loc;
|
2008-05-15 12:35:56 +04:00
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
while (getline(inp,stringBuf,'\n')){
|
2008-05-15 12:35:56 +04:00
|
|
|
if (stringBuf.empty()) continue;
|
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
TRACE_ERR("stringBuf: " << stringBuf << std::endl);
|
|
|
|
|
2008-05-15 12:35:56 +04:00
|
|
|
nextPound = getNextPound(stringBuf, substring, "|||"); //first field
|
2008-05-27 20:50:52 +04:00
|
|
|
sentence_index = substring;
|
2008-05-15 12:35:56 +04:00
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
nextPound = getNextPound(stringBuf, substring, "|||"); //second field
|
|
|
|
theSentence = substring;
|
2008-05-15 12:35:56 +04:00
|
|
|
|
|
|
|
// adding statistics for error measures
|
2008-05-20 18:15:30 +04:00
|
|
|
featentry.clear();
|
|
|
|
scoreentry.clear();
|
2008-05-27 20:50:52 +04:00
|
|
|
TRACE_ERR("theSentence: " << theSentence << std::endl);
|
|
|
|
TRACE_ERR("sentence_index: " << sentence_index << std::endl);
|
|
|
|
|
|
|
|
|
|
|
|
theScorer->prepareStats(sentence_index, theSentence, scoreentry);
|
|
|
|
TRACE_ERR("scoreentry: " << scoreentry << std::endl);
|
|
|
|
scoredata->add(scoreentry, sentence_index);
|
|
|
|
|
2008-05-15 12:35:56 +04:00
|
|
|
nextPound = getNextPound(stringBuf, substring, "|||"); //third field
|
|
|
|
|
|
|
|
// adding features
|
|
|
|
while (!substring.empty()){
|
|
|
|
// TRACE_ERR("Decompounding: " << substring << std::endl);
|
|
|
|
nextPound = getNextPound(substring, subsubstring);
|
2008-05-20 18:15:30 +04:00
|
|
|
|
|
|
|
// string ending with ":" are skipped, because they are the names of the features
|
2008-05-27 20:50:52 +04:00
|
|
|
if ((loc = subsubstring.find(":")) != subsubstring.length()-1){
|
2008-05-15 12:35:56 +04:00
|
|
|
featentry.add(ATOFST(subsubstring.c_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
featdata->add(featentry,sentence_index);
|
|
|
|
}
|
|
|
|
|
2008-05-20 18:15:30 +04:00
|
|
|
inp.close();
|
2008-05-15 12:35:56 +04:00
|
|
|
}
|