2008-05-14 11:57:45 +04:00
|
|
|
/*
|
|
|
|
* FeatureData.h
|
|
|
|
* met - Minimum Error Training
|
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FEATURE_DATA_H
|
|
|
|
#define FEATURE_DATA_H
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
2011-11-14 10:15:30 +04:00
|
|
|
#include <stdexcept>
|
2008-05-14 11:57:45 +04:00
|
|
|
#include "FeatureArray.h"
|
|
|
|
|
|
|
|
class FeatureData
|
|
|
|
{
|
|
|
|
private:
|
2011-02-24 15:42:19 +03:00
|
|
|
size_t number_of_features;
|
|
|
|
std::string features;
|
2011-09-07 20:37:33 +04:00
|
|
|
bool _sparse_flag;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
map<std::string, size_t> featname2idx_; // map from name to index of features
|
|
|
|
map<size_t, std::string> idx2featname_; // map from index to name of features
|
2008-06-05 21:03:54 +04:00
|
|
|
|
2011-11-14 07:20:04 +04:00
|
|
|
protected:
|
|
|
|
featdata_t array_;
|
|
|
|
idx2name idx2arrayname_; // map from index to name of array
|
|
|
|
name2idx arrayname2idx_; // map from name to index of array
|
|
|
|
|
2008-05-14 11:57:45 +04:00
|
|
|
public:
|
2011-02-24 15:42:19 +03:00
|
|
|
FeatureData();
|
2011-11-12 04:40:01 +04:00
|
|
|
~FeatureData() {}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
|
|
|
inline void clear() {
|
|
|
|
array_.clear();
|
|
|
|
}
|
|
|
|
|
2011-11-12 02:59:50 +04:00
|
|
|
inline bool hasSparseFeatures() const {
|
|
|
|
return _sparse_flag;
|
2011-09-07 20:37:33 +04:00
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
inline FeatureArray get(const std::string& idx) {
|
|
|
|
return array_.at(getIndex(idx));
|
|
|
|
}
|
|
|
|
inline FeatureArray& get(size_t idx) {
|
|
|
|
return array_.at(idx);
|
|
|
|
}
|
|
|
|
inline const FeatureArray& get(size_t idx) const {
|
|
|
|
return array_.at(idx);
|
|
|
|
}
|
|
|
|
|
2011-11-14 09:00:47 +04:00
|
|
|
inline bool exists(const std::string& sent_idx) {
|
2011-02-24 15:42:19 +03:00
|
|
|
return exists(getIndex(sent_idx));
|
|
|
|
}
|
|
|
|
inline bool exists(int sent_idx) {
|
|
|
|
return (sent_idx>-1 && sent_idx<(int) array_.size())?true:false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline FeatureStats& get(size_t i, size_t j) {
|
|
|
|
return array_.at(i).get(j);
|
|
|
|
}
|
|
|
|
inline const FeatureStats& get(size_t i, size_t j) const {
|
|
|
|
return array_.at(i).get(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(FeatureArray& e);
|
|
|
|
void add(FeatureStats& e, const std::string& sent_idx);
|
|
|
|
|
2011-11-12 06:26:13 +04:00
|
|
|
inline size_t size() const {
|
2011-02-24 15:42:19 +03:00
|
|
|
return array_.size();
|
|
|
|
}
|
|
|
|
inline size_t NumberOfFeatures() const {
|
|
|
|
return number_of_features;
|
|
|
|
}
|
|
|
|
inline void NumberOfFeatures(size_t v) {
|
|
|
|
number_of_features = v;
|
|
|
|
}
|
|
|
|
inline std::string Features() const {
|
|
|
|
return features;
|
|
|
|
}
|
2011-11-14 09:00:47 +04:00
|
|
|
inline void Features(const std::string& f) {
|
2011-02-24 15:42:19 +03:00
|
|
|
features = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void save(const std::string &file, bool bin=false);
|
|
|
|
void save(ofstream& outFile, bool bin=false);
|
|
|
|
inline void save(bool bin=false) {
|
|
|
|
save("/dev/stdout", bin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void load(ifstream& inFile);
|
|
|
|
void load(const std::string &file);
|
|
|
|
|
2011-11-12 06:26:13 +04:00
|
|
|
bool check_consistency() const;
|
2011-02-24 15:42:19 +03:00
|
|
|
void setIndex();
|
|
|
|
|
2011-11-12 06:26:13 +04:00
|
|
|
inline int getIndex(const std::string& idx) const {
|
|
|
|
name2idx::const_iterator i = arrayname2idx_.find(idx);
|
|
|
|
if (i != arrayname2idx_.end())
|
2011-02-24 15:42:19 +03:00
|
|
|
return i->second;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-12 06:26:13 +04:00
|
|
|
inline std::string getIndex(size_t idx) const {
|
|
|
|
idx2name::const_iterator i = idx2arrayname_.find(idx);
|
|
|
|
if (i != idx2arrayname_.end())
|
2011-02-24 15:42:19 +03:00
|
|
|
throw runtime_error("there is no entry at index " + idx);
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
2011-11-12 06:26:13 +04:00
|
|
|
bool existsFeatureNames() const {
|
|
|
|
return (idx2featname_.size() > 0) ? true : false;
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-12 06:26:13 +04:00
|
|
|
std::string getFeatureName(size_t idx) const {
|
2011-02-24 15:42:19 +03:00
|
|
|
if (idx >= idx2featname_.size())
|
|
|
|
throw runtime_error("Error: you required an too big index");
|
2011-11-12 06:26:13 +04:00
|
|
|
map<size_t, std::string>::const_iterator it = idx2featname_.find(idx);
|
|
|
|
if (it == idx2featname_.end()) {
|
|
|
|
throw runtime_error("Error: specified id is unknown: " + idx);
|
|
|
|
} else {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getFeatureIndex(const std::string& name) const {
|
|
|
|
map<std::string, size_t>::const_iterator it = featname2idx_.find(name);
|
|
|
|
if (it == featname2idx_.end())
|
|
|
|
throw runtime_error("Error: feature " + name + " is unknown");
|
|
|
|
return it->second;
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-14 09:00:47 +04:00
|
|
|
void setFeatureMap(const std::string& feat);
|
2008-05-14 11:57:45 +04:00
|
|
|
};
|
|
|
|
|
2011-11-12 02:59:50 +04:00
|
|
|
#endif // FEATURE_DATA_H
|