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.

100 lines
3.3 KiB

  1. // fstbin/fstaddselfloops.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/determinize-star.h"
  20. #include "fstext/fstext-utils.h"
  21. #include "fstext/kaldi-fst-io.h"
  22. #include "util/parse-options.h"
  23. #include "util/simple-io-funcs.h"
  24. /* some test examples:
  25. pushd ~/tmpdir
  26. ( echo 3; echo 4) > in.list
  27. ( echo 5; echo 6) > out.list
  28. ( echo "0 0 0 0"; echo "0 0" ) | fstcompile | fstaddselfloops in.list out.list
  29. | fstprint ( echo "0 1 0 1"; echo " 0 2 1 0"; echo "1 0"; echo "2 0"; ) |
  30. fstcompile | fstaddselfloops in.list out.list | fstprint
  31. */
  32. int main(int argc, char* argv[]) {
  33. try {
  34. using namespace kaldi; // NOLINT
  35. using namespace fst; // NOLINT
  36. using kaldi::int32;
  37. const char* usage =
  38. "Adds self-loops to states of an FST to propagate disambiguation "
  39. "symbols through it\n"
  40. "They are added on each final state and each state with non-epsilon "
  41. "output symbols\n"
  42. "on at least one arc out of the state. Useful in conjunction with "
  43. "predeterminize\n"
  44. "\n"
  45. "Usage: fstaddselfloops in-disambig-list out-disambig-list [in.fst "
  46. "[out.fst] ]\n"
  47. "E.g: fstaddselfloops in.list out.list < in.fst > withloops.fst\n"
  48. "in.list and out.list are lists of integers, one per line, of the\n"
  49. "same length.\n";
  50. ParseOptions po(usage);
  51. po.Read(argc, argv);
  52. if (po.NumArgs() < 2 || po.NumArgs() > 4) {
  53. po.PrintUsage();
  54. exit(1);
  55. }
  56. std::string disambig_in_rxfilename = po.GetArg(1),
  57. disambig_out_rxfilename = po.GetArg(2),
  58. fst_in_filename = po.GetOptArg(3),
  59. fst_out_filename = po.GetOptArg(4);
  60. VectorFst<StdArc>* fst = ReadFstKaldi(fst_in_filename);
  61. std::vector<int32> disambig_in;
  62. if (!ReadIntegerVectorSimple(disambig_in_rxfilename, &disambig_in))
  63. KALDI_ERR
  64. << "fstaddselfloops: Could not read disambiguation symbols from "
  65. << kaldi::PrintableRxfilename(disambig_in_rxfilename);
  66. std::vector<int32> disambig_out;
  67. if (!ReadIntegerVectorSimple(disambig_out_rxfilename, &disambig_out))
  68. KALDI_ERR
  69. << "fstaddselfloops: Could not read disambiguation symbols from "
  70. << kaldi::PrintableRxfilename(disambig_out_rxfilename);
  71. if (disambig_in.size() != disambig_out.size())
  72. KALDI_ERR
  73. << "fstaddselfloops: mismatch in size of disambiguation symbols";
  74. AddSelfLoops(fst, disambig_in, disambig_out);
  75. WriteFstKaldi(*fst, fst_out_filename);
  76. delete fst;
  77. return 0;
  78. } catch (const std::exception& e) {
  79. std::cerr << e.what();
  80. return -1;
  81. }
  82. return 0;
  83. }