Blame view

src/fstext/table-matcher-test.cc 6.83 KB
8dcb6dfcb   Yannick Estève   first commit
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
  // fstext/table-matcher-test.cc
  
  // Copyright 2009-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/table-matcher.h"
  #include "fstext/fst-test-utils.h"
  #include "base/kaldi-math.h"
  
  namespace fst{
  
  
  // Don't instantiate with log semiring, as RandEquivalent may fail.
  template<class Arc>  void TestTableMatcher(bool connect, bool left) {
    typedef typename Arc::Label Label;
    typedef typename Arc::StateId StateId;
    typedef typename Arc::Weight Weight;
  
  
    VectorFst<Arc> *fst1 = RandFst<Arc>();
  
    VectorFst<Arc> *fst2 = RandFst<Arc>();
  
    ILabelCompare<Arc> ilabel_comp;
    OLabelCompare<Arc> olabel_comp;
  
    TableComposeOptions opts;
    if (left) opts.table_match_type = MATCH_OUTPUT;
    else opts.table_match_type = MATCH_INPUT;
    opts.min_table_size = 1 + kaldi::Rand() % 5;
    opts.table_ratio = 0.25 * (kaldi::Rand() % 5);
    opts.connect = connect;
  
    ArcSort(fst1, olabel_comp);
    ArcSort(fst2, ilabel_comp);
  
    VectorFst<Arc> composed;
  
    TableCompose(*fst1, *fst2, &composed, opts);
  
    if (!connect) Connect(&composed);
  
    VectorFst<Arc> composed_baseline;
  
    Compose(*fst1, *fst2, &composed_baseline);
  
  
    std::cout << "Connect = "<< (connect?"True
  ":"False
  ");
  
    std::cout <<"Table-Composed FST
  ";
    {
      FstPrinter<Arc> fstprinter(composed, NULL, NULL, NULL, false, true, "\t");
      fstprinter.Print(&std::cout, "standard output");
    }
  
    std::cout <<" Baseline-Composed FST
  ";
    {
      FstPrinter<Arc> fstprinter(composed_baseline, NULL, NULL, NULL, false, true, "\t");
      fstprinter.Print(&std::cout, "standard output");
    }
  
    if ( !RandEquivalent(composed, composed_baseline, 3/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 20/*path length-- max?*/)) {
      VectorFst<Arc> diff1;
      Difference(composed, composed_baseline, &diff1);
      std::cout <<" Diff1 (composed - baseline) 
  ";
      {
        FstPrinter<Arc> fstprinter(diff1, NULL, NULL, NULL, false, true, "\t");
        fstprinter.Print(&std::cout, "standard output");
      }
  
  
      VectorFst<Arc> diff2;
      Difference(composed_baseline, composed, &diff2);
      std::cout <<" Diff2 (baseline - composed) 
  ";
      {
        FstPrinter<Arc> fstprinter(diff2, NULL, NULL, NULL, false, true, "\t");
        fstprinter.Print(&std::cout, "standard output");
      }
  
      assert(0);
    }
  
    delete fst1;
    delete fst2;
  }
  
  
  
  // Don't instantiate with log semiring, as RandEquivalent may fail.
  template<class Arc>  void TestTableMatcherCacheLeft(bool connect) {
    typedef typename Arc::Label Label;
    typedef typename Arc::StateId StateId;
    typedef typename Arc::Weight Weight;
  
  
    VectorFst<Arc> *fst1 = RandFst<Arc>();
  
  
    TableComposeOptions opts;
    opts.table_match_type = MATCH_OUTPUT;
    opts.min_table_size = 1 + kaldi::Rand() % 5;
    opts.table_ratio = 0.25 * (kaldi::Rand() % 5);
    opts.connect = connect;
  
    TableComposeCache<Fst<Arc> > cache(opts);
  
    for (size_t i = 0; i < 3; i++) {
  
      VectorFst<Arc> *fst2 = RandFst<Arc>();
  
      ILabelCompare<Arc> ilabel_comp;
      OLabelCompare<Arc> olabel_comp;
  
  
      ArcSort(fst1, olabel_comp);
      ArcSort(fst2, ilabel_comp);
  
      VectorFst<Arc> composed;
  
      TableCompose(*fst1, *fst2, &composed, &cache);
  
      if (!connect) Connect(&composed);
  
      VectorFst<Arc> composed_baseline;
  
      Compose(*fst1, *fst2, &composed_baseline);
  
  
      std::cout << "Connect = "<< (connect?"True
  ":"False
  ");
  
  
      if ( !RandEquivalent(composed, composed_baseline, 3/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/)) {
        VectorFst<Arc> diff1;
        Difference(composed, composed_baseline, &diff1);
        std::cout <<" Diff1 (composed - baseline) 
  ";
        {
          FstPrinter<Arc> fstprinter(diff1, NULL, NULL, NULL, false, true, "\t");
          fstprinter.Print(&std::cout, "standard output");
        }
  
  
        VectorFst<Arc> diff2;
        Difference(composed_baseline, composed, &diff2);
        std::cout <<" Diff2 (baseline - composed) 
  ";
        {
          FstPrinter<Arc> fstprinter(diff2, NULL, NULL, NULL, false, true, "\t");
          fstprinter.Print(&std::cout, "standard output");
        }
  
        assert(0);
      }
      delete fst2;
    }
  
    delete fst1;
  }
  
  
  template<class Arc>  void TestTableMatcherCacheRight(bool connect) {
    typedef typename Arc::Label Label;
    typedef typename Arc::StateId StateId;
    typedef typename Arc::Weight Weight;
  
  
    VectorFst<Arc> *fst2 = RandFst<Arc>();
    ILabelCompare<Arc> ilabel_comp;
    ArcSort(fst2, ilabel_comp);
  
  
    TableComposeOptions opts;
    opts.table_match_type = MATCH_INPUT;
    opts.min_table_size = 1 + kaldi::Rand() % 5;
    opts.table_ratio = 0.25 * (kaldi::Rand() % 5);
    opts.connect = connect;
  
    TableComposeCache<Fst<Arc> > cache(opts);
  
    for (size_t i = 0; i < 2; i++) {
  
      VectorFst<Arc> *fst1 = RandFst<Arc>();
  
  
      OLabelCompare<Arc> olabel_comp;
  
  
      ArcSort(fst1, olabel_comp);
  
      VectorFst<Arc> composed;
  
      TableCompose(*fst1, *fst2, &composed, &cache);
  
      if (!connect) Connect(&composed);
  
      VectorFst<Arc> composed_baseline;
  
      Compose(*fst1, *fst2, &composed_baseline);
  
  
      std::cout << "Connect = "<< (connect?"True
  ":"False
  ");
  
  
      if ( !RandEquivalent(composed, composed_baseline, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 20/*path length-- max?*/)) {
        VectorFst<Arc> diff1;
        Difference(composed, composed_baseline, &diff1);
        std::cout <<" Diff1 (composed - baseline) 
  ";
        {
          FstPrinter<Arc> fstprinter(diff1, NULL, NULL, NULL, false, true, "\t");
          fstprinter.Print(&std::cout, "standard output");
        }
  
  
        VectorFst<Arc> diff2;
        Difference(composed_baseline, composed, &diff2);
        std::cout <<" Diff2 (baseline - composed) 
  ";
        {
          FstPrinter<Arc> fstprinter(diff2, NULL, NULL, NULL, false, true, "\t");
          fstprinter.Print(&std::cout, "standard output");
        }
  
        assert(0);
      }
      delete fst1;
    }
  
    delete fst2;
  }
  
  
  } // namespace fst
  
  int main() {
    using namespace fst;
    for (int i = 0;i < 1;i++) {
      TestTableMatcher<fst::StdArc>(true, true);
      TestTableMatcher<fst::StdArc>(false, true);
      TestTableMatcher<fst::StdArc>(true, false);
      TestTableMatcher<fst::StdArc>(false, false);
      TestTableMatcherCacheLeft<fst::StdArc>(true);
      TestTableMatcherCacheLeft<fst::StdArc>(false);
      TestTableMatcherCacheRight<fst::StdArc>(true);
      TestTableMatcherCacheRight<fst::StdArc>(false);
    }
  }