Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StaticData.h « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 600030b0b8e01f8842fa1e40f86af771435c1d10 (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// $Id$

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#pragma once

#include <list>
#include <vector>
#include <map>
#include <memory>
#include "TypeDef.h"
#include "ScoreIndexManager.h"
#include "FactorCollection.h"
#include "Parameter.h"
#include "LanguageModel.h"
#include "LMList.h"
#include "SentenceStats.h"
#include "DecodeGraph.h"
//#include "UnknownWordHandler.h"

class InputType;
class LexicalReordering;
class PhraseDictionary;
class GenerationDictionary;
class DistortionScoreProducer;
class WordPenaltyProducer;
class DecodeStep;
class UnknownWordPenaltyProducer;

/** Contains global variables and contants */
class StaticData
{
private:
	static StaticData									s_instance;
protected:	
	std::vector<PhraseDictionary*>	m_phraseDictionary;
	std::vector<GenerationDictionary*>	m_generationDictionary;
	std::vector <DecodeGraph*>		m_decodeStepVL;
	Parameter			*m_parameter;
	std::vector<FactorType>			m_inputFactorOrder, m_outputFactorOrder;
	LMList									m_languageModel;
	ScoreIndexManager				m_scoreIndexManager;
	std::vector<float>			m_allWeights;
	std::vector<LexicalReordering*>                   m_reorderModels;
		// Initial	= 0 = can be used when creating poss trans
		// Other		= 1 = used to calculate LM score once all steps have been processed
	float
		m_beamWidth,
		m_weightDistortion, 
		m_weightWordPenalty, 
		m_wordDeletionWeight,
		m_weightUnknownWord;
									// PhraseTrans, Generation & LanguageModelScore has multiple weights.
	int																	m_maxDistortion;
									// do it differently from old pharaoh
									// -ve	= no limit on distortion
									// 0		= no disortion (monotone in old pharaoh)
	size_t                              
			m_maxHypoStackSize //hypothesis-stack size that triggers pruning
			, m_nBestSize
			, m_nBestFactor
			, m_maxNoTransOptPerCoverage
			, m_maxNoPartTransOpt
			, m_maxPhraseLength;
	
	std::string									m_nBestFilePath;
	bool                        m_fLMsLoaded, m_labeledNBestList,m_nBestIncludesAlignment;
	/***
	 * false = treat unknown words as unknowns, and translate them as themselves;
	 * true = drop (ignore) them
	 */
	bool m_dropUnknown;
	bool m_wordDeletionEnabled;

	bool m_sourceStartPosMattersForRecombination;
	bool m_recoverPath;

	InputTypeEnum m_inputType;
	size_t m_numInputScores;

	mutable size_t m_verboseLevel;
	DistortionScoreProducer *m_distortionScoreProducer;
	WordPenaltyProducer *m_wpProducer;
	UnknownWordPenaltyProducer *m_unknownWordPenaltyProducer;
	bool m_reportSegmentation;
	bool m_reportAllFactors;
	bool m_useDistortionFutureCosts;
	bool m_isDetailedTranslationReportingEnabled;
	bool m_onlyDistinctNBest;
	bool m_computeLMBackoffStats;

	mutable std::auto_ptr<SentenceStats> m_sentenceStats;
	std::string m_factorDelimiter; //! by default, |, but it can be changed
	size_t m_maxFactorIdx[2];  //! number of factors on source and target side
	size_t m_maxNumFactors;  //! max number of factors on both source and target sides
	
	XmlInputType m_xmlInputType; //! method for handling sentence XML input

	bool m_mbr; //! use MBR decoder
	size_t m_mbrSize; //! number of translation candidates considered
	float m_mbrScale; //! scaling factor for computing marginal probability of candidate translation

	bool m_useTransOptCache;
	mutable std::map<Phrase, TranslationOptionList> m_transOptCache;

	mutable const InputType* m_input;  //! holds reference to current sentence
	bool m_isAlwaysCreateDirectTranslationOption;
	//! constructor. only the 1 static variable can be created
	StaticData();

	//! helper fn to set bool param from ini file/command line
	void SetBooleanParameter(bool *paramter, string parameterName, bool defaultValue);

	/***
	 * load all language models as specified in ini file
	 */
	bool LoadLanguageModels();
	/***
	 * load not only the main phrase table but also any auxiliary tables that depend on which features are being used
	 * (eg word-deletion, word-insertion tables)
	 */
	bool LoadPhraseTables();
	//! load all generation tables as specified in ini file
	bool LoadGenerationTables();
	//! load decoding steps
	bool LoadMapping();
	bool LoadLexicalReorderingModel();
	
public:

	bool IsAlwaysCreateDirectTranslationOption() const {
		return m_isAlwaysCreateDirectTranslationOption;
	}
	//! destructor
	~StaticData();
	//! return static instance for use like global variable
	static const StaticData& Instance() { return s_instance; }
	
	/** delete current static instance and replace with another. 
		* Used by gui front end
		*/
	#ifdef WIN32
	static void Reset() { s_instance = StaticData(); }
	#endif
	
	/** load data into static instance. This function is required
		* as LoadData() is not const
		*/
	static bool LoadDataStatic(Parameter *parameter)
	{
		return s_instance.LoadData(parameter);
	}

	/** Main function to load everything.
	 * Also initialize the Parameter object
	 */
	bool LoadData(Parameter *parameter);

	const PARAM_VEC &GetParam(const std::string &paramName) const
	{
		return m_parameter->GetParam(paramName);
	}

	bool IsComputeLMBackoffStats() const
	{
		return m_computeLMBackoffStats;
	}
	const std::vector<FactorType> &GetInputFactorOrder() const
	{
		return m_inputFactorOrder;
	}
	const std::vector<FactorType> &GetOutputFactorOrder() const
	{
		return m_outputFactorOrder;
	}

	const std::vector<DecodeGraph*> &GetDecodeStepVL() const
	{
		return m_decodeStepVL;
	}
	
	inline bool GetSourceStartPosMattersForRecombination() const
	{ 
		return m_sourceStartPosMattersForRecombination; 
	}
	inline bool GetDropUnknown() const 
	{ 
		return m_dropUnknown; 
	}
	inline size_t GetMaxNoTransOptPerCoverage() const 
	{ 
		return m_maxNoTransOptPerCoverage;
	}
	inline size_t GetMaxNoPartTransOpt() const 
	{ 
		return m_maxNoPartTransOpt;
	}
	inline size_t GetMaxPhraseLength() const 
	{ 
		return m_maxPhraseLength;
	}
	std::vector<LexicalReordering*> GetReorderModels() const
	{
		return m_reorderModels;
	}
	float GetWeightDistortion() const
	{
		return m_weightDistortion;
	}
	float GetWeightWordPenalty() const
	{
		return m_weightWordPenalty;
	}
	bool IsWordDeletionEnabled() const
	{
		return m_wordDeletionEnabled;
	}
	size_t GetMaxHypoStackSize() const
	{
		return m_maxHypoStackSize;
	}
	size_t IsPathRecoveryEnabled() const
	{
		return m_recoverPath;
	}
	int GetMaxDistortion() const
	{
		return m_maxDistortion;
	}
	float GetBeamWidth() const
	{
		return m_beamWidth;
	}
	//! returns the total number of score components across all types, all factors
	size_t GetTotalScoreComponents() const
	{
		return m_scoreIndexManager.GetTotalNumberOfScores();
	}
	const ScoreIndexManager& GetScoreIndexManager() const
	{
		return m_scoreIndexManager;
	}

	size_t GetLMSize() const
	{
		return m_languageModel.size();
	}
	const LMList &GetAllLM() const
	{
		return m_languageModel;
	}
	size_t GetPhraseDictionarySize() const
	{
		return m_phraseDictionary.size();
	}
	std::vector<PhraseDictionary*> GetPhraseDictionaries() const
	{
		return m_phraseDictionary;
	}
	std::vector<GenerationDictionary*> GetGenerationDictionaries() const
	{
		return m_generationDictionary;
	}
	size_t GetGenerationDictionarySize() const
	{
		return m_generationDictionary.size();
	}
	size_t GetVerboseLevel() const
	{
		return m_verboseLevel;
	}
	void SetVerboseLevel(int x) const { m_verboseLevel = x; }
	bool GetReportSegmentation() const
	{
		return m_reportSegmentation;
	}
	bool GetReportAllFactors() const
	{
		return m_reportAllFactors;
	}
	bool IsDetailedTranslationReportingEnabled() const
	{
		return m_isDetailedTranslationReportingEnabled;
	}
	void ResetSentenceStats(const InputType& source) const
	{
		m_sentenceStats = std::auto_ptr<SentenceStats>(new SentenceStats(source));
	}
	bool IsLabeledNBestList() const
	{
		return m_labeledNBestList;
	}
	bool NBestIncludesAlignment() const
	{
		return m_nBestIncludesAlignment;
	}
	const std::vector<std::string> &GetDescription() const
	{
		return m_parameter->GetParam("description");
	}

	// for mert
	size_t GetNBestSize() const
	{
		return m_nBestSize;
	}
	const std::string &GetNBestFilePath() const
	{
		return m_nBestFilePath;
	}
  	bool IsNBestEnabled() const {
	  return (!m_nBestFilePath.empty()) || m_mbr;
  	}
	size_t GetNBestFactor() const
	{
		return m_nBestFactor;
	}

	//! Sets the global score vector weights for a given ScoreProducer.
	void SetWeightsForScoreProducer(const ScoreProducer* sp, const std::vector<float>& weights);
	InputTypeEnum GetInputType() const {return m_inputType;}
	size_t GetNumInputScores() const {return m_numInputScores;}
	const InputType* GetInput() const { return m_input; }
	void InitializeBeforeSentenceProcessing(InputType const&) const;
	void CleanUpAfterSentenceProcessing() const;
	SentenceStats& GetSentenceStats() const
	{
		return *m_sentenceStats;
	}
	const std::vector<float>& GetAllWeights() const
	{
		return m_allWeights;
	}
	const DistortionScoreProducer *GetDistortionScoreProducer() const { return m_distortionScoreProducer; }
	const WordPenaltyProducer *GetWordPenaltyProducer() const { return m_wpProducer; }
	const UnknownWordPenaltyProducer *GetUnknownWordPenaltyProducer() const { return m_unknownWordPenaltyProducer; }

	bool UseDistortionFutureCosts() const {return m_useDistortionFutureCosts;}
	bool GetDistinctNBest() const {return m_onlyDistinctNBest;}
	const std::string& GetFactorDelimiter() const {return m_factorDelimiter;}
	size_t GetMaxNumFactors(FactorDirection direction) const { return m_maxFactorIdx[(size_t)direction]+1; }
	size_t GetMaxNumFactors() const { return m_maxNumFactors; }
	size_t UseMBR() const { return m_mbr; }
	size_t GetMBRSize() const { return m_mbrSize; }
	float GetMBRScale() const { return m_mbrScale; }
		
	XmlInputType GetXmlInputType() const { return m_xmlInputType; }

	bool GetUseTransOptCache() const { return m_useTransOptCache; }

	void AddTransOptListToCache(const Phrase &sourcePhrase, const TranslationOptionList &transOptList) const
	{
		m_transOptCache[sourcePhrase] = transOptList;
	}

	const TranslationOptionList* FindTransOptListInCache(const Phrase &sourcePhrase) const;
};