You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.1 KiB

  1. // fstbin/fstisstochastic.cc
  2. // Copyright 2009-2011 Microsoft Corporation
  3. // See ../../COPYING for clarification regarding multiple authors
  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. #include "base/kaldi-common.h"
  18. #include "fst/fstlib.h"
  19. #include "fstext/fstext-utils.h"
  20. #include "fstext/kaldi-fst-io.h"
  21. #include "util/kaldi-io.h"
  22. #include "util/parse-options.h"
  23. // e.g. of test:
  24. // echo " 0 0" | fstcompile | fstisstochastic
  25. // should return 0 and print "0 0" [meaning, min and
  26. // max weight are one = exp(0)]
  27. // echo " 0 1" | fstcompile | fstisstochastic
  28. // should return 1, not stochastic, and print 1 1
  29. // (echo "0 0 0 0 0.693147 "; echo "0 1 0 0 0.693147 "; echo "1 0" ) |
  30. // fstcompile | fstisstochastic should return 0, stochastic; it prints "0
  31. // -1.78e-07" for me (echo "0 0 0 0 0.693147 "; echo "0 1 0 0 0.693147 "; echo
  32. // "1 0" ) | fstcompile | fstisstochastic --test-in-log=false should return 1,
  33. // not stochastic in tropical; it prints "0 0.693147" for me (echo "0 0 0 0 0 ";
  34. // echo "0 1 0 0 0 "; echo "1 0" ) | fstcompile | fstisstochastic
  35. // --test-in-log=false should return 0, stochastic in tropical; it prints "0 0"
  36. // for me (echo "0 0 0 0 0.693147 "; echo "0 1 0 0 0.693147 "; echo "1 0" ) |
  37. // fstcompile | fstisstochastic --test-in-log=false --delta=1 returns 0 even
  38. // though not stochastic because we gave it an absurdly large delta.
  39. int main(int argc, char* argv[]) {
  40. try {
  41. using namespace kaldi; // NOLINT
  42. using namespace fst; // NOLINT
  43. using kaldi::int32;
  44. const char* usage =
  45. "Checks whether an FST is stochastic and exits with success if so.\n"
  46. "Prints out maximum error (in log units).\n"
  47. "\n"
  48. "Usage: fstisstochastic [ in.fst ]\n";
  49. float delta = 0.01;
  50. bool test_in_log = true;
  51. ParseOptions po(usage);
  52. po.Register("delta", &delta, "Maximum error to accept.");
  53. po.Register("test-in-log", &test_in_log,
  54. "Test stochasticity in log semiring.");
  55. po.Read(argc, argv);
  56. if (po.NumArgs() > 1) {
  57. po.PrintUsage();
  58. exit(1);
  59. }
  60. std::string fst_in_filename = po.GetOptArg(1);
  61. Fst<StdArc>* fst = ReadFstKaldiGeneric(fst_in_filename);
  62. bool ans;
  63. StdArc::Weight min, max;
  64. if (test_in_log)
  65. ans = IsStochasticFstInLog(*fst, delta, &min, &max);
  66. else
  67. ans = IsStochasticFst(*fst, delta, &min, &max);
  68. std::cout << min.Value() << " " << max.Value() << '\n';
  69. delete fst;
  70. if (ans)
  71. return 0; // success;
  72. else
  73. return 1;
  74. } catch (const std::exception& e) {
  75. std::cerr << e.what();
  76. return -1;
  77. }
  78. }