fsts-to-transcripts.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // fstbin/fsts-to-transcripts.cc
  2. // Copyright 2012-2013 Johns Hopkins University (Authors: Guoguo Chen,
  3. // Daniel Povey)
  4. // See ../../COPYING for clarification regarding multiple authors
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  14. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  15. // MERCHANTABLITY OR NON-INFRINGEMENT.
  16. // See the Apache 2 License for the specific language governing permissions and
  17. // limitations under the License.
  18. #include "base/kaldi-common.h"
  19. #include "util/common-utils.h"
  20. #include "fstext/fstext-utils.h"
  21. #include "fstext/kaldi-fst-io.h"
  22. int main(int argc, char *argv[]) {
  23. try {
  24. using namespace kaldi;
  25. using namespace fst;
  26. typedef kaldi::int32 int32;
  27. typedef kaldi::uint64 uint64;
  28. const char *usage =
  29. "Reads a table of FSTs; for each element, finds the best path and \n"
  30. "prints out the output-symbol sequence (if --output-side=true), or \n"
  31. "input-symbol sequence otherwise.\n"
  32. "\n"
  33. "Usage:\n"
  34. " fsts-to-transcripts [options] <fsts-rspecifier>"
  35. " <transcriptions-wspecifier>\n"
  36. "e.g.:\n"
  37. " fsts-to-transcripts ark:train.fsts ark,t:train.text\n";
  38. ParseOptions po(usage);
  39. bool output_side = true;
  40. po.Register("output-side", &output_side, "If true, extract the symbols on "
  41. "the output side of the FSTs, else the input side.");
  42. po.Read(argc, argv);
  43. if (po.NumArgs() != 2) {
  44. po.PrintUsage();
  45. exit(1);
  46. }
  47. std::string fst_rspecifier = po.GetArg(1),
  48. transcript_wspecifier = po.GetArg(2);
  49. SequentialTableReader<VectorFstHolder> fst_reader(fst_rspecifier);
  50. Int32VectorWriter transcript_writer(transcript_wspecifier);
  51. int32 n_done = 0, n_err = 0;
  52. for (; !fst_reader.Done(); fst_reader.Next()) {
  53. std::string key = fst_reader.Key();
  54. const VectorFst<StdArc> &fst = fst_reader.Value();
  55. VectorFst<StdArc> shortest_path;
  56. ShortestPath(fst, &shortest_path); // the OpenFst algorithm ShortestPath.
  57. if (shortest_path.NumStates() == 0) {
  58. KALDI_WARN << "Input FST (after shortest path) was empty. Producing "
  59. << "no output for key " << key;
  60. n_err++;
  61. continue;
  62. }
  63. std::vector<int32> transcript;
  64. bool ans;
  65. if (output_side) ans = fst::GetLinearSymbolSequence<StdArc, int32>(
  66. shortest_path, NULL, &transcript, NULL);
  67. else
  68. ans = fst::GetLinearSymbolSequence<StdArc, int32>(
  69. shortest_path, &transcript, NULL, NULL);
  70. if (!ans) {
  71. KALDI_ERR << "GetLinearSymbolSequence returned false (code error);";
  72. }
  73. transcript_writer.Write(key, transcript);
  74. n_done++;
  75. }
  76. KALDI_LOG << "Converted " << n_done << " FSTs, " << n_err << " with errors";
  77. return (n_done != 0 ? 0 : 1);
  78. } catch(const std::exception &e) {
  79. std::cerr << e.what();
  80. return -1;
  81. }
  82. }