00001
00002
00003
00004
00005
00006
00007 #pragma once
00008 #include <string>
00009 #include <boost/unordered_map.hpp>
00010 #include "moses/OutputFileStream.h"
00011 #include "moses/Util.h"
00012
00013 namespace Moses
00014 {
00015
00016 template<typename VOCABID>
00017 class StoreVocab
00018 {
00019 protected:
00020 std::string m_path;
00021
00022 typedef boost::unordered_map<std::string, VOCABID> Coll;
00023 Coll m_vocab;
00024
00025 public:
00026 StoreVocab(const std::string &path)
00027 :m_path(path)
00028 {}
00029
00030 virtual ~StoreVocab() {}
00031
00032 VOCABID GetVocabId(const std::string &word) {
00033 typename Coll::iterator iter = m_vocab.find(word);
00034 if (iter == m_vocab.end()) {
00035 VOCABID ind = m_vocab.size() + 1;
00036 m_vocab[word] = ind;
00037 return ind;
00038 } else {
00039 return iter->second;
00040 }
00041 }
00042
00043 void Insert(VOCABID id, const std::string &word) {
00044 m_vocab[word] = id;
00045 }
00046
00047 void Save() {
00048 OutputFileStream strme(m_path);
00049
00050 typename Coll::const_iterator iter;
00051 for (iter = m_vocab.begin(); iter != m_vocab.end(); ++iter) {
00052 strme << iter->first << "\t" << iter->second << std::endl;
00053 }
00054
00055 strme.Close();
00056 }
00057 };
00058
00059 }
00060