1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
//
#ifndef moses_SyntacticLanguageModelFiles_h
#define moses_SyntacticLanguageModelFiles_h
#include "nl-iomacros.h"
#include "nl-string.h"
namespace Moses
{
template <class MH, class MO>
class SyntacticLanguageModelFiles {
public:
SyntacticLanguageModelFiles(const std::vector<std::string>& filePaths);
~SyntacticLanguageModelFiles();
MH* getHiddenModel();
MO* getObservedModel();
private:
MH* hiddenModel;
MO* observedModel;
};
template <class MH, class MO>
SyntacticLanguageModelFiles<MH,MO>::SyntacticLanguageModelFiles(const std::vector<std::string>& filePaths) {
this->hiddenModel = new MH();
this->observedModel = new MO();
//// 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"); //assert(pf); // Read model file
if(!pf){
std::cerr << "Error loading model file " << filePaths[a] << std::endl;
return;
}
std::cerr << "Loading model \'" << filePaths[a] << "\'...\n";
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
|| 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>
SyntacticLanguageModelFiles<MH,MO>::~SyntacticLanguageModelFiles() {
std::cerr<<"Destructing syntactic language model files" << std::endl;
//delete hiddenModel;
//delete observedModel;
}
template <class MH, class MO>
MH* SyntacticLanguageModelFiles<MH,MO>::getHiddenModel() {
return this->hiddenModel;
}
template <class MH, class MO>
MO* SyntacticLanguageModelFiles<MH,MO>::getObservedModel() {
return this->observedModel;
}
}
#endif
|