move GetFirstLM() as static method of LanguageModel

This commit is contained in:
Hieu Hoang 2013-05-28 17:35:06 +01:00
parent 266a81c9cf
commit b5bb500773
4 changed files with 28 additions and 27 deletions

View File

@ -184,7 +184,7 @@ Manager::~Manager() {
}
template <class Model, class Best> search::History Manager::PopulateBest(const Model &model, const std::vector<lm::WordIndex> &words, Best &out) {
const LanguageModel &abstract = GetFirstLM();
const LanguageModel &abstract = LanguageModel::GetFirstLM();
const float oov_weight = abstract.OOVFeatureEnabled() ? abstract.GetOOVWeight() : 0.0;
const StaticData &data = StaticData::Instance();
search::Config config(abstract.GetWeight(), data.GetCubePruningPopLimit(), search::NBestConfig(data.GetNBestSize()));
@ -238,7 +238,7 @@ template void Manager::LMCallback<lm::ngram::ArrayTrieModel>(const lm::ngram::Ar
template void Manager::LMCallback<lm::ngram::QuantArrayTrieModel>(const lm::ngram::QuantArrayTrieModel &model, const std::vector<lm::WordIndex> &words);
const std::vector<search::Applied> &Manager::ProcessSentence() {
GetFirstLM().IncrementalCallback(*this);
LanguageModel::GetFirstLM().IncrementalCallback(*this);
return *completed_nbest_;
}
@ -285,34 +285,11 @@ void PhraseAndFeatures(const search::Applied final, Phrase &phrase, ScoreCompone
float full, ignored_ngram;
std::size_t ignored_oov;
const LanguageModel &model = GetFirstLM();
const LanguageModel &model = LanguageModel::GetFirstLM();
model.CalcScore(phrase, full, ignored_ngram, ignored_oov);
// CalcScore transforms, but EvaluateChart doesn't.
features.Assign(&model, full);
}
const LanguageModel &GetFirstLM()
{
static const LanguageModel *lmStatic = NULL;
if (lmStatic) {
return *lmStatic;
}
// 1st time looking up lm
const std::vector<const StatefulFeatureFunction*> &statefulFFs = StatefulFeatureFunction::GetStatefulFeatureFunctions();
for (size_t i = 0; i < statefulFFs.size(); ++i) {
const StatefulFeatureFunction *ff = statefulFFs[i];
const LanguageModel *lm = dynamic_cast<const LanguageModel*>(ff);
if (lm) {
lmStatic = lm;
return *lmStatic;
}
}
throw std::logic_error("Incremental search only supports one language model.");
}
} // namespace Incremental
} // namespace Moses

View File

@ -55,7 +55,6 @@ void ToPhrase(const search::Applied final, Phrase &out);
// Get the phrase and the features.
void PhraseAndFeatures(const search::Applied final, Phrase &phrase, ScoreComponentCollection &features);
const LanguageModel &GetFirstLM();
} // namespace Incremental
} // namespace Moses

View File

@ -90,4 +90,27 @@ void LanguageModel::Evaluate(const Phrase &source
}
}
const LanguageModel &LanguageModel::GetFirstLM()
{
static const LanguageModel *lmStatic = NULL;
if (lmStatic) {
return *lmStatic;
}
// 1st time looking up lm
const std::vector<const StatefulFeatureFunction*> &statefulFFs = StatefulFeatureFunction::GetStatefulFeatureFunctions();
for (size_t i = 0; i < statefulFFs.size(); ++i) {
const StatefulFeatureFunction *ff = statefulFFs[i];
const LanguageModel *lm = dynamic_cast<const LanguageModel*>(ff);
if (lm) {
lmStatic = lm;
return *lmStatic;
}
}
throw std::logic_error("Incremental search only supports one language model.");
}
} // namespace Moses

View File

@ -46,6 +46,8 @@ protected:
bool m_enableOOVFeature;
public:
static const LanguageModel &GetFirstLM();
virtual ~LanguageModel();
bool OOVFeatureEnabled() const {