diff options
author | bhaddow <barry.haddow@gmail.com> | 2011-11-14 22:32:36 +0400 |
---|---|---|
committer | bhaddow <barry.haddow@gmail.com> | 2011-11-14 22:32:36 +0400 |
commit | 4cf6e0320a90950ab2688861066c36e613c534fa (patch) | |
tree | b355207220c50a9b4982b954c7d86b6f83de2c04 /mert/FeatureDataIterator.h | |
parent | 4bb9ecb8eb7548e47b3563b3632a7801a8f13032 (diff) |
pro extraction mainline and stub of feature data iterator
Diffstat (limited to 'mert/FeatureDataIterator.h')
-rw-r--r-- | mert/FeatureDataIterator.h | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/mert/FeatureDataIterator.h b/mert/FeatureDataIterator.h new file mode 100644 index 000000000..e87a6386d --- /dev/null +++ b/mert/FeatureDataIterator.h @@ -0,0 +1,94 @@ +// $Id$ +// vim:tabstop=2 + +/*********************************************************************** +Moses - factored phrase-based language decoder +Copyright (C) 2011- University of Edinburgh + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +***********************************************************************/ + +#ifndef _FEATURE_DATA_ITERATOR_ +#define _FEATURE_DATA_ITERATOR_ + +/** + * For loading from the feature data file. +**/ + +#include <fstream> +#include <map> +#include <vector> + +#include <boost/iterator/iterator_facade.hpp> + + +//Minimal sparse vector +class SparseVector { + + 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: + std::vector<float> dense; + SparseVector sparse; +}; + +class FeatureDataIterator : + public boost::iterator_facade<FeatureDataIterator, + const std::vector<FeatureDataItem>, + boost::forward_traversal_tag> +{ + public: + FeatureDataIterator(const std::string filename); + + static FeatureDataIterator end() { + return FeatureDataIterator(""); + } + + + private: + friend class boost::iterator_core_access; + + void increment(); + bool equal(const FeatureDataIterator& rhs) const; + const std::vector<FeatureDataItem>& dereference() const; + + std::ifstream* in_; +}; + +#endif + + |