00001 /*********************************************************************** 00002 Moses - factored phrase-based language decoder 00003 Copyright (C) 2011- University of Edinburgh 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 ***********************************************************************/ 00019 00020 #ifndef MERT_FEATURE_DATA_ITERATOR_H_ 00021 #define MERT_FEATURE_DATA_ITERATOR_H_ 00022 00027 #include <fstream> 00028 #include <map> 00029 #include <stdexcept> 00030 #include <vector> 00031 00032 #include <boost/iterator/iterator_facade.hpp> 00033 #include <boost/shared_ptr.hpp> 00034 00035 #include "util/exception.hh" 00036 #include "util/string_piece.hh" 00037 00038 #include "FeatureStats.h" 00039 00040 namespace util 00041 { 00042 class FilePiece; 00043 } 00044 00045 namespace MosesTuning 00046 { 00047 00048 00049 class FileFormatException : public util::Exception 00050 { 00051 public: 00052 explicit FileFormatException(const std::string& filename, const std::string& line) { 00053 *this << "Error in line \"" << line << "\" of " << filename; 00054 } 00055 }; 00056 00057 00059 int ParseInt(const StringPiece& str ); 00060 00062 float ParseFloat(const StringPiece& str); 00063 00064 00065 class FeatureDataItem 00066 { 00067 public: 00068 std::vector<float> dense; 00069 SparseVector sparse; 00070 }; 00071 00072 bool operator==(FeatureDataItem const& item1, FeatureDataItem const& item2); 00073 std::size_t hash_value(FeatureDataItem const& item); 00074 00075 class FeatureDataIterator : 00076 public boost::iterator_facade<FeatureDataIterator, 00077 const std::vector<FeatureDataItem>, 00078 boost::forward_traversal_tag> 00079 { 00080 public: 00081 FeatureDataIterator(); 00082 explicit FeatureDataIterator(const std::string& filename); 00083 ~FeatureDataIterator(); 00084 00085 static FeatureDataIterator end() { 00086 return FeatureDataIterator(); 00087 } 00088 00089 00090 private: 00091 friend class boost::iterator_core_access; 00092 00093 void increment(); 00094 bool equal(const FeatureDataIterator& rhs) const; 00095 const std::vector<FeatureDataItem>& dereference() const; 00096 00097 void readNext(); 00098 00099 boost::shared_ptr<util::FilePiece> m_in; 00100 std::vector<FeatureDataItem> m_next; 00101 }; 00102 00103 } 00104 00105 #endif // MERT_FEATURE_DATA_ITERATOR_H_