2008-05-14 11:57:45 +04:00
|
|
|
/*
|
|
|
|
* FeatureArray.cpp
|
|
|
|
* met - Minimum Error Training
|
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include "FeatureArray.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
FeatureArray::FeatureArray(): idx("")
|
2008-05-14 11:57:45 +04:00
|
|
|
{};
|
|
|
|
|
|
|
|
void FeatureArray::savetxt(std::ofstream& outFile)
|
|
|
|
{
|
|
|
|
|
2008-05-14 15:05:32 +04:00
|
|
|
outFile << FEATURES_TXT_BEGIN << " " << idx << " " << array_.size() << std::endl;
|
2008-05-27 20:50:52 +04:00
|
|
|
for (featarray_t::iterator i = array_.begin(); i !=array_.end(); i++){
|
|
|
|
i->savetxt(outFile);
|
|
|
|
outFile << std::endl;
|
|
|
|
}
|
2008-05-14 15:05:32 +04:00
|
|
|
outFile << FEATURES_TXT_END << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureArray::savebin(std::ofstream& outFile)
|
|
|
|
{
|
|
|
|
TRACE_ERR("binary saving is not yet implemented!" << std::endl);
|
|
|
|
|
|
|
|
/*
|
|
|
|
NOT YET IMPLEMENTED
|
|
|
|
*/
|
|
|
|
outFile << FEATURES_TXT_BEGIN << " " << idx << " " << array_.size() << std::endl;
|
|
|
|
outFile << FEATURES_BIN_END << std::endl;
|
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 15:28:54 +04:00
|
|
|
|
|
|
|
void FeatureArray::save(std::ofstream& inFile, bool bin)
|
|
|
|
{
|
|
|
|
(bin)?savebin(inFile):savetxt(inFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureArray::save(const std::string &file, bool bin)
|
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 15:28:54 +04:00
|
|
|
save(outFile);
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-05-14 15:28:54 +04:00
|
|
|
outFile.close();
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureArray::loadtxt(ifstream& inFile)
|
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
FeatureStats entry;
|
2008-05-14 11:57:45 +04:00
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
int number_of_entries=0;
|
2008-05-14 11:57:45 +04:00
|
|
|
int nextPound;
|
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
std::string substring, stringBuf;
|
|
|
|
std::string::size_type loc;
|
2008-05-14 11:57:45 +04:00
|
|
|
|
|
|
|
|
|
|
|
std::getline(inFile, stringBuf);
|
2008-05-20 18:15:30 +04:00
|
|
|
if (!inFile.good()){
|
2008-05-14 11:57:45 +04:00
|
|
|
return;
|
2008-05-20 18:15:30 +04:00
|
|
|
}
|
2008-05-14 11:57:45 +04:00
|
|
|
|
|
|
|
if (!stringBuf.empty()){
|
2008-05-20 18:15:30 +04:00
|
|
|
TRACE_ERR("Reading: " << stringBuf << std::endl);
|
2008-05-14 15:28:54 +04:00
|
|
|
if ((loc = stringBuf.find(FEATURES_TXT_BEGIN)) != 0){
|
|
|
|
TRACE_ERR("ERROR: FeatureArray::loadtxt(): Wrong header");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nextPound = getNextPound(stringBuf, substring);
|
2008-05-14 11:57:45 +04:00
|
|
|
nextPound = getNextPound(stringBuf, substring);
|
2008-05-27 20:50:52 +04:00
|
|
|
idx = substring;
|
2008-05-14 11:57:45 +04:00
|
|
|
nextPound = getNextPound(stringBuf, substring);
|
2008-05-27 20:50:52 +04:00
|
|
|
number_of_entries = atoi(substring.c_str());
|
|
|
|
// TRACE_ERR("idx: " << idx " nbest: " << number_of_entries << std::endl);
|
2008-05-14 11:57:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0 ; i < number_of_entries; i++)
|
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
entry.clear();
|
|
|
|
std::getline(inFile, stringBuf);
|
2008-05-14 11:57:45 +04:00
|
|
|
entry.set(stringBuf);
|
|
|
|
add(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::getline(inFile, stringBuf);
|
|
|
|
if (!stringBuf.empty()){
|
|
|
|
// TRACE_ERR("Reading: " << stringBuf << std::endl);
|
2008-05-14 15:28:54 +04:00
|
|
|
if ((loc = stringBuf.find(FEATURES_TXT_END)) != 0){
|
2008-05-14 11:57:45 +04:00
|
|
|
TRACE_ERR("ERROR: FeatureArray::loadtxt(): Wrong footer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-14 15:28:54 +04:00
|
|
|
void FeatureArray::loadbin(ifstream& inFile)
|
|
|
|
{
|
|
|
|
TRACE_ERR("binary saving is not yet implemented!" << std::endl);
|
|
|
|
|
|
|
|
/*
|
|
|
|
NOT YET IMPLEMENTED
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureArray::load(ifstream& inFile, bool bin)
|
|
|
|
{
|
|
|
|
(bin)?loadbin(inFile):loadtxt(inFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureArray::load(const std::string &file, bool bin)
|
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
|
|
|
load((ifstream&) inFile, bin);
|
2008-05-14 15:28:54 +04:00
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
inFile.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-14 18:16:05 +04:00
|
|
|
void FeatureArray::merge(FeatureArray& e)
|
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
//dummy implementation
|
|
|
|
for (size_t i=0; i<e.size(); i++)
|
2008-05-14 18:16:05 +04:00
|
|
|
add(e.get(i));
|
|
|
|
}
|
2008-05-27 20:50:52 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool FeatureArray::check_consistency()
|
|
|
|
{
|
|
|
|
size_t sz = NumberOfFeatures();
|
|
|
|
|
|
|
|
if (sz == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (featarray_t::iterator i=array_.begin(); i!=array_.end(); i++)
|
|
|
|
if (i->size()!=sz)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|