fstaddsubsequentialloop.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // fstbin/fstaddsubsequentialloop.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 "util/kaldi-io.h"
  19. #include "util/parse-options.h"
  20. #include "util/text-utils.h"
  21. #include "fst/fstlib.h"
  22. #include "fstext/fstext-utils.h"
  23. #include "fstext/context-fst.h"
  24. #include "fstext/kaldi-fst-io.h"
  25. /* some test examples:
  26. ( echo "0 0 0 0"; echo "0 0" ) | fstcompile | fstaddsubsequentialloop 1 | fstprint
  27. ( echo "0 1 0 0"; echo " 0 2 0 0"; echo "1 0"; echo "2 0"; ) | fstcompile | fstaddsubsequentialloop 1 | fstprint
  28. */
  29. int main(int argc, char *argv[]) {
  30. try {
  31. using namespace kaldi;
  32. using namespace fst;
  33. using kaldi::int32;
  34. const char *usage =
  35. "Minimizes FST after encoding [this algorithm applicable to all FSTs in tropical semiring]\n"
  36. "\n"
  37. "Usage: fstaddsubsequentialloop subseq_sym [in.fst [out.fst] ]\n"
  38. "E.g.: fstaddsubsequentialloop 52 < LG.fst > LG_sub.fst\n";
  39. float delta = kDelta;
  40. ParseOptions po(usage);
  41. po.Register("delta", &delta,
  42. "Delta likelihood used for quantization of weights");
  43. po.Read(argc, argv);
  44. if (po.NumArgs() < 1 || po.NumArgs() > 3) {
  45. po.PrintUsage();
  46. exit(1);
  47. }
  48. int32 subseq_sym;
  49. if (!ConvertStringToInteger(po.GetArg(1), &subseq_sym))
  50. KALDI_ERR << "Invalid subsequential symbol "<<po.GetArg(1);
  51. std::string fst_in_filename = po.GetOptArg(2);
  52. std::string fst_out_filename = po.GetOptArg(3);
  53. VectorFst<StdArc> *fst = ReadFstKaldi(fst_in_filename);
  54. int32 h = HighestNumberedInputSymbol(*fst);
  55. if (subseq_sym <= h) {
  56. std::cerr << "fstaddsubsequentialloop.cc: subseq symbol does not seem right, "<<subseq_sym<<" <= "<<h<<'\n';
  57. }
  58. AddSubsequentialLoop(subseq_sym, fst);
  59. WriteFstKaldi(*fst, fst_out_filename);
  60. delete fst;
  61. return 0;
  62. } catch(const std::exception &e) {
  63. std::cerr << e.what();
  64. return -1;
  65. }
  66. }