mosesdecoder/moses/LM/InMemoryPerSentenceOnDemandLM.h

167 lines
5.4 KiB
C
Raw Permalink Normal View History

2017-01-02 21:57:52 +03:00
// $Id$
#pragma once
#include <string>
2017-01-02 21:57:52 +03:00
#include <vector>
#include "SingleFactor.h"
#include <boost/thread/tss.hpp>
#include "lm/model.hh"
#include "moses/LM/Ken.h"
#include "moses/FF/FFState.h"
namespace Moses
{
struct InMemoryPerSentenceOnDemandLMState : public FFState {
lm::ngram::State state;
virtual size_t hash() const {
size_t ret = hash_value(state);
return ret;
}
virtual bool operator==(const FFState& o) const {
const InMemoryPerSentenceOnDemandLMState &other = static_cast<const InMemoryPerSentenceOnDemandLMState &>(o);
bool ret = state == other.state;
return ret;
}
};
class InMemoryPerSentenceOnDemandLM : public LanguageModel
{
public:
InMemoryPerSentenceOnDemandLM(const std::string &line);
~InMemoryPerSentenceOnDemandLM();
void InitializeForInput(ttasksptr const& ttask);
virtual void SetParameter(const std::string& key, const std::string& value) {
GetPerThreadLM().SetParameter(key, value);
}
virtual const FFState* EmptyHypothesisState(const InputType &input) const {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
return GetPerThreadLM().EmptyHypothesisState(input);
} else {
return new InMemoryPerSentenceOnDemandLMState();
}
}
virtual FFState *EvaluateWhenApplied(const Hypothesis &hypo, const FFState *ps, ScoreComponentCollection *out) const {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
return GetPerThreadLM().EvaluateWhenApplied(hypo, ps, out);
} else {
UTIL_THROW(util::Exception, "Can't evaluate an uninitialized LM\n");
}
}
virtual FFState *EvaluateWhenApplied(const ChartHypothesis& cur_hypo, int featureID, ScoreComponentCollection *accumulator) const {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
return GetPerThreadLM().EvaluateWhenApplied(cur_hypo, featureID, accumulator);
} else {
UTIL_THROW(util::Exception, "Can't evaluate an uninitialized LM\n");
}
}
virtual FFState *EvaluateWhenApplied(const Syntax::SHyperedge& hyperedge, int featureID, ScoreComponentCollection *accumulator) const {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
return GetPerThreadLM().EvaluateWhenApplied(hyperedge, featureID, accumulator);
} else {
UTIL_THROW(util::Exception, "Can't evaluate an uninitialized LM\n");
}
}
virtual void CalcScore(const Phrase &phrase, float &fullScore, float &ngramScore, std::size_t &oovCount) const {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
GetPerThreadLM().CalcScore(phrase, fullScore, ngramScore, oovCount);
} else {
UTIL_THROW(util::Exception, "WARNING: InMemoryPerSentenceOnDemand::CalcScore called prior to being initialized");
2017-01-02 21:57:52 +03:00
}
}
virtual void CalcScoreFromCache(const Phrase &phrase, float &fullScore, float &ngramScore, std::size_t &oovCount) const {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
GetPerThreadLM().CalcScoreFromCache(phrase, fullScore, ngramScore, oovCount);
} else {
UTIL_THROW(util::Exception, "WARNING: InMemoryPerSentenceOnDemand::CalcScoreFromCache called prior to being initialized");
2017-01-02 21:57:52 +03:00
}
}
virtual void IssueRequestsFor(Hypothesis& hypo, const FFState* input_state) {
if (isInitialized()) {
GetPerThreadLM().IssueRequestsFor(hypo, input_state);
} else {
UTIL_THROW(util::Exception, "WARNING: InMemoryPerSentenceOnDemand::IssueRequestsFor called prior to being initialized");
}
2017-01-02 21:57:52 +03:00
}
virtual void sync() {
if (isInitialized()) {
GetPerThreadLM().sync();
} else {
UTIL_THROW(util::Exception, "WARNING: InMemoryPerSentenceOnDemand::sync called prior to being initialized");
}
2017-01-02 21:57:52 +03:00
}
2017-01-03 03:00:36 +03:00
2017-01-02 21:57:52 +03:00
virtual void SetFFStateIdx(int state_idx) {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
GetPerThreadLM().SetFFStateIdx(state_idx);
} else {
UTIL_THROW(util::Exception, "WARNING: InMemoryPerSentenceOnDemand::SetFFStateIdx called prior to being initialized");
2017-01-02 21:57:52 +03:00
}
}
virtual void IncrementalCallback(Incremental::Manager &manager) const {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
GetPerThreadLM().IncrementalCallback(manager);
} else {
UTIL_THROW(util::Exception, "WARNING: InMemoryPerSentenceOnDemand::IncrementalCallback called prior to being initialized");
2017-01-02 21:57:52 +03:00
}
}
virtual void ReportHistoryOrder(std::ostream &out,const Phrase &phrase) const {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
GetPerThreadLM().ReportHistoryOrder(out, phrase);
} else {
UTIL_THROW(util::Exception, "WARNING: InMemoryPerSentenceOnDemand::ReportHistoryOrder called prior to being initialized");
2017-01-02 21:57:52 +03:00
}
}
2017-01-03 03:00:36 +03:00
2017-01-02 21:57:52 +03:00
virtual void EvaluateInIsolation(const Phrase &source
, const TargetPhrase &targetPhrase
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedScores) const {
if (isInitialized()) {
2017-01-02 21:57:52 +03:00
GetPerThreadLM().EvaluateInIsolation(source, targetPhrase, scoreBreakdown, estimatedScores);
} else {
// UTIL_THROW(util::Exception, "WARNING: InMemoryPerSentenceOnDemand::EvaluateInIsolation called prior to being initialized");
2017-01-02 21:57:52 +03:00
}
}
bool IsUseable(const FactorMask &mask) const {
bool ret = mask[m_factorType];
return ret;
2017-01-02 21:57:52 +03:00
}
protected:
LanguageModelKen<lm::ngram::ProbingModel> & GetPerThreadLM() const;
mutable boost::thread_specific_ptr<LanguageModelKen<lm::ngram::ProbingModel> > m_perThreadLM;
mutable boost::thread_specific_ptr<std::string> m_tmpFilename;
FactorType m_factorType;
2017-01-02 21:57:52 +03:00
bool isInitialized() const {
2017-01-06 03:00:50 +03:00
if (m_tmpFilename.get() == NULL) {
return false;
} else {
return true;
}
}
2017-01-02 21:57:52 +03:00
};
}