-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgramOptions.cpp
271 lines (215 loc) · 8.34 KB
/
ProgramOptions.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* ProgramOptions.cpp
*
* Created on: Dec 10, 2016
* Author: louis
*/
#include <unistd.h>
#include <iomanip>
#include <fstream>
#include <iterator>
#include <algorithm>
#include "Logging.h"
#include "ProgramOptions.h"
#include "Utils.h"
#include "ValueInterpolationStrategy.h"
namespace SLM {
const std::string corpusExtension = "dat";
const std::string patternmodelExtension = "patternmodel";
const std::string vocabularyExtension = "cls";
const std::string languagemodelExtension = "ser";
ProgramOptions::ProgramOptions(int argc, char** argv) {
L_A << "ProgramOptions\t+ Processing program options\n";
//
clp.add<int>("n", 'n', "n", false, 4);
clp.add<std::string>("traindirectory", 'o', "train output directory", true);
clp.add<std::string>("trainmodelname", 'm', "train model name", true);
clp.add<std::string>("traincorpus", '\0', "load colibri encoded corpus", false);
clp.add<std::string>("trainpatternmodel", '\0', "load colibri encoded pattern model", false);
clp.add<std::string>("trainvocabulary", '\0', "load colibri class file", false);
clp.add<std::string>("trainlanguagemodel", '\0', "load SLM language model", false);
clp.add<std::string>("testmodelname", 'M', "test model name", true);
clp.add<std::string>("testinputdirectory", 'I', "test input directory", false);
clp.add<std::string>("testinputfiles", '\0', "file with filename per line", false);
clp.add<std::string>("testoutputdirectory", 'O', "test output directory", true);
clp.add<double>("npref", '\0', "npref value", false, 2.0);
clp.add<std::string>("interpolweights", '\0', "interpolation weights, d-0.5:ab_d-0.1:a_cd-5", false, "");
clp.add<std::string>("backoff", 'B', "backoff method", false);
clp.add("ignorecache", '\0', "ignore cache");
clp.add("addsentencemarkers", '\0', "Add <s> <s> to the beginning (for now it is fixed..)");
clp.add<std::string>("debug", '\0', "debug setting", false, "none");
clp.add("disableprogress", '\0', "disable progress counter");
clp.parse_check(argc, argv);
//
trainDirectory = clp.get<std::string>("traindirectory");
trainModelName = trainDirectory + "/" + clp.get<std::string>("trainmodelname");
trainCorpus = clp.get<std::string>("traincorpus");
trainPatternModel = clp.get<std::string>("trainpatternmodel");
trainVocabulary = clp.get<std::string>("trainvocabulary");
trainLanguageModel = clp.get<std::string>("trainlanguagemodel");
testInputDirectory = clp.get<std::string>("testinputdirectory");
testInputFilesList = clp.get<std::string>("testinputfiles");
testOutputDirectory = clp.get<std::string>("testoutputdirectory");
testModelName = testOutputDirectory + "/" + clp.get<std::string>("testmodelname");
testCorpus = testModelName + "." + corpusExtension;
testPatternModel = testModelName + "." + patternmodelExtension;
testVocabulary = testModelName + "." + vocabularyExtension;
backoffOptions = clp.get<std::string>("backoff");
npref = clp.get<double>("npref");
valuesFromString(clp.get<std::string>("interpolweights"));
ignoreCache = clp.exist("ignorecache");
sentenceMarkers = clp.exist("addsentencemarkers");
disableProgress = clp.exist("disableprogress");
char hostname[128];
gethostname(hostname, sizeof hostname);
hostName = std::string(hostname);
if(!testInputFilesList.empty())
{
std::ifstream inputFiles(testInputFilesList);
std::copy(std::istream_iterator<std::string>(inputFiles),
std::istream_iterator<std::string>(),
back_inserter(testInputFiles));
}
testInputFiles.insert(std::end(testInputFiles), std::begin(clp.rest()), std::end(clp.rest()));
//
if(trainCorpus.empty()) { trainCorpus = trainModelName + "." + corpusExtension; }
if(trainPatternModel.empty()) { trainPatternModel = trainModelName + "." + patternmodelExtension; }
if(trainVocabulary.empty()) { trainVocabulary = trainModelName + "." + vocabularyExtension; }
if(trainLanguageModel.empty()) { trainLanguageModel = trainModelName + "." + languagemodelExtension; }
//
SLM::Logging::getInstance().set(clp.get<std::string>("debug"));
L_V << std::setw(30) << "Log level set on: " << SLM::Logging::getInstance().toString() << "\n"
<< std::setw(30) << "Running on machine: " << hostName << "\n"
<< "\n"
<< std::setw(30) << "Input model directory: " << trainDirectory << "\n"
<< std::setw(30) << "Input model name: " << trainModelName << "\n"
<< "\n"
<< std::setw(30) << "Input corpus file: " << trainCorpus << "\n"
<< std::setw(30) << "Input pattern model file: " << trainPatternModel << "\n"
<< std::setw(30) << "Input vocabulary file: " << trainVocabulary << "\n"
<< std::setw(30) << "Input language model file: " << trainLanguageModel << "\n"
<< "\n"
<< std::setw(30) << "Output model directory: " << testOutputDirectory << "\n"
<< std::setw(30) << "Output model name: " << testModelName << "\n"
<< "\n"
<< std::setw(30) << "Output corpus file: " << testCorpus << "\n"
<< std::setw(30) << "Output pattern model file: " << testPatternModel << "\n"
<< std::setw(30) << "Output vocabulary file: " << testVocabulary << "\n"
<< "\n"
<< std::setw(30) << "Number of test input files: " << testInputFiles.size() << "\n"
<< "\n"
<< std::setw(30) << "Backoff strategies: " << backoffOptions << "\n"
<< std::setw(30) << "Ignore cache: " << (ignoreCache ? "Yes" : "No") << "\n"
<< std::setw(30) << "Add sentence markers: " << (sentenceMarkers ? "Yes" : "No") << "\n"
<< std::setw(30) << "Interpolation weights: " << viWeights.toString() << "\n"
<< std::setw(30) << "Npref ratio: " << npref << "\n";
//
L_A << "ProgramOptions\t- Processing program options\n";
}
ProgramOptions::~ProgramOptions() {
// TODO Auto-generated destructor stub
}
std::string ProgramOptions::getTrainDirectory() const
{
return trainDirectory;
}
std::string ProgramOptions::getTrainModelName() const
{
return trainModelName;
}
std::string ProgramOptions::getTrainCorpus() const
{
return trainCorpus;
}
std::string ProgramOptions::getTrainPatternModel() const
{
return trainPatternModel;
}
std::string ProgramOptions::getTrainVocabulary() const
{
return trainVocabulary;
}
std::string ProgramOptions::getTrainLanguageModel() const
{
return trainLanguageModel;
}
std::string ProgramOptions::getTestModelName() const
{
return testModelName;
}
std::string ProgramOptions::getTestInputDirectory() const
{
return testInputDirectory;
}
std::string ProgramOptions::getTestOutputDirectory() const
{
return testOutputDirectory;
}
std::string ProgramOptions::getTestCorpus() const
{
return testCorpus;
}
std::string ProgramOptions::getTestPatternModel() const
{
return testPatternModel;
}
std::string ProgramOptions::getTestVocabulary() const
{
return testVocabulary;
}
std::vector<std::string> ProgramOptions::getTestInputFiles() const
{
return testInputFiles;
}
int ProgramOptions::getOrder() const
{
return order;
}
bool ProgramOptions::isIgnoreCache() const
{
return ignoreCache;
}
bool ProgramOptions::addSentenceMarkers() const
{
return sentenceMarkers;
}
bool ProgramOptions::isDisableProgress() const
{
return disableProgress;
}
std::string ProgramOptions::getHostName() const
{
return hostName;
}
std::string ProgramOptions::getBackoffOptions() const
{
return backoffOptions;
}
double ProgramOptions::getNpref() const
{
return npref;
}
SLM::ValueInterpolationWeights ProgramOptions::valuesFromString(const std::string& vals)
{ // d - cd - bcd - b_d - abcd - a_cd - ab_d - a__d
std::vector<std::string> tokens = delimiterTokeniser(vals, '@');
for(std::string token : tokens)
{
std::vector<std::string> keyval = delimiterTokeniser(token, '-');
if(keyval.size() != 2 )
continue;
if(keyval[0] == "d") { viWeights.w_d = std::stod(keyval[1]); }
if(keyval[0] == "cd") { viWeights.w_cd = std::stod(keyval[1]); }
if(keyval[0] == "b_d") { viWeights.w_b_d = std::stod(keyval[1]); }
if(keyval[0] == "abcd") { viWeights.w_abcd = std::stod(keyval[1]); }
if(keyval[0] == "a_cd") { viWeights.w_a_cd = std::stod(keyval[1]); }
if(keyval[0] == "ab_d") { viWeights.w_ab_d = std::stod(keyval[1]); }
if(keyval[0] == "a__d") { viWeights.w_a__d = std::stod(keyval[1]); }
if(keyval[0] == "bcd") { viWeights.w_bcd = std::stod(keyval[1]); }
}
return viWeights;
}
SLM::ValueInterpolationWeights& ProgramOptions::getValues()
{
return viWeights;
}
} /* namespace SLM */