push-lattice-test.cc
Go to the documentation of this file.
1 // lat/push-lattice-test.cc
2 
3 // Copyright 2013 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "lat/kaldi-lattice.h"
22 #include "lat/push-lattice.h"
23 #include "fstext/rand-fst.h"
24 
25 
26 namespace kaldi {
27 using namespace fst;
28 
30  RandFstOptions opts;
31  opts.acyclic = true;
32  Lattice *fst = fst::RandPairFst<LatticeArc>(opts);
33  CompactLattice *cfst = new CompactLattice;
34  ConvertLattice(*fst, cfst);
35  delete fst;
36  return cfst;
37 }
38 
41  CompactLattice clat2(*clat);
43  KALDI_ASSERT(fst::RandEquivalent(*clat, clat2, 5, 0.001, Rand(), 10));
44  for (CompactLatticeArc::StateId s = 0; s < clat2.NumStates(); s++) {
45  if (s == 0)
46  continue; // We don't check state zero, as the "leftover string" stays
47  // there.
48  int32 first_label = -1;
49  bool ok = false;
50  bool first_label_set = false;
51  for (ArcIterator<CompactLattice> aiter(clat2, s); !aiter.Done();
52  aiter.Next()) {
53  if (aiter.Value().weight.String().size() == 0) {
54  ok = true;
55  } else {
56  int32 this_label = aiter.Value().weight.String().front();
57  if (first_label_set) {
58  if (this_label != first_label) ok = true;
59  } else {
60  first_label = this_label;
61  first_label_set = true;
62  }
63  }
64  }
65  if (clat2.Final(s) != CompactLatticeWeight::Zero()) {
66  if (clat2.Final(s).String().size() == 0) ok = true;
67  else {
68  int32 this_label = clat2.Final(s).String().front();
69  if (first_label_set && this_label != first_label) ok = true;
70  }
71  }
72  KALDI_ASSERT(ok);
73  }
74  delete clat;
75 }
76 
79  CompactLattice clat2(*clat);
81  KALDI_ASSERT(fst::RandEquivalent(*clat, clat2, 5, 0.001, Rand(), 10));
82  for (CompactLatticeArc::StateId s = 0; s < clat2.NumStates(); s++) {
83  if (s == 0)
84  continue; // We don't check state zero, as the "leftover string" stays
85  // there.
86  LatticeWeight sum = clat2.Final(s).Weight();
87  for (ArcIterator<CompactLattice> aiter(clat2, s); !aiter.Done();
88  aiter.Next()) {
89  sum = Plus(sum, aiter.Value().weight.Weight());
90  }
91  if (!ApproxEqual(sum, LatticeWeight::One())) {
92  {
93  fst::FstPrinter<CompactLatticeArc> printer(clat2, NULL, NULL,
94  NULL, true, true, "\t");
95  printer.Print(&std::cerr, "<unknown>");
96  }
97  {
98  fst::FstPrinter<CompactLatticeArc> printer(*clat, NULL, NULL,
99  NULL, true, true, "\t");
100  printer.Print(&std::cerr, "<unknown>");
101  }
102  KALDI_ERR << "Bad lattice being pushed.";
103  }
104  }
105  delete clat;
106 }
107 
108 
109 
110 } // end namespace kaldi
111 
112 int main() {
113  using namespace kaldi;
114  using kaldi::int32;
115  for (int32 i = 0; i < 15; i++) {
118  }
119  KALDI_LOG << "Success.";
120 }
fst::StdArc::StateId StateId
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
static const LatticeWeightTpl One()
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
CompactLattice * RandCompactLattice()
bool PushCompactLatticeStrings(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
This function pushes the transition-ids as far towards the start as they will go. ...
kaldi::int32 int32
bool PushCompactLatticeWeights(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
This function pushes the weights in the CompactLattice so that all states except possibly the start s...
void TestPushCompactLatticeWeights()
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.
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
#define KALDI_ERR
Definition: kaldi-error.h:147
void TestPushCompactLatticeStrings()
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
static const CompactLatticeWeightTpl< WeightType, IntType > Zero()
#define KALDI_LOG
Definition: kaldi-error.h:153
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265
int main()