#include "search/rule.hh" #include "search/context.hh" #include "search/final.hh" #include #include namespace search { template void Rule::Init(const Context &context, Score additive, const std::vector &words, bool prepend_bos) { additive_ = additive; Score lm_score = 0.0; lexical_.clear(); const lm::WordIndex oov = context.LanguageModel().GetVocabulary().NotFound(); for (std::vector::const_iterator word = words.begin(); ; ++word) { lexical_.resize(lexical_.size() + 1); lm::ngram::RuleScore scorer(context.LanguageModel(), lexical_.back()); // TODO: optimize if (prepend_bos && (word == words.begin())) { scorer.BeginSentence(); } for (; ; ++word) { if (word == words.end()) { lm_score += scorer.Finish(); bound_ = additive_ + context.GetWeights().LM() * lm_score; arity_ = lexical_.size() - 1; return; } if (*word == kNonTerminal) break; if (*word == oov) additive_ += context.GetWeights().OOV(); scorer.Terminal(*word); } lm_score += scorer.Finish(); } } template void Rule::Init(const Context &context, Score additive, const std::vector &words, bool prepend_bos); template void Rule::Init(const Context &context, Score additive, const std::vector &words, bool prepend_bos); } // namespace search