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 "LanguageModelSingleFactor.h"
00029 #include "LanguageModelMultiFactor.h"
00030 #include "Word.h"
00031 #include "FactorTypeSet.h"
00032 #include "FactorCollection.h"
00033
00034 namespace Moses
00035 {
00036
00037 class Phrase;
00038 class FactorCollection;
00039
00043 class LanguageModelJoint : public LanguageModelMultiFactor
00044 {
00045 protected:
00046 LanguageModelSingleFactor *m_lmImpl;
00047 std::vector<FactorType> m_factorTypesOrdered;
00048
00049 size_t m_implFactor;
00050 public:
00051 LanguageModelJoint(LanguageModelSingleFactor *lmImpl, bool registerScore, ScoreIndexManager &scoreIndexManager)
00052 :LanguageModelMultiFactor(registerScore, scoreIndexManager)
00053 {
00054 m_lmImpl = lmImpl;
00055 }
00056
00057 ~LanguageModelJoint()
00058 {
00059 delete m_lmImpl;
00060 }
00061
00062 bool Load(const std::string &filePath
00063 , const std::vector<FactorType> &factorTypes
00064 , size_t nGramOrder)
00065 {
00066 m_factorTypes = FactorMask(factorTypes);
00067 m_filePath = filePath;
00068 m_nGramOrder = nGramOrder;
00069
00070 m_factorTypesOrdered= factorTypes;
00071 m_implFactor = 0;
00072
00073 FactorCollection &factorCollection = FactorCollection::Instance();
00074
00075
00076 for (size_t index = 0 ; index < factorTypes.size() ; ++index)
00077 {
00078 FactorType factorType = factorTypes[index];
00079 m_sentenceStartArray[factorType] = factorCollection.AddFactor(Output, factorType, BOS_);
00080 m_sentenceEndArray[factorType] = factorCollection.AddFactor(Output, factorType, EOS_);
00081 }
00082
00083 return m_lmImpl->Load(filePath, m_implFactor, nGramOrder);
00084 }
00085
00086 float GetValue(const std::vector<const Word*> &contextFactor, State* finalState = NULL, unsigned int* len = NULL) const
00087 {
00088 if (contextFactor.size() == 0)
00089 {
00090 return 0;
00091 }
00092
00093
00094 std::vector<const Word*> jointContext;
00095
00096 for (size_t currPos = 0 ; currPos < m_nGramOrder ; ++currPos )
00097 {
00098 const Word &word = *contextFactor[currPos];
00099
00100
00101 std::stringstream stream("");
00102
00103 const Factor *factor = word[ m_factorTypesOrdered[0] ];
00104 stream << factor->GetString();
00105
00106 for (size_t index = 1 ; index < m_factorTypesOrdered.size() ; ++index)
00107 {
00108 FactorType factorType = m_factorTypesOrdered[index];
00109 const Factor *factor = word[factorType];
00110 stream << "|" << factor->GetString();
00111 }
00112
00113 factor = FactorCollection::Instance().AddFactor(Output, m_implFactor, stream.str());
00114
00115 Word* jointWord = new Word;
00116 jointWord->SetFactor(m_implFactor, factor);
00117 jointContext.push_back(jointWord);
00118 }
00119
00120
00121 float ret = m_lmImpl->GetValue(jointContext, finalState, len);
00122
00123 RemoveAllInColl(jointContext);
00124
00125 return ret;
00126 }
00127
00128 };
00129
00130 }
00131 #endif