00001 #include <boost/foreach.hpp> 00002 #include "InMemoryPerSentenceOnDemandLM.h" 00003 #include "moses/FactorCollection.h" 00004 #include "moses/Util.h" 00005 #include "moses/StaticData.h" 00006 #include "moses/TranslationTask.h" 00007 #include "moses/ContextScope.h" 00008 #include "moses/LM/Ken.h" 00009 #include "lm/model.hh" 00010 #include "util/mmap.hh" 00011 00012 #include <cstdio> 00013 #include <iostream> 00014 #include <fstream> 00015 00016 using namespace std; 00017 00018 namespace Moses 00019 { 00020 InMemoryPerSentenceOnDemandLM::InMemoryPerSentenceOnDemandLM(const std::string &line) : LanguageModel(line) 00021 { 00022 ReadParameters(); 00023 } 00024 00025 InMemoryPerSentenceOnDemandLM::~InMemoryPerSentenceOnDemandLM() 00026 { 00027 } 00028 00029 void InMemoryPerSentenceOnDemandLM::InitializeForInput(ttasksptr const& ttask) 00030 { 00031 00032 // The context scope object for this translation task 00033 // contains a map of translation task-specific data 00034 boost::shared_ptr<Moses::ContextScope> contextScope = ttask->GetScope(); 00035 00036 // The key to the map is this object 00037 void const* key = static_cast<void const*>(this); 00038 00039 // The value stored in the map is a string representing a phrase table 00040 boost::shared_ptr<string> value = contextScope->get<string>(key); 00041 00042 // Create a stream to read the phrase table data 00043 stringstream strme(*(value.get())); 00044 00045 char * nullpointer = (char *) 0; 00046 const char * filename = std::tmpnam(nullpointer); 00047 ofstream tmp; 00048 tmp.open(filename); 00049 00050 // Read the phrase table data, one line at a time 00051 string line; 00052 while (getline(strme, line)) { 00053 00054 tmp << line << "\n"; 00055 00056 } 00057 00058 tmp.close(); 00059 00060 // m_tmpFilename.reset(new std::string("/home/lanes/mosesdecoder/tiny.with_per_sentence/europarl.en.srilm")); 00061 m_tmpFilename.reset(new std::string(filename)); 00062 00063 //LanguageModelKen<lm::ngram::ProbingModel> & lm = 00064 GetPerThreadLM(); 00065 00066 // std::remove(filename); 00067 00068 } 00069 00070 LanguageModelKen<lm::ngram::ProbingModel>& InMemoryPerSentenceOnDemandLM::GetPerThreadLM() const 00071 { 00072 00073 LanguageModelKen<lm::ngram::ProbingModel> *lm; 00074 lm = m_perThreadLM.get(); 00075 if (lm == NULL) { 00076 lm = new LanguageModelKen<lm::ngram::ProbingModel>(); 00077 00078 string* filename = m_tmpFilename.get(); 00079 if (filename == NULL) { 00080 UTIL_THROW(util::Exception, "Can't get a thread-specific LM because no temporary filename has been set for this thread\n"); 00081 } else { 00082 lm->LoadModel(*filename, util::POPULATE_OR_READ); 00083 } 00084 00085 VERBOSE(1, filename); 00086 VERBOSE(1, "\tLM initialized\n"); 00087 00088 m_perThreadLM.reset(lm); 00089 } 00090 assert(lm); 00091 00092 return *lm; 00093 00094 } 00095 00096 00097 00098 } 00099 00100 00101