|
| 1 | +// fstext/push-special-test.cc |
| 2 | + |
| 3 | +// Copyright 2009-2011 Microsoft Corporation |
| 4 | + |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 12 | +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED |
| 13 | +// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 14 | +// MERCHANTABLITY OR NON-INFRINGEMENT. |
| 15 | +// See the Apache 2 License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | + |
| 19 | +#include "fstext/push-special.h" |
| 20 | +#include "fstext/rand-fst.h" |
| 21 | +#include "fstext/fstext-utils.h" |
| 22 | + |
| 23 | +namespace fst |
| 24 | +{ |
| 25 | + |
| 26 | + |
| 27 | +// Don't instantiate with log semiring, as RandEquivalent may fail. |
| 28 | +static void TestPushSpecial() { |
| 29 | + typedef StdArc Arc; |
| 30 | + typedef Arc::Label Label; |
| 31 | + typedef Arc::StateId StateId; |
| 32 | + typedef Arc::Weight Weight; |
| 33 | + |
| 34 | + VectorFst<Arc> *fst = RandFst<StdArc>(); |
| 35 | + |
| 36 | + { |
| 37 | + FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true); |
| 38 | + fstprinter.Print(&std::cout, "standard output"); |
| 39 | + } |
| 40 | + |
| 41 | + VectorFst<Arc> fst_copy(*fst); |
| 42 | + |
| 43 | + float delta = kDelta; |
| 44 | + PushSpecial(&fst_copy, delta); |
| 45 | + |
| 46 | + Weight min, max; |
| 47 | + float delta_dontcare = 0.1; |
| 48 | + IsStochasticFstInLog(fst_copy, delta_dontcare, &min, &max); |
| 49 | + // the per-state normalizers are allowed to deviate from the average by delta |
| 50 | + // up and down, so the difference from the min to max weight should be 2*delta |
| 51 | + // or less. We give it a bit of wiggle room (->2.5) due to numerical roundoff. |
| 52 | + |
| 53 | + |
| 54 | + { |
| 55 | + FstPrinter<Arc> fstprinter(fst_copy, NULL, NULL, NULL, false, true); |
| 56 | + fstprinter.Print(&std::cout, "standard output"); |
| 57 | + } |
| 58 | + KALDI_LOG << "Min value is " << min.Value() << ", max value is " << max.Value(); |
| 59 | + |
| 60 | + // below, should be <= delta but different pieces of code compute this in this |
| 61 | + // part vs. push-special, so the roundoff may be different. |
| 62 | + KALDI_ASSERT(std::abs(min.Value() - max.Value()) <= 1.2 * delta); |
| 63 | + |
| 64 | + KALDI_ASSERT(RandEquivalent(*fst, fst_copy, |
| 65 | + 5/*paths*/, 0.01/*delta*/, rand()/*seed*/, 100/*path length-- max?*/)); |
| 66 | + delete fst; |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +} // namespace fst |
| 71 | + |
| 72 | +int main() { |
| 73 | + kaldi::g_kaldi_verbose_level = 4; |
| 74 | + using namespace fst; |
| 75 | + for (int i = 0; i < 25; i++) { |
| 76 | + TestPushSpecial(); |
| 77 | + } |
| 78 | +} |
0 commit comments