00001 /* Copyright (C) 2010 webis.de 00002 * All rights reserved. 00003 */ 00004 #ifndef AITOOLS_INVERTEDINDEX_NGRAM_FILE_READER_HPP 00005 #define AITOOLS_INVERTEDINDEX_NGRAM_FILE_READER_HPP 00006 00007 #include "Triple.hpp" 00008 #include "RecordReader.hpp" 00009 #include <boost/filesystem/fstream.hpp> 00010 00011 namespace bfs = boost::filesystem; 00012 00013 namespace aitools { 00014 namespace invertedindex { 00015 00016 typedef Triple<int64_t, int32_t, int8_t> value_type; 00017 00018 class NGramFileReader : public RecordReader<value_type> { 00019 00020 public: // c'tor / d'tor 00021 00025 NGramFileReader() {}; 00026 00030 ~NGramFileReader() {}; 00031 00032 public: // methods 00033 00037 void close() 00038 { 00039 ifs_.close(); 00040 iss_.clear(); 00041 iss_.str(""); 00042 } 00043 00050 bool next(Record<value_type>& record) 00051 { 00052 while (true) 00053 { 00054 if (iss_ >> record.key()) 00055 { 00056 record.value() = value_; 00057 ++value_.e3(); 00058 break; 00059 } 00060 else if (std::getline(ifs_, line_)) 00061 { 00062 tabpos_ = line_.find('\t'); 00063 if (tabpos_ == std::string::npos) continue; 00064 iss_.clear(); 00065 iss_.str(line_.substr(tabpos_)); 00066 iss_ >> value_.e1(); 00067 iss_ >> value_.e2(); 00068 value_.e3() = 0; 00069 iss_.clear(); 00070 iss_.str(line_.substr(0, tabpos_)); 00071 } 00072 else 00073 { 00074 return false; 00075 } 00076 } 00077 return true; 00078 } 00079 00085 void open(const bfs::path& path) 00086 { 00087 ifs_.open(path); 00088 } 00089 00090 private: 00091 00092 bfs::ifstream ifs_; 00093 std::istringstream iss_; 00094 std::string::size_type tabpos_; 00095 std::string line_; 00096 value_type value_; 00097 00098 }; 00099 00100 } // namespace invertedindex 00101 } // namespace aitools 00102 00103 #endif // AITOOLS_INVERTEDINDEX_NGRAM_FILE_READER_HPP