00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef moses_LanguageModelJoint_h
00023 #define moses_LanguageModelJoint_h
00024
00025 #include <vector>
00026 #include <string>
00027 #include <sstream>
00028 #include "SingleFactor.h"
00029 #include "MultiFactor.h"
00030 #include "moses/Word.h"
00031 #include "moses/FactorTypeSet.h"
00032 #include "moses/FactorCollection.h"
00033
00034 namespace Moses
00035 {
00036
00037 class Phrase;
00038 class FactorCollection;
00039
00044 class LanguageModelJoint : public LanguageModelMultiFactor
00045 {
00046 protected:
00047 LanguageModelSingleFactor *m_lmImpl;
00048 std::vector<FactorType> m_factorTypesOrdered;
00049
00050 size_t m_implFactor;
00051 public:
00052 LanguageModelJoint(const std::string &line, LanguageModelSingleFactor *lmImpl)
00053 :LanguageModelMultiFactor("JointLM", line) {
00054 m_lmImpl = lmImpl;
00055 }
00056
00057 ~LanguageModelJoint() {
00058 delete m_lmImpl;
00059 }
00060
00061 bool Load(const std::string &filePath
00062 , const std::vector<FactorType> &factorTypes
00063 , size_t nGramOrder) {
00064 m_factorTypes = FactorMask(factorTypes);
00065 m_filePath = filePath;
00066 m_nGramOrder = nGramOrder;
00067
00068 m_factorTypesOrdered= factorTypes;
00069 m_implFactor = 0;
00070
00071 FactorCollection &factorCollection = FactorCollection::Instance();
00072
00073
00074 for (size_t index = 0 ; index < factorTypes.size() ; ++index) {
00075 FactorType factorType = factorTypes[index];
00076 m_sentenceStartWord[factorType] = factorCollection.AddFactor(Output, factorType, BOS_);
00077 m_sentenceEndWord[factorType] = factorCollection.AddFactor(Output, factorType, EOS_);
00078 }
00079
00080 m_lmImpl->Load();
00081 }
00082
00083 LMResult GetValueForgotState(const std::vector<const Word*> &contextFactor, FFState &outState) const {
00084 if (contextFactor.size() == 0) {
00085 LMResult ret;
00086 ret.score = 0.0;
00087 ret.unknown = false;
00088 return ret;
00089 }
00090
00091
00092 std::vector<const Word*> jointContext;
00093
00094 for (size_t currPos = 0 ; currPos < m_nGramOrder ; ++currPos ) {
00095 const Word &word = *contextFactor[currPos];
00096
00097
00098 std::stringstream stream("");
00099
00100 const Factor *factor = word[ m_factorTypesOrdered[0] ];
00101 stream << factor->GetString();
00102
00103 for (size_t index = 1 ; index < m_factorTypesOrdered.size() ; ++index) {
00104 FactorType factorType = m_factorTypesOrdered[index];
00105 const Factor *factor = word[factorType];
00106 stream << "|" << factor->GetString();
00107 }
00108
00109 factor = FactorCollection::Instance().AddFactor(Output, m_implFactor, stream.str());
00110
00111 Word* jointWord = new Word;
00112 jointWord->SetFactor(m_implFactor, factor);
00113 jointContext.push_back(jointWord);
00114 }
00115
00116
00117 LMResult ret = m_lmImpl->GetValueForgotState(jointContext, outState);
00118
00119 RemoveAllInColl(jointContext);
00120
00121 return ret;
00122 }
00123
00124 const FFState *GetNullContextState() const {
00125 return m_lmImpl->GetNullContextState();
00126 }
00127
00128 const FFState *GetBeginSentenceState() const {
00129 return m_lmImpl->GetBeginSentenceState();
00130 }
00131
00132 FFState *NewState(const FFState *from) const {
00133 return m_lmImpl->NewState(from);
00134 }
00135
00136 };
00137
00138 }
00139 #endif