2017-01-02 21:57:52 +03:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include "InMemoryPerSentenceOnDemandLM.h"
|
|
|
|
#include "moses/FactorCollection.h"
|
|
|
|
#include "moses/Util.h"
|
|
|
|
#include "moses/StaticData.h"
|
|
|
|
#include "moses/TranslationTask.h"
|
|
|
|
#include "moses/ContextScope.h"
|
|
|
|
#include "moses/LM/Ken.h"
|
|
|
|
#include "lm/model.hh"
|
|
|
|
#include "util/mmap.hh"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
2017-01-07 23:36:41 +03:00
|
|
|
InMemoryPerSentenceOnDemandLM::InMemoryPerSentenceOnDemandLM(const std::string &line) : LanguageModel(line)
|
2017-01-02 21:57:52 +03:00
|
|
|
{
|
|
|
|
ReadParameters();
|
|
|
|
}
|
|
|
|
|
|
|
|
InMemoryPerSentenceOnDemandLM::~InMemoryPerSentenceOnDemandLM()
|
|
|
|
{
|
|
|
|
}
|
2017-01-06 03:00:50 +03:00
|
|
|
|
2017-01-03 03:00:36 +03:00
|
|
|
void InMemoryPerSentenceOnDemandLM::InitializeForInput(ttasksptr const& ttask)
|
|
|
|
{
|
2017-01-02 21:57:52 +03:00
|
|
|
|
|
|
|
// The context scope object for this translation task
|
|
|
|
// contains a map of translation task-specific data
|
|
|
|
boost::shared_ptr<Moses::ContextScope> contextScope = ttask->GetScope();
|
|
|
|
|
|
|
|
// The key to the map is this object
|
|
|
|
void const* key = static_cast<void const*>(this);
|
|
|
|
|
|
|
|
// The value stored in the map is a string representing a phrase table
|
|
|
|
boost::shared_ptr<string> value = contextScope->get<string>(key);
|
|
|
|
|
|
|
|
// Create a stream to read the phrase table data
|
|
|
|
stringstream strme(*(value.get()));
|
|
|
|
|
|
|
|
char * nullpointer = (char *) 0;
|
|
|
|
const char * filename = std::tmpnam(nullpointer);
|
|
|
|
ofstream tmp;
|
|
|
|
tmp.open(filename);
|
|
|
|
|
|
|
|
// Read the phrase table data, one line at a time
|
|
|
|
string line;
|
|
|
|
while (getline(strme, line)) {
|
|
|
|
|
|
|
|
tmp << line << "\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp.close();
|
|
|
|
|
2017-01-06 00:48:30 +03:00
|
|
|
// m_tmpFilename.reset(new std::string("/home/lanes/mosesdecoder/tiny.with_per_sentence/europarl.en.srilm"));
|
|
|
|
m_tmpFilename.reset(new std::string(filename));
|
2017-01-02 21:57:52 +03:00
|
|
|
|
2017-01-06 03:00:50 +03:00
|
|
|
//LanguageModelKen<lm::ngram::ProbingModel> & lm =
|
2017-01-06 00:48:30 +03:00
|
|
|
GetPerThreadLM();
|
2017-01-02 21:57:52 +03:00
|
|
|
|
|
|
|
// std::remove(filename);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-03 03:00:36 +03:00
|
|
|
LanguageModelKen<lm::ngram::ProbingModel>& InMemoryPerSentenceOnDemandLM::GetPerThreadLM() const
|
|
|
|
{
|
2017-01-02 21:57:52 +03:00
|
|
|
|
|
|
|
LanguageModelKen<lm::ngram::ProbingModel> *lm;
|
|
|
|
lm = m_perThreadLM.get();
|
|
|
|
if (lm == NULL) {
|
|
|
|
lm = new LanguageModelKen<lm::ngram::ProbingModel>();
|
2017-01-06 00:48:30 +03:00
|
|
|
|
|
|
|
string* filename = m_tmpFilename.get();
|
|
|
|
if (filename == NULL) {
|
|
|
|
UTIL_THROW(util::Exception, "Can't get a thread-specific LM because no temporary filename has been set for this thread\n");
|
|
|
|
} else {
|
|
|
|
lm->LoadModel(*filename, util::POPULATE_OR_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
VERBOSE(1, filename);
|
2017-01-06 03:00:50 +03:00
|
|
|
VERBOSE(1, "\tLM initialized\n");
|
2017-01-06 00:48:30 +03:00
|
|
|
|
2017-01-02 21:57:52 +03:00
|
|
|
m_perThreadLM.reset(lm);
|
|
|
|
}
|
|
|
|
assert(lm);
|
2017-01-06 00:48:30 +03:00
|
|
|
|
2017-01-02 21:57:52 +03:00
|
|
|
return *lm;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|