diff options
author | bhaddow <barry.haddow@gmail.com> | 2011-11-15 02:18:22 +0400 |
---|---|---|
committer | bhaddow <barry.haddow@gmail.com> | 2011-11-15 02:18:22 +0400 |
commit | 3a6c0e0680e656a9a05da24c1b54e54caf651f48 (patch) | |
tree | 5b0c73dfe9c3c0bf72ab403002bf50e1a3e82559 /mert/FeatureDataIterator.h | |
parent | 4cf6e0320a90950ab2688861066c36e613c534fa (diff) |
iterate through feature file
Diffstat (limited to 'mert/FeatureDataIterator.h')
-rw-r--r-- | mert/FeatureDataIterator.h | 44 |
1 files changed, 18 insertions, 26 deletions
diff --git a/mert/FeatureDataIterator.h b/mert/FeatureDataIterator.h index e87a6386d..49d77f77f 100644 --- a/mert/FeatureDataIterator.h +++ b/mert/FeatureDataIterator.h @@ -29,36 +29,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include <fstream> #include <map> +#include <memory> +#include <stdexcept> #include <vector> #include <boost/iterator/iterator_facade.hpp> +#include <boost/shared_ptr.hpp> +#include "util/file_piece.hh" -//Minimal sparse vector -class SparseVector { +#include "FeatureStats.h" - public: - typedef std::map<size_t,float> fvector_t; - typedef std::map<std::string, size_t> name2id_t; - typedef std::vector<std::string> id2name_t; - - float get(std::string name) const; - float get(size_t id) const; - void set(std::string name, float value); - void clear(); - size_t size() const; - - void write(std::ostream& out, const std::string& sep = " ") const; - - SparseVector& operator-=(const SparseVector& rhs); - - private: - static name2id_t name2id_; - static id2name_t id2name_; - fvector_t fvector_; -}; - -SparseVector operator-(const SparseVector& lhs, const SparseVector& rhs); class FeatureDataItem { public: @@ -66,16 +47,24 @@ class FeatureDataItem { SparseVector sparse; }; +class FileFormatException : public util::Exception { + public: + explicit FileFormatException(const std::string filename, const std::string& line) { + *this << "Error in line \"" << line << "\" of " << filename; + } +}; + class FeatureDataIterator : public boost::iterator_facade<FeatureDataIterator, const std::vector<FeatureDataItem>, boost::forward_traversal_tag> { public: + FeatureDataIterator(); FeatureDataIterator(const std::string filename); static FeatureDataIterator end() { - return FeatureDataIterator(""); + return FeatureDataIterator(); } @@ -86,7 +75,10 @@ class FeatureDataIterator : bool equal(const FeatureDataIterator& rhs) const; const std::vector<FeatureDataItem>& dereference() const; - std::ifstream* in_; + void readNext(); + + boost::shared_ptr<util::FilePiece> m_in; + std::vector<FeatureDataItem> m_next; }; #endif |