00001 #ifndef MERT_VOCABULARY_H_ 00002 #define MERT_VOCABULARY_H_ 00003 00004 #include <boost/unordered_map.hpp> 00005 #include <string> 00006 00007 namespace mert 00008 { 00009 00016 class Vocabulary 00017 { 00018 public: 00019 typedef boost::unordered_map<std::string, int>::iterator iterator; 00020 typedef boost::unordered_map<std::string, int>::const_iterator const_iterator; 00021 00022 Vocabulary() {} 00023 virtual ~Vocabulary() {} 00024 00026 int Encode(const std::string& token); 00027 00031 bool Lookup(const std::string&str , int* v) const; 00032 00033 void clear() { 00034 m_vocab.clear(); 00035 } 00036 00037 bool empty() const { 00038 return m_vocab.empty(); 00039 } 00040 00041 std::size_t size() const { 00042 return m_vocab.size(); 00043 } 00044 00045 iterator find(const std::string& str) { 00046 return m_vocab.find(str); 00047 } 00048 const_iterator find(const std::string& str) const { 00049 return m_vocab.find(str); 00050 } 00051 00052 int& operator[](const std::string& str) { 00053 return m_vocab[str]; 00054 } 00055 00056 iterator begin() { 00057 return m_vocab.begin(); 00058 } 00059 const_iterator begin() const { 00060 return m_vocab.begin(); 00061 } 00062 iterator end() { 00063 return m_vocab.end(); 00064 } 00065 const_iterator end() const { 00066 return m_vocab.end(); 00067 } 00068 00069 private: 00070 boost::unordered_map<std::string, int> m_vocab; 00071 }; 00072 00073 class VocabularyFactory 00074 { 00075 public: 00076 static Vocabulary* GetVocabulary(); 00077 static void SetVocabulary(Vocabulary* vocab); 00078 00079 private: 00080 VocabularyFactory() {} 00081 virtual ~VocabularyFactory() {} 00082 }; 00083 00084 } // namespace mert 00085 00086 #endif // MERT_VOCABULARY_H_