2012-03-20 00:49:10 +04:00
|
|
|
#include "Vocabulary.h"
|
|
|
|
#include "Singleton.h"
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
namespace mert
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
2012-03-20 00:49:10 +04:00
|
|
|
Vocabulary* g_vocab = NULL;
|
|
|
|
} // namespace
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
int Vocabulary::Encode(const std::string& token)
|
|
|
|
{
|
|
|
|
iterator it = m_vocab.find(token);
|
|
|
|
int encoded_token;
|
|
|
|
if (it == m_vocab.end()) {
|
|
|
|
// Add an new entry to the vocaburary.
|
|
|
|
encoded_token = static_cast<int>(m_vocab.size());
|
|
|
|
|
|
|
|
m_vocab[token] = encoded_token;
|
|
|
|
} else {
|
|
|
|
encoded_token = it->second;
|
|
|
|
}
|
|
|
|
return encoded_token;
|
2012-06-26 21:33:50 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
bool Vocabulary::Lookup(const std::string&str , int* v) const
|
|
|
|
{
|
2012-06-26 21:33:50 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
const_iterator it = m_vocab.find(str);
|
|
|
|
if (it == m_vocab.end()) return false;
|
|
|
|
*v = it->second;
|
|
|
|
return true;
|
2012-06-26 21:33:50 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
Vocabulary* VocabularyFactory::GetVocabulary()
|
|
|
|
{
|
2012-03-20 00:49:10 +04:00
|
|
|
if (g_vocab == NULL) {
|
2012-06-30 23:23:45 +04:00
|
|
|
return MosesTuning::Singleton<Vocabulary>::GetInstance();
|
2012-03-20 00:49:10 +04:00
|
|
|
} else {
|
|
|
|
return g_vocab;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void VocabularyFactory::SetVocabulary(Vocabulary* vocab)
|
|
|
|
{
|
2012-03-20 00:49:10 +04:00
|
|
|
g_vocab = vocab;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mert
|