mosesdecoder/moses/SyntacticLanguageModelFiles.h

104 lines
2.7 KiB
C
Raw Normal View History

//
#ifndef moses_SyntacticLanguageModelFiles_h
#define moses_SyntacticLanguageModelFiles_h
#include "nl-iomacros.h"
#include "nl-string.h"
namespace Moses
{
2013-05-29 21:16:15 +04:00
template <class MH, class MO>
class SyntacticLanguageModelFiles
{
2013-05-29 21:16:15 +04:00
public:
SyntacticLanguageModelFiles(const std::vector<std::string>& filePaths);
~SyntacticLanguageModelFiles();
2013-05-29 21:16:15 +04:00
MH* getHiddenModel();
MO* getObservedModel();
2013-05-29 21:16:15 +04:00
private:
MH* hiddenModel;
MO* observedModel;
2013-05-29 21:16:15 +04:00
};
template <class MH, class MO>
2013-05-29 21:16:15 +04:00
SyntacticLanguageModelFiles<MH,MO>::SyntacticLanguageModelFiles(const std::vector<std::string>& filePaths)
{
this->hiddenModel = new MH();
this->observedModel = new MO();
2013-05-29 21:16:15 +04:00
//// I. LOAD MODELS...
std::cerr << "Reading syntactic language model files...\n";
// For each model file...
for ( int a=0, n=filePaths.size(); a<n; a++ ) { // read models
FILE* pf = fopen(filePaths[a].c_str(),"r"); //CHECK(pf); // Read model file
2013-05-29 21:16:15 +04:00
if(!pf) {
std::cerr << "Error loading model file " << filePaths[a] << std::endl;
return;
}
std::cerr << "Loading model \'" << filePaths[a] << "\'...\n";
2013-05-29 21:16:15 +04:00
int c=' ';
int i=0;
int line=1;
String sBuff(1000); // Lookahead/ctrs/buffers
CONSUME_ALL ( pf, c, WHITESPACE(c), line); // Get to first record
while ( c!=-1 && c!='\0' && c!='\5' ) { // For each record
CONSUME_STR ( pf, c, (c!='\n' && c!='\0' && c!='\5'), sBuff, i, line ); // Consume line
StringInput si(sBuff.c_array());
if ( !( sBuff[0]=='#' // Accept comments/fields
2013-05-29 21:16:15 +04:00
|| si>>*(this->hiddenModel)>>"\0"!=NULL
|| si>>*(this->observedModel)>>"\0"!=NULL
))
std::cerr<<"\nERROR: can't parse \'"<<sBuff<<"\' in line "<<line<<"\n\n";
CONSUME_ALL ( pf, c, WHITESPACE(c), line); // Consume whitespace
if ( line%100000==0 ) std::cerr<<" "<<line<<" lines read...\n"; // Progress for big models
}
std::cerr << "Model \'" << filePaths[a] << "\' loaded.\n";
}
std::cerr << "...reading syntactic language model files completed\n";
}
template <class MH, class MO>
2013-05-29 21:16:15 +04:00
SyntacticLanguageModelFiles<MH,MO>::~SyntacticLanguageModelFiles()
{
VERBOSE(3,"Destructing syntactic language model files" << std::endl);
delete hiddenModel;
delete observedModel;
}
template <class MH, class MO>
2013-05-29 21:16:15 +04:00
MH* SyntacticLanguageModelFiles<MH,MO>::getHiddenModel()
{
return this->hiddenModel;
}
template <class MH, class MO>
2013-05-29 21:16:15 +04:00
MO* SyntacticLanguageModelFiles<MH,MO>::getObservedModel()
{
return this->observedModel;
}
}
#endif