26 template<
class Weight,
class Int>
void TestConvert(
bool invert) {
27 typedef ArcTpl<Weight>
Arc;
28 typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
29 for(
int i = 0;
i < 5;
i++) {
30 VectorFst<Arc> *
fst = RandFst<Arc>();
31 std::cout <<
"FST before converting to compact-arc is:\n";
33 FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL,
false,
true,
"\t");
34 fstprinter.Print(&std::cout,
"standard output");
36 VectorFst<CompactArc> ofst;
37 ConvertLattice<Weight, Int>(*fst, &ofst, invert);
39 std::cout <<
"FST after converting is:\n";
41 FstPrinter<CompactArc> fstprinter(ofst, NULL, NULL, NULL,
false,
true,
"\t");
42 fstprinter.Print(&std::cout,
"standard output");
44 VectorFst<Arc> origfst;
45 ConvertLattice<Weight, Int>(ofst, &origfst, invert);
46 std::cout <<
"FST after back conversion is:\n";
48 FstPrinter<Arc> fstprinter(origfst, NULL, NULL, NULL,
false,
true,
"\t");
49 fstprinter.Print(&std::cout,
"standard output");
52 assert(RandEquivalent(*fst, origfst, 5, 0.01,
kaldi::Rand(), 100));
61 for (
int p = 0; p < 10; p++) {
62 typedef ArcTpl<Weight>
Arc;
63 typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
64 for(
int i = 0;
i < 5;
i++) {
65 VectorFst<Arc> *
fst = RandPairFst<Arc>();
66 std::cout <<
"Testing shortest path\n";
67 std::cout <<
"FST before converting to compact-arc is:\n";
69 FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL,
false,
true,
"\t");
70 fstprinter.Print(&std::cout,
"standard output");
72 VectorFst<CompactArc> cfst;
73 ConvertLattice<Weight, Int>(*fst, &cfst,
false);
77 VectorFst<Arc> nbest_fst_1;
78 ShortestPath(*fst, &nbest_fst_1, 1);
79 VectorFst<Arc> nbest_fst_2;
80 ShortestPath(*fst, &nbest_fst_2, 3);
81 VectorFst<Arc> nbest_fst_1b;
82 ShortestPath(nbest_fst_2, &nbest_fst_1b, 1);
86 ShortestDistance(nbest_fst_1b)));
90 ShortestDistance(nbest_fst_1b)));
93 VectorFst<CompactArc> nbest_fst_1;
94 ShortestPath(cfst, &nbest_fst_1, 1);
95 VectorFst<CompactArc> nbest_fst_2;
96 ShortestPath(cfst, &nbest_fst_2, 3);
97 VectorFst<CompactArc> nbest_fst_1b;
98 ShortestPath(nbest_fst_2, &nbest_fst_1b, 1);
101 ShortestDistance(nbest_fst_1b)));
104 ShortestDistance(nbest_fst_1b)));
115 typedef ArcTpl<LatticeWeightTpl<float> > ArcF;
116 typedef ArcTpl<LatticeWeightTpl<double> > ArcD;
117 typedef ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > CArcF;
118 typedef ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > CArcD;
120 for(
int i = 0;
i < 2;
i++) {
122 VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
123 VectorFst<ArcD> fst2;
124 VectorFst<ArcF> fst3;
128 assert(RandEquivalent(*fst1, fst3, 5, 0.01,
kaldi::Rand(), 100));
133 VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
134 VectorFst<CArcF> cfst1, cfst3;
136 VectorFst<CArcD> cfst2;
139 assert(RandEquivalent(cfst1, cfst3, 5, 0.01,
kaldi::Rand(), 100));
144 VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
145 VectorFst<CArcD> cfst1, cfst3;
147 VectorFst<CArcF> cfst2;
150 assert(RandEquivalent(cfst1, cfst3, 5, 0.01,
kaldi::Rand(), 100));
155 VectorFst<ArcD> *fst1 = RandPairFst<ArcD>();
156 VectorFst<CArcD> cfst1, cfst3;
158 VectorFst<CArcF> cfst2;
161 assert(RandEquivalent(cfst1, cfst3, 5, 0.01,
kaldi::Rand(), 100));
166 VectorFst<ArcD> *fst1 = RandPairFst<ArcD>();
167 VectorFst<CArcF> cfst1;
169 VectorFst<ArcD> fst2;
171 assert(RandEquivalent(*fst1, fst2, 5, 0.01,
kaldi::Rand(), 100));
176 VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
177 VectorFst<CArcD> cfst1;
179 VectorFst<ArcF> fst2;
181 assert(RandEquivalent(*fst1, fst2, 5, 0.01,
kaldi::Rand(), 100));
186 VectorFst<ArcD> *fst1 = RandPairFst<ArcD>();
187 VectorFst<CArcF> cfst1;
189 VectorFst<ArcD> fst2;
191 assert(RandEquivalent(*fst1, fst2, 5, 0.01,
kaldi::Rand(), 100));
201 typedef ArcTpl<Weight>
Arc;
202 typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
203 for(
int i = 0;
i < 2;
i++) {
204 VectorFst<Arc> *
fst = RandPairFst<Arc>();
210 VectorFst<CompactArc> ofst;
211 ConvertLattice<Weight, Int>(*fst, &ofst, invert);
218 VectorFst<Arc> origfst;
219 ConvertLattice<Weight, Int>(ofst, &origfst, invert);
226 assert(RandEquivalent(*fst, origfst, 5, 0.01,
kaldi::Rand(), 100));
249 scale2[0][1] = -0.25;
252 scale2[1][0] = -0.25;
256 typedef ArcTpl<Weight>
Arc;
257 typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
258 for(
int i = 0;
i < 2;
i++) {
259 VectorFst<Arc> *
fst = RandPairFst<Arc>();
265 VectorFst<CompactArc> ofst;
266 ConvertLattice<Weight, Int>(*fst, &ofst, invert);
273 VectorFst<Arc> origfst;
274 ConvertLattice<Weight, Int>(ofst, &origfst, invert);
283 if (!RandEquivalent(*fst, origfst, 5, 0.01,
kaldi::Rand(), 100)) {
284 std::cerr <<
"Warn, randequivalent returned false. Checking equivalence another way.\n";
285 assert(Equal(*fst, origfst));
298 typedef ::int64 int64;
299 typedef ::uint64 uint64;
301 typedef ::uint32 uint32;
305 for(
int i = 0;
i < 2;
i++) {
306 bool invert = (
i % 2);
307 TestConvert<TropicalWeight, int32>(invert);
308 TestConvertPair<LatticeWeight, int32>(invert);
309 TestConvertPair<LatticeWeight, size_t>(invert);
310 TestConvertPair<LexicographicWeight<TropicalWeight, TropicalWeight>,
size_t>(invert);
311 TestScalePair<LatticeWeight, int32>(invert);
312 TestScalePair<LatticeWeight, size_t>(invert);
313 TestScalePair<LexicographicWeight<TropicalWeight, TropicalWeight>,
size_t>(invert);
318 TestShortestPath<LatticeWeight, int32>();
319 TestConvert2<int32>();
320 for(
int i = 0;
i < 2;
i++) {
321 bool invert = (
i % 2);
322 TestConvertPair<LatticeWeight, int32>(invert);
323 TestConvertPair<LatticeWeight, size_t>(invert);
324 TestConvertPair<LexicographicWeight<TropicalWeight, TropicalWeight>,
size_t>(invert);
325 TestScalePair<LatticeWeight, int32>(invert);
326 TestScalePair<LatticeWeight, size_t>(invert);
327 TestScalePair<LexicographicWeight<TropicalWeight, TropicalWeight>,
size_t>(invert);
330 std::cout <<
"Tests succeeded\n";
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
bool ApproxEqual(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, float delta=kDelta)
void TestConvertPair(bool invert)
void TestScalePair(bool invert)
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
int Rand(struct RandomState *state)
LatticeWeightTpl< BaseFloat > LatticeWeight
void TestConvert(bool invert)
std::vector< std::vector< double > > DefaultLatticeScale()
Returns a default 2x2 matrix scaling factor for LatticeWeight.