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
|
// $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
***********************************************************************/
#include "LM/ParallelBackoff.h"
#include "File.h"
#include "TypeDef.h"
#include "Util.h"
#include "FNgramSpecs.h"
#include "FNgramStats.h"
#include "FactoredVocab.h"
#include "FNgram.h"
#include "wmatrix.h"
#include "Vocab.h"
using namespace std;
namespace Moses
{
LanguageModelParallelBackoff::~LanguageModelParallelBackoff()
{
///
}
bool LanguageModelParallelBackoff::Load(const std::string &filePath, const std::vector<FactorType> &factorTypes, size_t nGramOrder)
{
cerr << "Loading Language Model Parallel Backoff!!!\n";
widMatrix = new ::WidMatrix();
m_factorTypes = FactorMask(factorTypes);
m_srilmVocab = new ::FactoredVocab();
//assert(m_srilmVocab != 0);
fnSpecs = 0;
File f(filePath.c_str(),"r");
fnSpecs = new ::FNgramSpecs<FNgramCount>(f,*m_srilmVocab, 0/*debug*/);
cerr << "Loaded fnSpecs!\n";
m_srilmVocab->unkIsWord() = true;
m_srilmVocab->nullIsWord() = true;
m_srilmVocab->toLower() = false;
FNgramStats *factoredStats = new FNgramStats(*m_srilmVocab, *fnSpecs);
factoredStats->debugme(2);
cerr << "Factored stats\n";
FNgram* fngramLM = new FNgram(*m_srilmVocab,*fnSpecs);
cerr << "FNgram object created\n";
fngramLM->skipOOVs = false;
if (!factoredStats->read()) {
cerr << "error reading in counts in factor file\n";
exit(1);
}
cerr << "Factored stats read!\n";
factoredStats->estimateDiscounts();
factoredStats->computeCardinalityFunctions();
factoredStats->sumCounts();
cerr << "Another three operations made!\n";
if (!fngramLM->read()) {
cerr << "format error in lm file\n";
exit(1);
}
cerr << "fngramLM reads!\n";
m_filePath = filePath;
m_nGramOrder= nGramOrder;
m_factorTypesOrdered= factorTypes;
m_unknownId = m_srilmVocab->unkIndex();
cerr << "m_unknowdId = " << m_unknownId << endl;
m_srilmModel = fngramLM;
cerr << "Create factors...\n";
CreateFactors();
cerr << "Factors created! \n";
//FactorCollection &factorCollection = FactorCollection::Instance();
/*for (size_t index = 0 ; index < m_factorTypesOrdered.size() ; ++index)
{
FactorType factorType = m_factorTypesOrdered[index];
m_sentenceStartArray[factorType] = factorCollection.AddFactor(Output, factorType, BOS_);
m_sentenceEndArray[factorType] = factorCollection.AddFactor(Output, factorType, EOS_);
//factorIdStart = m_sentenceStartArray[factorType]->GetId();
//factorIdEnd = m_sentenceEndArray[factorType]->GetId();
for (size_t i = 0; i < 10; i++)
{
lmIdMap[factorIdStart * 10 + i] = GetLmID(BOS_);
lmIdMap[factorIdEnd * 10 + i] = GetLmID(EOS_);
}
//(*lmIdMap)[factorIdStart * 10 + index] = GetLmID(BOS_);
//(*lmIdMap)[factorIdEnd * 10 + index] = GetLmID(EOS_);
}*/
return true;
}
VocabIndex LanguageModelParallelBackoff::GetLmID( const std::string &str ) const
{
return m_srilmVocab->getIndex( str.c_str(), m_unknownId );
}
VocabIndex LanguageModelParallelBackoff::GetLmID( const Factor *factor, size_t ft ) const
{
size_t factorId = factor->GetId();
if ( lmIdMap->find( factorId * 10 + ft ) != lmIdMap->end() ) {
return lmIdMap->find( factorId * 10 + ft )->second;
} else {
return m_unknownId;
}
}
void LanguageModelParallelBackoff::CreateFactors()
{
// add factors which have srilm id
FactorCollection &factorCollection = FactorCollection::Instance();
lmIdMap = new std::map<size_t, VocabIndex>();
VocabString str;
VocabIter iter(*m_srilmVocab);
iter.init();
size_t pomFactorTypeNum = 0;
while ( (str = iter.next()) != NULL) {
if ((str[0] < 'a' || str[0] > 'k') && str[0] != 'W') {
continue;
}
VocabIndex lmId = GetLmID(str);
pomFactorTypeNum = str[0] - 'a';
size_t factorId = factorCollection.AddFactor(Output, m_factorTypesOrdered[pomFactorTypeNum], &(str[2]) )->GetId();
(*lmIdMap)[factorId * 10 + pomFactorTypeNum] = lmId;
}
size_t factorIdStart;
size_t factorIdEnd;
// sentence markers
for (size_t index = 0 ; index < m_factorTypesOrdered.size() ; ++index) {
FactorType factorType = m_factorTypesOrdered[index];
m_sentenceStartArray[index] = factorCollection.AddFactor(Output, factorType, BOS_);
m_sentenceEndArray[index] = factorCollection.AddFactor(Output, factorType, EOS_);
factorIdStart = m_sentenceStartArray[index]->GetId();
factorIdEnd = m_sentenceEndArray[index]->GetId();
/*for (size_t i = 0; i < 10; i++)
{
lmIdMap[factorIdStart * 10 + i] = GetLmID(BOS_);
lmIdMap[factorIdEnd * 10 + i] = GetLmID(EOS_);
}*/
(*lmIdMap)[factorIdStart * 10 + index] = GetLmID(BOS_);
(*lmIdMap)[factorIdEnd * 10 + index] = GetLmID(EOS_);
cerr << "BOS_:" << GetLmID(BOS_) << ", EOS_:" << GetLmID(EOS_) << endl;
}
m_wtid = GetLmID("W-<unk>");
m_wtbid = GetLmID("W-<s>");
m_wteid = GetLmID("W-</s>");
cerr << "W-<unk> index: " << m_wtid << endl;
cerr << "W-<s> index: " << m_wtbid << endl;
cerr << "W-</s> index: " << m_wteid << endl;
}
LMResult LanguageModelParallelBackoff::GetValueForgotState(const std::vector<const Word*> &contextFactor, FFState & /*outState */) const
{
static WidMatrix widMatrix;
for (int i=0; i<contextFactor.size(); i++)
::memset(widMatrix[i],0,(m_factorTypesOrdered.size() + 1)*sizeof(VocabIndex));
for (size_t i = 0; i < contextFactor.size(); i++) {
const Word &word = *contextFactor[i];
for (size_t j = 0; j < m_factorTypesOrdered.size(); j++) {
const Factor *factor = word[ m_factorTypesOrdered[j] ];
if (factor == NULL)
widMatrix[i][j + 1] = 0;
else
widMatrix[i][j + 1] = GetLmID(factor, j);
}
if (widMatrix[i][1] == GetLmID(m_sentenceStartArray[0], 0) ) {
widMatrix[i][0] = m_wtbid;
} else if (widMatrix[i][1] == GetLmID(m_sentenceEndArray[0], 0 )) {
widMatrix[i][0] = m_wteid;
} else {
widMatrix[i][0] = m_wtid;
}
}
LMResult ret;
ret.score = m_srilmModel->wordProb( widMatrix, contextFactor.size() - 1, contextFactor.size() );
ret.score = FloorScore(TransformLMScore(ret.score));
ret.unknown = !contextFactor.empty() && (widMatrix[contextFactor.size() - 1][0] == m_unknownId);
return ret;
/*if (contextFactor.size() == 0)
{
return 0;
}
for (size_t currPos = 0 ; currPos < m_nGramOrder ; ++currPos )
{
const Word &word = *contextFactor[currPos];
for (size_t index = 0 ; index < m_factorTypesOrdered.size() ; ++index)
{
FactorType factorType = m_factorTypesOrdered[index];
const Factor *factor = word[factorType];
(*widMatrix)[currPos][index] = GetLmID(factor, index);
}
}
float p = m_srilmModel->wordProb( (*widMatrix), m_nGramOrder - 1, m_nGramOrder );
return FloorScore(TransformLMScore(p)); */
}
// The old version did not initialize finalState like it should. Technically that makes the behavior undefined, so it's not clear what else to do here.
FFState *LanguageModelParallelBackoff::NewState(const FFState * /*from*/) const
{
return NULL;
}
const FFState *LanguageModelParallelBackoff::GetNullContextState() const
{
return NULL;
}
const FFState *LanguageModelParallelBackoff::GetBeginSentenceState() const
{
return NULL;
}
}
|