2011-05-13 23:28:23 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
2011-05-13 23:28:23 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
public:
|
2011-05-13 23:28:23 +04:00
|
|
|
|
|
|
|
SyntacticLanguageModelFiles(const std::vector<std::string>& filePaths);
|
|
|
|
~SyntacticLanguageModelFiles();
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-05-13 23:28:23 +04:00
|
|
|
MH* getHiddenModel();
|
|
|
|
MO* getObservedModel();
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
private:
|
2011-05-13 23:28:23 +04:00
|
|
|
MH* hiddenModel;
|
|
|
|
MO* observedModel;
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-05-13 23:28:23 +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)
|
|
|
|
{
|
2011-05-13 23:28:23 +04:00
|
|
|
|
|
|
|
this->hiddenModel = new MH();
|
|
|
|
this->observedModel = new MO();
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-05-13 23:28:23 +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
|
2013-11-19 22:52:15 +04:00
|
|
|
FILE* pf = fopen(filePaths[a].c_str(),"r"); // Read model file
|
2013-05-29 21:16:15 +04:00
|
|
|
if(!pf) {
|
2011-05-13 23:28:23 +04:00
|
|
|
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
|
2011-05-13 23:28:23 +04:00
|
|
|
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";
|
2011-05-13 23:28:23 +04:00
|
|
|
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()
|
|
|
|
{
|
2011-05-13 23:28:23 +04:00
|
|
|
|
2012-03-31 00:27:11 +04:00
|
|
|
VERBOSE(3,"Destructing syntactic language model files" << std::endl);
|
2012-03-31 00:28:22 +04:00
|
|
|
delete hiddenModel;
|
|
|
|
delete observedModel;
|
2011-05-13 23:28:23 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class MH, class MO>
|
2013-05-29 21:16:15 +04:00
|
|
|
MH* SyntacticLanguageModelFiles<MH,MO>::getHiddenModel()
|
|
|
|
{
|
|
|
|
|
2011-05-13 23:28:23 +04:00
|
|
|
return this->hiddenModel;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class MH, class MO>
|
2013-05-29 21:16:15 +04:00
|
|
|
MO* SyntacticLanguageModelFiles<MH,MO>::getObservedModel()
|
|
|
|
{
|
|
|
|
|
2011-05-13 23:28:23 +04:00
|
|
|
return this->observedModel;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|