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