|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef moses_LanguageModelJoint_h |
|
#define moses_LanguageModelJoint_h |
|
|
|
#include <vector> |
|
#include <string> |
|
#include <sstream> |
|
#include "SingleFactor.h" |
|
#include "MultiFactor.h" |
|
#include "moses/Word.h" |
|
#include "moses/FactorTypeSet.h" |
|
#include "moses/FactorCollection.h" |
|
|
|
namespace Moses |
|
{ |
|
|
|
class Phrase; |
|
class FactorCollection; |
|
|
|
|
|
|
|
|
|
|
|
class LanguageModelJoint : public LanguageModelMultiFactor |
|
{ |
|
protected: |
|
LanguageModelSingleFactor *m_lmImpl; |
|
std::vector<FactorType> m_factorTypesOrdered; |
|
|
|
size_t m_implFactor; |
|
public: |
|
LanguageModelJoint(const std::string &line, LanguageModelSingleFactor *lmImpl) |
|
:LanguageModelMultiFactor(line) { |
|
m_lmImpl = lmImpl; |
|
} |
|
|
|
~LanguageModelJoint() { |
|
delete m_lmImpl; |
|
} |
|
|
|
bool Load(AllOptions const& opts, const std::string &filePath |
|
, const std::vector<FactorType> &factorTypes |
|
, size_t nGramOrder) { |
|
m_factorTypes = FactorMask(factorTypes); |
|
m_filePath = filePath; |
|
m_nGramOrder = nGramOrder; |
|
|
|
m_factorTypesOrdered= factorTypes; |
|
m_implFactor = 0; |
|
|
|
FactorCollection &factorCollection = FactorCollection::Instance(); |
|
|
|
|
|
for (size_t index = 0 ; index < factorTypes.size() ; ++index) { |
|
FactorType factorType = factorTypes[index]; |
|
m_sentenceStartWord[factorType] = factorCollection.AddFactor(Output, factorType, BOS_); |
|
m_sentenceEndWord[factorType] = factorCollection.AddFactor(Output, factorType, EOS_); |
|
} |
|
|
|
m_lmImpl->Load(AllOptions const& opts); |
|
} |
|
|
|
LMResult GetValueForgotState(const std::vector<const Word*> &contextFactor, FFState &outState) const { |
|
if (contextFactor.size() == 0) { |
|
LMResult ret; |
|
ret.score = 0.0; |
|
ret.unknown = false; |
|
return ret; |
|
} |
|
|
|
|
|
std::vector<const Word*> jointContext; |
|
|
|
for (size_t currPos = 0 ; currPos < m_nGramOrder ; ++currPos ) { |
|
const Word &word = *contextFactor[currPos]; |
|
|
|
|
|
std::stringstream stream(""); |
|
|
|
const Factor *factor = word[ m_factorTypesOrdered[0] ]; |
|
stream << factor->GetString(); |
|
|
|
for (size_t index = 1 ; index < m_factorTypesOrdered.size() ; ++index) { |
|
FactorType factorType = m_factorTypesOrdered[index]; |
|
const Factor *factor = word[factorType]; |
|
stream << "|" << factor->GetString(); |
|
} |
|
|
|
factor = FactorCollection::Instance().AddFactor(Output, m_implFactor, stream.str()); |
|
|
|
Word* jointWord = new Word; |
|
jointWord->SetFactor(m_implFactor, factor); |
|
jointContext.push_back(jointWord); |
|
} |
|
|
|
|
|
LMResult ret = m_lmImpl->GetValueForgotState(jointContext, outState); |
|
|
|
RemoveAllInColl(jointContext); |
|
|
|
return ret; |
|
} |
|
|
|
const FFState *GetNullContextState() const { |
|
return m_lmImpl->GetNullContextState(); |
|
} |
|
|
|
const FFState *GetBeginSentenceState() const { |
|
return m_lmImpl->GetBeginSentenceState(); |
|
} |
|
|
|
FFState *NewState(const FFState *from) const { |
|
return m_lmImpl->NewState(from); |
|
} |
|
|
|
}; |
|
|
|
} |
|
#endif |
|
|