// fstext/fstext-utils-test.cc // Copyright 2009-2012 Microsoft Corporation Daniel Povey // 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 "base/kaldi-common.h" // for exceptions #include "fstext/fstext-utils.h" #include "fstext/fst-test-utils.h" #include "util/stl-utils.h" #include "base/kaldi-math.h" namespace fst { template void TestMakeLinearAcceptor() { typedef typename Arc::Label Label; typedef typename Arc::StateId StateId; typedef typename Arc::Weight Weight; int len = kaldi::Rand() % 10; vector vec; vector vec_nozeros; for (int i = 0; i < len; i++) { int j = kaldi::Rand() % len; vec.push_back(j); if (j != 0) vec_nozeros.push_back(j); } VectorFst vfst; MakeLinearAcceptor(vec, &vfst); vector vec2; vector vec3; Weight w; GetLinearSymbolSequence(vfst, &vec2, &vec3, &w); assert(w == Weight::One()); assert(vec_nozeros == vec2); assert(vec_nozeros == vec3); if (vec2.size() != 0 || vec3.size() != 0) { // This test might not work // for empty sequences... { vector > fstvec; NbestAsFsts(vfst, 1, &fstvec); KALDI_ASSERT(fstvec.size() == 1); assert(RandEquivalent(vfst, fstvec[0], 2/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/)); } } bool include_eps = (kaldi::Rand() % 2 == 0); if (!include_eps) vec = vec_nozeros; kaldi::SortAndUniq(&vec); vector vec4; GetInputSymbols(vfst, include_eps, &vec4); assert(vec4 == vec); vector vec5; GetInputSymbols(vfst, include_eps, &vec5); } template void TestDeterminizeStarInLog() { VectorFst *fst = RandFst(); VectorFst fst_copy(fst); typename Arc::Label next_sym = 1 + HighestNumberedInputSymbol(*fst); vector syms; PreDeterminize(fst, NULL, "#", next_sym, &syms); } // Don't instantiate with log semiring, as RandEquivalent may fail. template void TestSafeDeterminizeWrapper() { // also tests SafeDeterminizeMinimizeWrapper(). typedef typename Arc::Label Label; typedef typename Arc::StateId StateId; typedef typename Arc::Weight Weight; VectorFst *fst = new VectorFst(); int n_syms = 2 + kaldi::Rand() % 5, n_states = 3 + kaldi::Rand() % 10, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%3; // Up to 2 unique symbols. cout << "Testing pre-determinize with "<AddSymbol(""); delete sptr; sptr = new SymbolTable("my-symbol-table"); vector