2008-05-14 11:57:45 +04:00
|
|
|
/*
|
|
|
|
* FeatureData.cpp
|
|
|
|
* met - Minimum Error Training
|
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include "FeatureData.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
static const float MIN_FLOAT=-1.0*numeric_limits<float>::max();
|
|
|
|
static const float MAX_FLOAT=numeric_limits<float>::max();
|
|
|
|
|
2008-05-16 23:57:01 +04:00
|
|
|
FeatureData::FeatureData() {};
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-05-14 18:16:05 +04:00
|
|
|
void FeatureData::save(std::ofstream& outFile, bool bin)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
for (featdata_t::iterator i = array_.begin(); i !=array_.end(); i++)
|
|
|
|
i->save(outFile, bin);
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 18:16:05 +04:00
|
|
|
void FeatureData::save(const std::string &file, bool bin)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2008-05-15 12:35:56 +04:00
|
|
|
if (file.empty()) return;
|
|
|
|
|
2008-05-14 11:57:45 +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
|
|
|
|
|
2008-05-14 18:16:05 +04:00
|
|
|
save(outFile, bin);
|
2008-05-14 15:28:54 +04:00
|
|
|
|
|
|
|
outFile.close();
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 18:16:05 +04:00
|
|
|
void FeatureData::load(ifstream& inFile)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
FeatureArray entry;
|
2008-05-14 11:57:45 +04:00
|
|
|
|
|
|
|
int iter=0;
|
|
|
|
while (!inFile.eof()){
|
|
|
|
|
2008-05-20 18:15:30 +04:00
|
|
|
|
|
|
|
if (!inFile.good()){
|
|
|
|
std::cerr << "ERROR FeatureData::load inFile.good()" << std::endl;
|
|
|
|
}
|
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
entry.clear();
|
2008-05-14 15:28:54 +04:00
|
|
|
entry.load(inFile);
|
2008-05-14 11:57:45 +04:00
|
|
|
|
|
|
|
if (entry.size() == 0){
|
2008-05-20 18:15:30 +04:00
|
|
|
return;
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
add(entry);
|
|
|
|
iter++;
|
|
|
|
}
|
2008-05-27 20:50:52 +04:00
|
|
|
if (size()>0)
|
|
|
|
number_of_features=get(0).NumberOfFeatures();
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-14 18:16:05 +04:00
|
|
|
void FeatureData::load(const std::string &file)
|
2008-05-14 11:57:45 +04:00
|
|
|
{
|
|
|
|
TRACE_ERR("loading data from " << file << std::endl);
|
|
|
|
|
2008-05-20 18:15:30 +04:00
|
|
|
inputfilestream inFile(file); // matches a stream with a file. Opens the file
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-05-20 18:15:30 +04:00
|
|
|
if (!inFile) {
|
|
|
|
throw runtime_error("Unable to open feature file: " + file);
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-05-20 18:15:30 +04:00
|
|
|
load((ifstream&) inFile);
|
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
inFile.close();
|
|
|
|
}
|
2008-05-27 20:50:52 +04:00
|
|
|
|
2008-05-14 18:16:05 +04:00
|
|
|
void FeatureData::add(FeatureArray& e){
|
2008-05-27 20:50:52 +04:00
|
|
|
if (exists(e.getIndex())){ // array at position e.getIndex() already exists
|
|
|
|
//enlarge array at position e.getIndex()
|
|
|
|
size_t pos = getIndex(e.getIndex());
|
|
|
|
array_.at(pos).merge(e);
|
2008-05-14 18:16:05 +04:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
array_.push_back(e);
|
2008-05-27 20:50:52 +04:00
|
|
|
setIndex();
|
2008-05-14 18:16:05 +04:00
|
|
|
}
|
|
|
|
}
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
void FeatureData::add(FeatureStats& e, const std::string & sent_idx){
|
|
|
|
if (exists(sent_idx)){ // array at position e.getIndex() already exists
|
|
|
|
//enlarge array at position e.getIndex()
|
|
|
|
size_t pos = getIndex(sent_idx);
|
|
|
|
// TRACE_ERR("Inserting " << e << " in array " << sent_idx << std::endl);
|
|
|
|
array_.at(pos).add(e);
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
else{
|
2008-05-27 20:50:52 +04:00
|
|
|
// TRACE_ERR("Creating a new entry in the array and inserting " << e << std::endl);
|
2008-05-14 11:57:45 +04:00
|
|
|
FeatureArray a;
|
|
|
|
a.add(e);
|
|
|
|
a.setIndex(sent_idx);
|
|
|
|
add(a);
|
|
|
|
}
|
2008-05-15 10:44:36 +04:00
|
|
|
}
|
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
bool FeatureData::check_consistency()
|
|
|
|
{
|
|
|
|
if (array_.size() == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (featdata_t::iterator i = array_.begin(); i !=array_.end(); i++)
|
|
|
|
if (!i->check_consistency()) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureData::setIndex()
|
|
|
|
{
|
|
|
|
size_t j=0;
|
|
|
|
for (featdata_t::iterator i = array_.begin(); i !=array_.end(); i++){
|
|
|
|
idx2arrayname_[j]=(*i).getIndex();
|
|
|
|
arrayname2idx_[(*i).getIndex()] = j;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|