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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
// $Id$
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (c) 2006 University of Edinburgh
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Edinburgh nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
***********************************************************************/
// example file on how to use moses library
#ifdef WIN32
// Include Visual Leak Detector
//#include <vld.h>
#endif
#include <exception>
#include <fstream>
#include "Main.h"
#include "FactorCollection.h"
#include "Manager.h"
#include "Phrase.h"
#include "Util.h"
#include "Timer.h"
#include "IOWrapper.h"
#include "Sentence.h"
#include "ConfusionNet.h"
#include "WordLattice.h"
#include "TreeInput.h"
#include "TranslationAnalysis.h"
#include "mbr.h"
#include "ThreadPool.h"
#include "ChartManager.h"
#include "ChartHypothesis.h"
#include "ChartTrellisPath.h"
#include "ChartTrellisPathList.h"
#include "util/usage.hh"
using namespace std;
using namespace Moses;
using namespace MosesChartCmd;
/**
* Translates a sentence.
**/
class TranslationTask : public Task
{
public:
TranslationTask(InputType *source, IOWrapper &ioWrapper)
: m_source(source)
, m_ioWrapper(ioWrapper)
{}
~TranslationTask() {
delete m_source;
}
void Run() {
const StaticData &staticData = StaticData::Instance();
const TranslationSystem &system = staticData.GetTranslationSystem(TranslationSystem::DEFAULT);
const size_t lineNumber = m_source->GetTranslationId();
VERBOSE(2,"\nTRANSLATING(" << lineNumber << "): " << *m_source);
if ((*m_source).GetSize() == 0) return;
ChartManager manager(*m_source, &system);
manager.ProcessSentence();
CHECK(!staticData.UseMBR());
// 1-best
const ChartHypothesis *bestHypo = manager.GetBestHypothesis();
m_ioWrapper.OutputBestHypo(bestHypo, lineNumber,
staticData.GetReportSegmentation(),
staticData.GetReportAllFactors());
IFVERBOSE(2) {
PrintUserTime("Best Hypothesis Generation Time:");
}
if (staticData.IsDetailedTranslationReportingEnabled()) {
const Sentence &sentence = dynamic_cast<const Sentence &>(*m_source);
m_ioWrapper.OutputDetailedTranslationReport(bestHypo, sentence, lineNumber);
}
// n-best
size_t nBestSize = staticData.GetNBestSize();
if (nBestSize > 0) {
VERBOSE(2,"WRITING " << nBestSize << " TRANSLATION ALTERNATIVES TO " << staticData.GetNBestFilePath() << endl);
ChartTrellisPathList nBestList;
manager.CalcNBest(nBestSize, nBestList,staticData.GetDistinctNBest());
m_ioWrapper.OutputNBestList(nBestList, bestHypo, &system, lineNumber);
IFVERBOSE(2) {
PrintUserTime("N-Best Hypotheses Generation Time:");
}
}
if (staticData.GetOutputSearchGraph()) {
std::ostringstream out;
manager.GetSearchGraph(lineNumber, out);
OutputCollector *oc = m_ioWrapper.GetSearchGraphOutputCollector();
CHECK(oc);
oc->Write(lineNumber, out.str());
}
IFVERBOSE(2) {
PrintUserTime("Sentence Decoding Time:");
}
manager.CalcDecoderStatistics();
}
private:
// Non-copyable: copy constructor and assignment operator not implemented.
TranslationTask(const TranslationTask &);
TranslationTask &operator=(const TranslationTask &);
InputType *m_source;
IOWrapper &m_ioWrapper;
};
bool ReadInput(IOWrapper &ioWrapper, InputTypeEnum inputType, InputType*& source)
{
delete source;
switch(inputType) {
case SentenceInput:
source = ioWrapper.GetInput(new Sentence);
break;
case ConfusionNetworkInput:
source = ioWrapper.GetInput(new ConfusionNet);
break;
case WordLatticeInput:
source = ioWrapper.GetInput(new WordLattice);
break;
case TreeInputType:
source = ioWrapper.GetInput(new TreeInput);
break;
default:
TRACE_ERR("Unknown input type: " << inputType << "\n");
}
return (source ? true : false);
}
static void PrintFeatureWeight(const FeatureFunction* ff)
{
size_t numScoreComps = ff->GetNumScoreComponents();
if (numScoreComps != ScoreProducer::unlimited) {
vector<float> values = StaticData::Instance().GetAllWeights().GetScoresForProducer(ff);
for (size_t i = 0; i < numScoreComps; ++i)
cout << ff->GetScoreProducerDescription() << " "
<< ff->GetScoreProducerWeightShortName() << " "
<< values[i] << endl;
}
else {
if (ff->GetSparseProducerWeight() == 1)
cout << ff->GetScoreProducerDescription() << " " <<
ff->GetScoreProducerWeightShortName() << " sparse" << endl;
else
cout << ff->GetScoreProducerDescription() << " " <<
ff->GetScoreProducerWeightShortName() << " " << ff->GetSparseProducerWeight() << endl;
}
}
static void ShowWeights()
{
cout.precision(6);
const StaticData& staticData = StaticData::Instance();
const TranslationSystem& system = staticData.GetTranslationSystem(TranslationSystem::DEFAULT);
const vector<const StatelessFeatureFunction*>& slf =system.GetStatelessFeatureFunctions();
const vector<const StatefulFeatureFunction*>& sff = system.GetStatefulFeatureFunctions();
for (size_t i = 0; i < sff.size(); ++i) {
PrintFeatureWeight(sff[i]);
}
for (size_t i = 0; i < slf.size(); ++i) {
if (slf[i]->GetScoreProducerWeightShortName() != "u") {
PrintFeatureWeight(slf[i]);
}
}
}
int main(int argc, char* argv[])
{
try {
IFVERBOSE(1) {
TRACE_ERR("command: ");
for(int i=0; i<argc; ++i) TRACE_ERR(argv[i]<<" ");
TRACE_ERR(endl);
}
IOWrapper::FixPrecision(cout);
IOWrapper::FixPrecision(cerr);
// load data structures
Parameter parameter;
if (!parameter.LoadParam(argc, argv)) {
return EXIT_FAILURE;
}
const StaticData &staticData = StaticData::Instance();
if (!StaticData::LoadDataStatic(¶meter, argv[0]))
return EXIT_FAILURE;
if (parameter.isParamSpecified("show-weights")) {
ShowWeights();
exit(0);
}
CHECK(staticData.GetSearchAlgorithm() == ChartDecoding);
// set up read/writing class
IOWrapper *ioWrapper = GetIOWrapper(staticData);
// check on weights
const ScoreComponentCollection& weights = staticData.GetAllWeights();
IFVERBOSE(2) {
TRACE_ERR("The global weight vector looks like this: ");
TRACE_ERR(weights);
TRACE_ERR("\n");
}
if (ioWrapper == NULL)
return EXIT_FAILURE;
#ifdef WITH_THREADS
ThreadPool pool(staticData.ThreadCount());
#endif
// read each sentence & decode
InputType *source=0;
while(ReadInput(*ioWrapper,staticData.GetInputType(),source)) {
IFVERBOSE(1)
ResetUserTime();
TranslationTask *task = new TranslationTask(source, *ioWrapper);
source = NULL; // task will delete source
#ifdef WITH_THREADS
pool.Submit(task); // pool will delete task
#else
task->Run();
delete task;
#endif
}
#ifdef WITH_THREADS
pool.Stop(true); // flush remaining jobs
#endif
delete ioWrapper;
IFVERBOSE(1)
PrintUserTime("End.");
} catch (const std::exception &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
IFVERBOSE(1) util::PrintUsage(std::cerr);
#ifdef HACK_EXIT
//This avoids that detructors are called (it can take a long time)
exit(EXIT_SUCCESS);
#else
return EXIT_SUCCESS;
#endif
}
IOWrapper *GetIOWrapper(const StaticData &staticData)
{
IOWrapper *ioWrapper;
const std::vector<FactorType> &inputFactorOrder = staticData.GetInputFactorOrder()
,&outputFactorOrder = staticData.GetOutputFactorOrder();
FactorMask inputFactorUsed(inputFactorOrder);
// io
if (staticData.GetParam("input-file").size() == 1) {
VERBOSE(2,"IO from File" << endl);
string filePath = staticData.GetParam("input-file")[0];
ioWrapper = new IOWrapper(inputFactorOrder, outputFactorOrder, inputFactorUsed
, staticData.GetNBestSize()
, staticData.GetNBestFilePath()
, filePath);
} else {
VERBOSE(1,"IO from STDOUT/STDIN" << endl);
ioWrapper = new IOWrapper(inputFactorOrder, outputFactorOrder, inputFactorUsed
, staticData.GetNBestSize()
, staticData.GetNBestFilePath());
}
ioWrapper->ResetTranslationId();
IFVERBOSE(1)
PrintUserTime("Created input-output object");
return ioWrapper;
}
|