kaldi-lattice-test.cc
Go to the documentation of this file.
1 // lat/kaldi-lattice-test.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
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 "fstext/rand-fst.h"
23 
24 
25 namespace kaldi {
26 
27 
29  Lattice *fst = fst::RandPairFst<LatticeArc>();
30  CompactLattice *cfst = new CompactLattice;
31  ConvertLattice(*fst, cfst);
32  delete fst;
33  return cfst;
34 }
35 
37  Lattice *fst = fst::RandPairFst<LatticeArc>();
38  return fst;
39 }
40 
41 void TestCompactLatticeTable(bool binary) {
42  CompactLatticeWriter writer(binary ? "ark:tmpf" : "ark,t:tmpf");
43  int N = 10;
44  std::vector<CompactLattice*> lat_vec(N);
45  for (int i = 0; i < N; i++) {
46  char buf[2];
47  buf[0] = '0' + i;
48  buf[1] = '\0';
49  std::string key = "key" + std::string(buf);
51  lat_vec[i] = fst;
52  writer.Write(key, *fst);
53  }
54  writer.Close();
55 
56  RandomAccessCompactLatticeReader reader("ark:tmpf");
57  for (int i = 0; i < N; i++) {
58  char buf[2];
59  buf[0] = '0' + i;
60  buf[1] = '\0';
61  std::string key = "key" + std::string(buf);
62  const CompactLattice &fst = reader.Value(key);
63  KALDI_ASSERT(fst::Equal(fst, *(lat_vec[i])));
64  delete lat_vec[i];
65  }
66 }
67 
68 // Write as CompactLattice, read as Lattice.
69 void TestCompactLatticeTableCross(bool binary) {
70  CompactLatticeWriter writer(binary ? "ark:tmpf" : "ark,t:tmpf");
71  int N = 10;
72  std::vector<CompactLattice*> lat_vec(N);
73  for (int i = 0; i < N; i++) {
74  char buf[2];
75  buf[0] = '0' + i;
76  buf[1] = '\0';
77  std::string key = "key" + std::string(buf);
79  lat_vec[i] = fst;
80  writer.Write(key, *fst);
81  }
82  writer.Close();
83 
84  RandomAccessLatticeReader reader("ark:tmpf");
85  for (int i = 0; i < N; i++) {
86  char buf[2];
87  buf[0] = '0' + i;
88  buf[1] = '\0';
89  std::string key = "key" + std::string(buf);
90  const Lattice &fst = reader.Value(key);
91  CompactLattice fst2;
92  ConvertLattice(fst, &fst2);
93  KALDI_ASSERT(fst::Equal(fst2, *(lat_vec[i])));
94  delete lat_vec[i];
95  }
96 }
97 
98 // Lattice, binary.
99 void TestLatticeTable(bool binary) {
100  LatticeWriter writer(binary ? "ark:tmpf" : "ark,t:tmpf");
101  int N = 10;
102  std::vector<Lattice*> lat_vec(N);
103  for (int i = 0; i < N; i++) {
104  char buf[2];
105  buf[0] = '0' + i;
106  buf[1] = '\0';
107  std::string key = "key" + std::string(buf);
108  Lattice *fst = RandLattice();
109  lat_vec[i] = fst;
110  writer.Write(key, *fst);
111  }
112  writer.Close();
113 
114  RandomAccessLatticeReader reader("ark:tmpf");
115  for (int i = 0; i < N; i++) {
116  char buf[2];
117  buf[0] = '0' + i;
118  buf[1] = '\0';
119  std::string key = "key" + std::string(buf);
120  const Lattice &fst = reader.Value(key);
121  KALDI_ASSERT(fst::Equal(fst, *(lat_vec[i])));
122  delete lat_vec[i];
123  }
124 }
125 
126 
127 // Write as Lattice, read as CompactLattice.
128 void TestLatticeTableCross(bool binary) {
129  LatticeWriter writer(binary ? "ark:tmpf" : "ark,t:tmpf");
130  int N = 10;
131  std::vector<Lattice*> lat_vec(N);
132  for (int i = 0; i < N; i++) {
133  char buf[2];
134  buf[0] = '0' + i;
135  buf[1] = '\0';
136  std::string key = "key" + std::string(buf);
137  Lattice *fst = RandLattice();
138  lat_vec[i] = fst;
139  writer.Write(key, *fst);
140  }
141  writer.Close();
142 
143  RandomAccessCompactLatticeReader reader("ark:tmpf");
144  for (int i = 0; i < N; i++) {
145  char buf[2];
146  buf[0] = '0' + i;
147  buf[1] = '\0';
148  std::string key = "key" + std::string(buf);
149  const CompactLattice &fst = reader.Value(key);
150  Lattice fst2;
151  ConvertLattice(fst, &fst2);
152  KALDI_ASSERT(fst::RandEquivalent(fst2, *(lat_vec[i]), 5, 0.01, Rand(), 10));
153  delete lat_vec[i];
154  }
155 }
156 
157 
158 
159 } // end namespace kaldi
160 
161 int main() {
162  using namespace kaldi;
163  for (int i = 0; i < 2; i++) {
164  bool binary = (i%2 == 0);
165  TestCompactLatticeTable(binary);
167  TestLatticeTable(binary);
168  TestLatticeTableCross(binary);
169  }
170  std::cout << "Test OK\n";
171 
172  unlink("tmpf");
173 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
CompactLattice * RandCompactLattice()
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
void Write(const std::string &key, const T &value) const
void TestLatticeTableCross(bool binary)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
int main()
const T & Value(const std::string &key)
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
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
void TestCompactLatticeTableCross(bool binary)
void TestCompactLatticeTable(bool binary)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Lattice * RandLattice()
void TestLatticeTable(bool binary)