table-matcher-test.cc
Go to the documentation of this file.
1 // fstext/table-matcher-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 #include "fstext/table-matcher.h"
21 #include "fstext/fst-test-utils.h"
22 #include "base/kaldi-math.h"
23 
24 namespace fst{
25 
26 
27 // Don't instantiate with log semiring, as RandEquivalent may fail.
28 template<class Arc> void TestTableMatcher(bool connect, bool left) {
29  typedef typename Arc::Label Label;
30  typedef typename Arc::StateId StateId;
31  typedef typename Arc::Weight Weight;
32 
33 
34  VectorFst<Arc> *fst1 = RandFst<Arc>();
35 
36  VectorFst<Arc> *fst2 = RandFst<Arc>();
37 
38  ILabelCompare<Arc> ilabel_comp;
39  OLabelCompare<Arc> olabel_comp;
40 
42  if (left) opts.table_match_type = MATCH_OUTPUT;
43  else opts.table_match_type = MATCH_INPUT;
44  opts.min_table_size = 1 + kaldi::Rand() % 5;
45  opts.table_ratio = 0.25 * (kaldi::Rand() % 5);
46  opts.connect = connect;
47 
48  ArcSort(fst1, olabel_comp);
49  ArcSort(fst2, ilabel_comp);
50 
51  VectorFst<Arc> composed;
52 
53  TableCompose(*fst1, *fst2, &composed, opts);
54 
55  if (!connect) Connect(&composed);
56 
57  VectorFst<Arc> composed_baseline;
58 
59  Compose(*fst1, *fst2, &composed_baseline);
60 
61 
62  std::cout << "Connect = "<< (connect?"True\n":"False\n");
63 
64  std::cout <<"Table-Composed FST\n";
65  {
66  FstPrinter<Arc> fstprinter(composed, NULL, NULL, NULL, false, true, "\t");
67  fstprinter.Print(&std::cout, "standard output");
68  }
69 
70  std::cout <<" Baseline-Composed FST\n";
71  {
72  FstPrinter<Arc> fstprinter(composed_baseline, NULL, NULL, NULL, false, true, "\t");
73  fstprinter.Print(&std::cout, "standard output");
74  }
75 
76  if ( !RandEquivalent(composed, composed_baseline, 3/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 20/*path length-- max?*/)) {
77  VectorFst<Arc> diff1;
78  Difference(composed, composed_baseline, &diff1);
79  std::cout <<" Diff1 (composed - baseline) \n";
80  {
81  FstPrinter<Arc> fstprinter(diff1, NULL, NULL, NULL, false, true, "\t");
82  fstprinter.Print(&std::cout, "standard output");
83  }
84 
85 
86  VectorFst<Arc> diff2;
87  Difference(composed_baseline, composed, &diff2);
88  std::cout <<" Diff2 (baseline - composed) \n";
89  {
90  FstPrinter<Arc> fstprinter(diff2, NULL, NULL, NULL, false, true, "\t");
91  fstprinter.Print(&std::cout, "standard output");
92  }
93 
94  assert(0);
95  }
96 
97  delete fst1;
98  delete fst2;
99 }
100 
101 
102 
103 // Don't instantiate with log semiring, as RandEquivalent may fail.
104 template<class Arc> void TestTableMatcherCacheLeft(bool connect) {
105  typedef typename Arc::Label Label;
106  typedef typename Arc::StateId StateId;
107  typedef typename Arc::Weight Weight;
108 
109 
110  VectorFst<Arc> *fst1 = RandFst<Arc>();
111 
112 
113  TableComposeOptions opts;
114  opts.table_match_type = MATCH_OUTPUT;
115  opts.min_table_size = 1 + kaldi::Rand() % 5;
116  opts.table_ratio = 0.25 * (kaldi::Rand() % 5);
117  opts.connect = connect;
118 
119  TableComposeCache<Fst<Arc> > cache(opts);
120 
121  for (size_t i = 0; i < 3; i++) {
122 
123  VectorFst<Arc> *fst2 = RandFst<Arc>();
124 
125  ILabelCompare<Arc> ilabel_comp;
126  OLabelCompare<Arc> olabel_comp;
127 
128 
129  ArcSort(fst1, olabel_comp);
130  ArcSort(fst2, ilabel_comp);
131 
132  VectorFst<Arc> composed;
133 
134  TableCompose(*fst1, *fst2, &composed, &cache);
135 
136  if (!connect) Connect(&composed);
137 
138  VectorFst<Arc> composed_baseline;
139 
140  Compose(*fst1, *fst2, &composed_baseline);
141 
142 
143  std::cout << "Connect = "<< (connect?"True\n":"False\n");
144 
145 
146  if ( !RandEquivalent(composed, composed_baseline, 3/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/)) {
147  VectorFst<Arc> diff1;
148  Difference(composed, composed_baseline, &diff1);
149  std::cout <<" Diff1 (composed - baseline) \n";
150  {
151  FstPrinter<Arc> fstprinter(diff1, NULL, NULL, NULL, false, true, "\t");
152  fstprinter.Print(&std::cout, "standard output");
153  }
154 
155 
156  VectorFst<Arc> diff2;
157  Difference(composed_baseline, composed, &diff2);
158  std::cout <<" Diff2 (baseline - composed) \n";
159  {
160  FstPrinter<Arc> fstprinter(diff2, NULL, NULL, NULL, false, true, "\t");
161  fstprinter.Print(&std::cout, "standard output");
162  }
163 
164  assert(0);
165  }
166  delete fst2;
167  }
168 
169  delete fst1;
170 }
171 
172 
173 template<class Arc> void TestTableMatcherCacheRight(bool connect) {
174  typedef typename Arc::Label Label;
175  typedef typename Arc::StateId StateId;
176  typedef typename Arc::Weight Weight;
177 
178 
179  VectorFst<Arc> *fst2 = RandFst<Arc>();
180  ILabelCompare<Arc> ilabel_comp;
181  ArcSort(fst2, ilabel_comp);
182 
183 
184  TableComposeOptions opts;
185  opts.table_match_type = MATCH_INPUT;
186  opts.min_table_size = 1 + kaldi::Rand() % 5;
187  opts.table_ratio = 0.25 * (kaldi::Rand() % 5);
188  opts.connect = connect;
189 
190  TableComposeCache<Fst<Arc> > cache(opts);
191 
192  for (size_t i = 0; i < 2; i++) {
193 
194  VectorFst<Arc> *fst1 = RandFst<Arc>();
195 
196 
197  OLabelCompare<Arc> olabel_comp;
198 
199 
200  ArcSort(fst1, olabel_comp);
201 
202  VectorFst<Arc> composed;
203 
204  TableCompose(*fst1, *fst2, &composed, &cache);
205 
206  if (!connect) Connect(&composed);
207 
208  VectorFst<Arc> composed_baseline;
209 
210  Compose(*fst1, *fst2, &composed_baseline);
211 
212 
213  std::cout << "Connect = "<< (connect?"True\n":"False\n");
214 
215 
216  if ( !RandEquivalent(composed, composed_baseline, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 20/*path length-- max?*/)) {
217  VectorFst<Arc> diff1;
218  Difference(composed, composed_baseline, &diff1);
219  std::cout <<" Diff1 (composed - baseline) \n";
220  {
221  FstPrinter<Arc> fstprinter(diff1, NULL, NULL, NULL, false, true, "\t");
222  fstprinter.Print(&std::cout, "standard output");
223  }
224 
225 
226  VectorFst<Arc> diff2;
227  Difference(composed_baseline, composed, &diff2);
228  std::cout <<" Diff2 (baseline - composed) \n";
229  {
230  FstPrinter<Arc> fstprinter(diff2, NULL, NULL, NULL, false, true, "\t");
231  fstprinter.Print(&std::cout, "standard output");
232  }
233 
234  assert(0);
235  }
236  delete fst1;
237  }
238 
239  delete fst2;
240 }
241 
242 
243 } // namespace fst
244 
245 int main() {
246  using namespace fst;
247  for (int i = 0;i < 1;i++) {
248  TestTableMatcher<fst::StdArc>(true, true);
249  TestTableMatcher<fst::StdArc>(false, true);
250  TestTableMatcher<fst::StdArc>(true, false);
251  TestTableMatcher<fst::StdArc>(false, false);
252  TestTableMatcherCacheLeft<fst::StdArc>(true);
253  TestTableMatcherCacheLeft<fst::StdArc>(false);
254  TestTableMatcherCacheRight<fst::StdArc>(true);
255  TestTableMatcherCacheRight<fst::StdArc>(false);
256  }
257 }
fst::StdArc::StateId StateId
void TableCompose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const TableComposeOptions &opts=TableComposeOptions())
void TestTableMatcherCacheLeft(bool connect)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void TestTableMatcher(bool connect, bool left)
TableComposeCache lets us do multiple compositions while caching the same matcher.
void TestTableMatcherCacheRight(bool connect)
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
int main()