00001
00002
00003
00004 #ifndef AITOOLS_INVERTEDINDEX_VOCABULARY_HPP
00005 #define AITOOLS_INVERTEDINDEX_VOCABULARY_HPP
00006
00007 #include "Externalizable.hpp"
00008 #include <tr1/unordered_map>
00009 #include <stdexcept>
00010 #include <ostream>
00011 #include <vector>
00012 #include <string>
00013 #include <map>
00014
00015 namespace aitools {
00016 namespace invertedindex {
00017
00028 class Vocabulary : public Externalizable {
00029
00030 public:
00031
00039 typedef std::map<std::string, uint64_t> tree_map_t;
00040
00046 typedef std::tr1::unordered_map<std::string, uint64_t> hash_map_t;
00047
00051 typedef hash_map_t::const_iterator const_iterator;
00052
00056 typedef hash_map_t::iterator iterator;
00057
00058 public:
00059
00060 static const char delim = '\t';
00061 static const char newline = '\n';
00062
00063 public:
00064
00068 Vocabulary();
00069
00074 Vocabulary(const bfs::path& path) throw (std::invalid_argument);
00075
00079 Vocabulary(const Vocabulary& vocabulary);
00080
00084 ~Vocabulary();
00085
00086 public:
00087
00092 void accumulate(const std::string& word, uint64_t frequency = 1);
00093
00099 const_iterator begin() const;
00100
00106 iterator begin();
00107
00111 void clear();
00112
00118 bool contains(const std::string& word) const;
00119
00125 const_iterator end() const;
00126
00132 iterator end();
00133
00138 const_iterator find(const std::string& word) const;
00139
00144 iterator find(const std::string& word);
00145
00151 uint64_t frequency(const std::string& word) const;
00152
00157 void get_words(std::vector<std::string>& words) const;
00158
00164 bool insert(const std::string& word, uint64_t frequency = 0);
00165
00170 bool is_empty() const;
00171
00176 void load(const bfs::path& file) throw (std::invalid_argument);
00177
00183 void save(const bfs::path& file) throw (std::invalid_argument);
00184
00189 size_t size() const;
00190
00191 private:
00192
00193 hash_map_t hash_map_;
00194
00195 };
00196
00197 }
00198 }
00199
00200 namespace std
00201 {
00202 ostream&
00203 operator<<(ostream& os, const aitools::invertedindex::Vocabulary& vocab);
00204
00205 }
00206
00207 #endif // AITOOLS_INVERTEDINDEX_VOCABULARY_HPP