00001
00002 #pragma once
00003
00004 #include <vector>
00005 #include "Implementation.h"
00006 #include "moses/Hypothesis.h"
00007
00008 namespace DALM
00009 {
00010 class Logger;
00011 class Vocabulary;
00012 class State;
00013 class LM;
00014 union Fragment;
00015 class Gap;
00016
00017 typedef unsigned int VocabId;
00018 }
00019
00020 namespace Moses
00021 {
00022 class Factor;
00023 class DALMChartState;
00024
00025 class LanguageModelDALM : public LanguageModel
00026 {
00027 public:
00028 LanguageModelDALM(const std::string &line);
00029 virtual ~LanguageModelDALM();
00030
00031 void Load(AllOptions::ptr const& opts);
00032
00033 virtual const FFState *EmptyHypothesisState(const InputType &) const;
00034
00035 virtual void CalcScore(const Phrase &phrase, float &fullScore, float &ngramScore, size_t &oovCount) const;
00036
00037 virtual FFState *EvaluateWhenApplied(const Hypothesis &hypo, const FFState *ps, ScoreComponentCollection *out) const;
00038
00039 virtual FFState *EvaluateWhenApplied(const ChartHypothesis& hypo, int featureID, ScoreComponentCollection *out) const;
00040
00041 virtual bool IsUseable(const FactorMask &mask) const;
00042
00043 virtual void SetParameter(const std::string& key, const std::string& value);
00044
00045 protected:
00046 const Factor *m_beginSentenceFactor;
00047
00048 FactorType m_factorType;
00049
00050 std::string m_filePath;
00051 size_t m_nGramOrder;
00052 size_t m_ContextSize;
00053
00054 DALM::Logger *m_logger;
00055 DALM::Vocabulary *m_vocab;
00056 DALM::LM *m_lm;
00057 DALM::VocabId wid_start, wid_end;
00058
00059 mutable std::vector<DALM::VocabId> m_vocabMap;
00060
00061 void CreateVocabMapping(const std::string &wordstxt);
00062 DALM::VocabId GetVocabId(const Factor *factor) const;
00063
00064 private:
00065
00066 DALM::VocabId *LastIDs(const Hypothesis &hypo, DALM::VocabId *indices) const {
00067 DALM::VocabId *index = indices;
00068 DALM::VocabId *end = indices + m_nGramOrder - 1;
00069 int position = hypo.GetCurrTargetWordsRange().GetEndPos();
00070 for (; ; ++index, --position) {
00071 if (index == end) return index;
00072 if (position == -1) {
00073 *index = wid_start;
00074 return index + 1;
00075 }
00076 *index = GetVocabId(hypo.GetWord(position).GetFactor(m_factorType));
00077 }
00078 }
00079
00080 void EvaluateTerminal(
00081 const Word &word,
00082 float &hypoScore,
00083 DALMChartState *newState,
00084 DALM::State &state,
00085 DALM::Fragment *prefixFragments,
00086 unsigned char &prefixLength
00087 ) const;
00088
00089 void EvaluateNonTerminal(
00090 const Word &word,
00091 float &hypoScore,
00092 DALMChartState *newState,
00093 DALM::State &state,
00094 DALM::Fragment *prefixFragments,
00095 unsigned char &prefixLength,
00096 const DALMChartState *prevState,
00097 size_t prevTargetPhraseLength
00098 ) const;
00099 };
00100
00101 }
00102