00001 #include "Vocabulary.h" 00002 #include "Singleton.h" 00003 00004 namespace mert { 00005 namespace { 00006 Vocabulary* g_vocab = NULL; 00007 } // namespace 00008 00009 int Vocabulary::Encode(const std::string& token) { 00010 iterator it = m_vocab.find(token); 00011 int encoded_token; 00012 if (it == m_vocab.end()) { 00013 // Add an new entry to the vocaburary. 00014 encoded_token = static_cast<int>(m_vocab.size()); 00015 00016 m_vocab[token] = encoded_token; 00017 } else { 00018 encoded_token = it->second; 00019 } 00020 return encoded_token; 00021 } 00022 00023 bool Vocabulary::Lookup(const std::string&str , int* v) const { 00024 00025 const_iterator it = m_vocab.find(str); 00026 if (it == m_vocab.end()) return false; 00027 *v = it->second; 00028 return true; 00029 } 00030 00031 Vocabulary* VocabularyFactory::GetVocabulary() { 00032 if (g_vocab == NULL) { 00033 return MosesTuning::Singleton<Vocabulary>::GetInstance(); 00034 } else { 00035 return g_vocab; 00036 } 00037 } 00038 00039 void VocabularyFactory::SetVocabulary(Vocabulary* vocab) { 00040 g_vocab = vocab; 00041 } 00042 00043 } // namespace mert