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) {
00052 m_lmImpl = lmImpl;
00053 }
00054
00055 ~LanguageModelJoint() {
00056 delete m_lmImpl;
00057 }
00058
00059 bool Load(const std::string &filePath
00060 , const std::vector<FactorType> &factorTypes
00061 , size_t nGramOrder) {
00062 m_factorTypes = FactorMask(factorTypes);
00063 m_filePath = filePath;
00064 m_nGramOrder = nGramOrder;
00065
00066 m_factorTypesOrdered= factorTypes;
00067 m_implFactor = 0;
00068
00069 FactorCollection &factorCollection = FactorCollection::Instance();
00070
00071
00072 for (size_t index = 0 ; index < factorTypes.size() ; ++index) {
00073 FactorType factorType = factorTypes[index];
00074 m_sentenceStartArray[factorType] = factorCollection.AddFactor(Output, factorType, BOS_);
00075 m_sentenceEndArray[factorType] = factorCollection.AddFactor(Output, factorType, EOS_);
00076 }
00077
00078 return m_lmImpl->Load(filePath, m_implFactor, nGramOrder);
00079 }
00080
00081 LMResult GetValueForgotState(const std::vector<const Word*> &contextFactor, FFState &outState) const {
00082 if (contextFactor.size() == 0) {
00083 LMResult ret;
00084 ret.score = 0.0;
00085 ret.unknown = false;
00086 return ret;
00087 }
00088
00089
00090 std::vector<const Word*> jointContext;
00091
00092 for (size_t currPos = 0 ; currPos < m_nGramOrder ; ++currPos ) {
00093 const Word &word = *contextFactor[currPos];
00094
00095
00096 std::stringstream stream("");
00097
00098 const Factor *factor = word[ m_factorTypesOrdered[0] ];
00099 stream << factor->GetString();
00100
00101 for (size_t index = 1 ; index < m_factorTypesOrdered.size() ; ++index) {
00102 FactorType factorType = m_factorTypesOrdered[index];
00103 const Factor *factor = word[factorType];
00104 stream << "|" << factor->GetString();
00105 }
00106
00107 factor = FactorCollection::Instance().AddFactor(Output, m_implFactor, stream.str());
00108
00109 Word* jointWord = new Word;
00110 jointWord->SetFactor(m_implFactor, factor);
00111 jointContext.push_back(jointWord);
00112 }
00113
00114
00115 LMResult ret = m_lmImpl->GetValueForgotState(jointContext, outState);
00116
00117 RemoveAllInColl(jointContext);
00118
00119 return ret;
00120 }
00121
00122 const FFState *GetNullContextState() const {
00123 return m_lmImpl->GetNullContextState();
00124 }
00125
00126 const FFState *GetBeginSentenceState() const {
00127 return m_lmImpl->GetBeginSentenceState();
00128 }
00129
00130 FFState *NewState(const FFState *from) const {
00131 return m_lmImpl->NewState(from);
00132 }
00133
00134 };
00135
00136 }
00137 #endif