|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef moses_LanguageModelKen_h |
|
#define moses_LanguageModelKen_h |
|
|
|
#include <string> |
|
#include <boost/shared_ptr.hpp> |
|
|
|
#include "lm/word_index.hh" |
|
#include "util/mmap.hh" |
|
|
|
#include "moses/LM/Base.h" |
|
#include "moses/Hypothesis.h" |
|
#include "moses/TypeDef.h" |
|
#include "moses/Word.h" |
|
|
|
|
|
|
|
namespace Moses |
|
{ |
|
|
|
|
|
class FFState; |
|
class InMemoryPerSentenceOnDemandLM; |
|
|
|
LanguageModel *ConstructKenLM(const std::string &line); |
|
|
|
|
|
LanguageModel *ConstructKenLM(const std::string &line, const std::string &file, FactorType factorType, util::LoadMethod load_method); |
|
|
|
|
|
|
|
|
|
template <class Model> class LanguageModelKen : public LanguageModel |
|
{ |
|
public: |
|
LanguageModelKen(const std::string &line, const std::string &file, FactorType factorType, util::LoadMethod load_method); |
|
|
|
virtual const FFState *EmptyHypothesisState(const InputType &) const; |
|
|
|
virtual void CalcScore(const Phrase &phrase, float &fullScore, float &ngramScore, size_t &oovCount) const; |
|
|
|
virtual FFState *EvaluateWhenApplied(const Hypothesis &hypo, const FFState *ps, ScoreComponentCollection *out) const; |
|
|
|
virtual FFState *EvaluateWhenApplied(const ChartHypothesis& cur_hypo, int featureID, ScoreComponentCollection *accumulator) const; |
|
|
|
virtual FFState *EvaluateWhenApplied(const Syntax::SHyperedge& hyperedge, int featureID, ScoreComponentCollection *accumulator) const; |
|
|
|
virtual void IncrementalCallback(Incremental::Manager &manager) const; |
|
virtual void ReportHistoryOrder(std::ostream &out,const Phrase &phrase) const; |
|
|
|
virtual bool IsUseable(const FactorMask &mask) const; |
|
|
|
friend class InMemoryPerSentenceOnDemandLM; |
|
|
|
protected: |
|
boost::shared_ptr<Model> m_ngram; |
|
|
|
const Factor *m_beginSentenceFactor; |
|
|
|
FactorType m_factorType; |
|
|
|
void LoadModel(const std::string &file, util::LoadMethod load_method); |
|
|
|
lm::WordIndex TranslateID(const Word &word) const { |
|
std::size_t factor = word.GetFactor(m_factorType)->GetId(); |
|
return (factor >= m_lmIdLookup.size() ? 0 : m_lmIdLookup[factor]); |
|
} |
|
|
|
std::vector<lm::WordIndex> m_lmIdLookup; |
|
|
|
private: |
|
LanguageModelKen(); |
|
LanguageModelKen(const LanguageModelKen<Model> ©_from); |
|
|
|
|
|
lm::WordIndex *LastIDs(const Hypothesis &hypo, lm::WordIndex *indices) const { |
|
lm::WordIndex *index = indices; |
|
lm::WordIndex *end = indices + m_ngram->Order() - 1; |
|
int position = hypo.GetCurrTargetWordsRange().GetEndPos(); |
|
for (; ; ++index, --position) { |
|
if (index == end) return index; |
|
if (position == -1) { |
|
*index = m_ngram->GetVocabulary().BeginSentence(); |
|
return index + 1; |
|
} |
|
*index = TranslateID(hypo.GetWord(position)); |
|
} |
|
} |
|
|
|
|
|
protected: |
|
|
|
}; |
|
|
|
} |
|
|
|
#endif |
|
|