lattice-utils-test.cc
11.5 KB
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
// fstext/lattice-utils-test.cc
// Copyright 2011 Microsoft Corporation
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "fstext/lattice-utils.h"
#include "fstext/fst-test-utils.h"
#include "base/kaldi-math.h"
namespace fst {
template<class Weight, class Int> void TestConvert(bool invert) {
typedef ArcTpl<Weight> Arc;
typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
for(int i = 0; i < 5; i++) {
VectorFst<Arc> *fst = RandFst<Arc>();
std::cout << "FST before converting to compact-arc is:\n";
{
FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
fstprinter.Print(&std::cout, "standard output");
}
VectorFst<CompactArc> ofst;
ConvertLattice<Weight, Int>(*fst, &ofst, invert);
std::cout << "FST after converting is:\n";
{
FstPrinter<CompactArc> fstprinter(ofst, NULL, NULL, NULL, false, true, "\t");
fstprinter.Print(&std::cout, "standard output");
}
VectorFst<Arc> origfst;
ConvertLattice<Weight, Int>(ofst, &origfst, invert);
std::cout << "FST after back conversion is:\n";
{
FstPrinter<Arc> fstprinter(origfst, NULL, NULL, NULL, false, true, "\t");
fstprinter.Print(&std::cout, "standard output");
}
assert(RandEquivalent(*fst, origfst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
delete fst;
}
}
// This tests the ShortestPath algorithm, and by proxy, tests the
// NaturalLess template etc.
template<class Weight, class Int> void TestShortestPath() {
for (int p = 0; p < 10; p++) {
typedef ArcTpl<Weight> Arc;
typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
for(int i = 0; i < 5; i++) {
VectorFst<Arc> *fst = RandPairFst<Arc>();
std::cout << "Testing shortest path\n";
std::cout << "FST before converting to compact-arc is:\n";
{
FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
fstprinter.Print(&std::cout, "standard output");
}
VectorFst<CompactArc> cfst;
ConvertLattice<Weight, Int>(*fst, &cfst, false); // invert == false
{
VectorFst<Arc> nbest_fst_1;
ShortestPath(*fst, &nbest_fst_1, 1);
VectorFst<Arc> nbest_fst_2;
ShortestPath(*fst, &nbest_fst_2, 3);
VectorFst<Arc> nbest_fst_1b;
ShortestPath(nbest_fst_2, &nbest_fst_1b, 1);
assert(ApproxEqual(ShortestDistance(nbest_fst_1),
ShortestDistance(nbest_fst_1b)));
// since semiring is idempotent, this should succeed too.
assert(ApproxEqual(ShortestDistance(*fst),
ShortestDistance(nbest_fst_1b)));
}
{
VectorFst<CompactArc> nbest_fst_1;
ShortestPath(cfst, &nbest_fst_1, 1);
VectorFst<CompactArc> nbest_fst_2;
ShortestPath(cfst, &nbest_fst_2, 3);
VectorFst<CompactArc> nbest_fst_1b;
ShortestPath(nbest_fst_2, &nbest_fst_1b, 1);
assert(ApproxEqual(ShortestDistance(nbest_fst_1),
ShortestDistance(nbest_fst_1b)));
// since semiring is idempotent, this should succeed too.
assert(ApproxEqual(ShortestDistance(cfst),
ShortestDistance(nbest_fst_1b)));
}
delete fst;
}
}
}
template<class Int> void TestConvert2() {
typedef ArcTpl<LatticeWeightTpl<float> > ArcF;
typedef ArcTpl<LatticeWeightTpl<double> > ArcD;
typedef ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > CArcF;
typedef ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > CArcD;
for(int i = 0; i < 2; i++) {
{
VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
VectorFst<ArcD> fst2;
VectorFst<ArcF> fst3;
ConvertLattice(*fst1, &fst2);
ConvertLattice(fst2, &fst3);
assert(RandEquivalent(*fst1, fst3, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
delete fst1;
}
{
VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
VectorFst<CArcF> cfst1, cfst3;
ConvertLattice(*fst1, &cfst1);
VectorFst<CArcD> cfst2;
ConvertLattice(cfst1, &cfst2);
ConvertLattice(cfst2, &cfst3);
assert(RandEquivalent(cfst1, cfst3, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
delete fst1;
}
{
VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
VectorFst<CArcD> cfst1, cfst3;
ConvertLattice(*fst1, &cfst1);
VectorFst<CArcF> cfst2;
ConvertLattice(cfst1, &cfst2);
ConvertLattice(cfst2, &cfst3);
assert(RandEquivalent(cfst1, cfst3, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
delete fst1;
}
{
VectorFst<ArcD> *fst1 = RandPairFst<ArcD>();
VectorFst<CArcD> cfst1, cfst3;
ConvertLattice(*fst1, &cfst1);
VectorFst<CArcF> cfst2;
ConvertLattice(cfst1, &cfst2);
ConvertLattice(cfst2, &cfst3);
assert(RandEquivalent(cfst1, cfst3, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
delete fst1;
}
{
VectorFst<ArcD> *fst1 = RandPairFst<ArcD>();
VectorFst<CArcF> cfst1;
ConvertLattice(*fst1, &cfst1);
VectorFst<ArcD> fst2;
ConvertLattice(cfst1, &fst2);
assert(RandEquivalent(*fst1, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
delete fst1;
}
{
VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
VectorFst<CArcD> cfst1;
ConvertLattice(*fst1, &cfst1);
VectorFst<ArcF> fst2;
ConvertLattice(cfst1, &fst2);
assert(RandEquivalent(*fst1, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
delete fst1;
}
{
VectorFst<ArcD> *fst1 = RandPairFst<ArcD>();
VectorFst<CArcF> cfst1;
ConvertLattice(*fst1, &cfst1);
VectorFst<ArcD> fst2;
ConvertLattice(cfst1, &fst2);
assert(RandEquivalent(*fst1, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
delete fst1;
}
}
}
// use TestConvertPair when the Weight can be constructed from
// a pair of floats.
template<class Weight, class Int> void TestConvertPair(bool invert) {
typedef ArcTpl<Weight> Arc;
typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
for(int i = 0; i < 2; i++) {
VectorFst<Arc> *fst = RandPairFst<Arc>();
/*std::cout << "FST before converting to compact-arc is:\n";
{
FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true);
fstprinter.Print(&std::cout, "standard output");
}*/
VectorFst<CompactArc> ofst;
ConvertLattice<Weight, Int>(*fst, &ofst, invert);
/*std::cout << "FST after converting is:\n";
{
FstPrinter<CompactArc> fstprinter(ofst, NULL, NULL, NULL, false, true);
fstprinter.Print(&std::cout, "standard output");
}*/
VectorFst<Arc> origfst;
ConvertLattice<Weight, Int>(ofst, &origfst, invert);
/*std::cout << "FST after back conversion is:\n";
{
FstPrinter<Arc> fstprinter(origfst, NULL, NULL, NULL, false, true);
fstprinter.Print(&std::cout, "standard output");
}*/
assert(RandEquivalent(*fst, origfst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
delete fst;
}
}
// use TestConvertPair when the Weight can be constructed from
// a pair of floats.
template<class Weight, class Int> void TestScalePair(bool invert) {
vector<vector<double> > scale1 = DefaultLatticeScale(),
scale2 = DefaultLatticeScale();
// important that all these numbers exactly representable as floats..
// exact floating-point comparisons are used in LatticeWeight, and
// this exactness is being tested here.. this test will fail for
// other types of number.
if (kaldi::Rand() % 4 == 0) {
scale1[0][0] = 2.0;
scale2[0][0] = 0.5;
scale1[1][1] = 4.0;
scale2[1][1] = 0.25;
} else if (kaldi::Rand() % 3 == 0) {
// use that [1 0.25; 0 1] [ 1 -0.25; 0 1] is the unit matrix.
scale1[0][1] = 0.25;
scale2[0][1] = -0.25;
} else if (kaldi::Rand() % 2 == 0) {
scale1[1][0] = 0.25;
scale2[1][0] = -0.25;
}
typedef ArcTpl<Weight> Arc;
typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
for(int i = 0; i < 2; i++) {
VectorFst<Arc> *fst = RandPairFst<Arc>();
/*std::cout << "FST before converting to compact-arc is:\n";
{
FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true);
fstprinter.Print(&std::cout, "standard output");
}*/
VectorFst<CompactArc> ofst;
ConvertLattice<Weight, Int>(*fst, &ofst, invert);
ScaleLattice(scale1, &ofst);
/*std::cout << "FST after converting and scaling is:\n";
{
FstPrinter<CompactArc> fstprinter(ofst, NULL, NULL, NULL, false, true);
fstprinter.Print(&std::cout, "standard output");
}*/
VectorFst<Arc> origfst;
ConvertLattice<Weight, Int>(ofst, &origfst, invert);
ScaleLattice(scale2, &origfst);
/*std::cout << "FST after back conversion and scaling is:\n";
{
FstPrinter<Arc> fstprinter(origfst, NULL, NULL, NULL, false, true);
fstprinter.Print(&std::cout, "standard output");
}*/
// If RandEquivalent doesn't work, it could be due to a nasty issue related to the use
// of exact floating-point comparisons in the Plus function of LatticeWeight.
if (!RandEquivalent(*fst, origfst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/)) {
std::cerr << "Warn, randequivalent returned false. Checking equivalence another way.\n";
assert(Equal(*fst, origfst));
}
delete fst;
}
}
} // end namespace fst
int main() {
using namespace fst;
typedef ::int64 int64;
typedef ::uint64 uint64;
typedef ::int32 int32;
typedef ::uint32 uint32;
{
typedef LatticeWeightTpl<float> LatticeWeight;
for(int i = 0; i < 2; i++) {
bool invert = (i % 2);
TestConvert<TropicalWeight, int32>(invert);
TestConvertPair<LatticeWeight, int32>(invert);
TestConvertPair<LatticeWeight, size_t>(invert);
TestConvertPair<LexicographicWeight<TropicalWeight, TropicalWeight>, size_t>(invert);
TestScalePair<LatticeWeight, int32>(invert);
TestScalePair<LatticeWeight, size_t>(invert);
TestScalePair<LexicographicWeight<TropicalWeight, TropicalWeight>, size_t>(invert);
}
}
{
typedef LatticeWeightTpl<double> LatticeWeight;
TestShortestPath<LatticeWeight, int32>();
TestConvert2<int32>();
for(int i = 0; i < 2; i++) {
bool invert = (i % 2);
TestConvertPair<LatticeWeight, int32>(invert);
TestConvertPair<LatticeWeight, size_t>(invert);
TestConvertPair<LexicographicWeight<TropicalWeight, TropicalWeight>, size_t>(invert);
TestScalePair<LatticeWeight, int32>(invert);
TestScalePair<LatticeWeight, size_t>(invert);
TestScalePair<LexicographicWeight<TropicalWeight, TropicalWeight>, size_t>(invert);
}
}
std::cout << "Tests succeeded\n";
}