fsts-to-transcripts.cc
Go to the documentation of this file.
1 // fstbin/fsts-to-transcripts.cc
2 
3 // Copyright 2012-2013 Johns Hopkins University (Authors: Guoguo Chen,
4 // Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "fstext/fstext-utils.h"
25 #include "fstext/kaldi-fst-io.h"
26 
27 
28 int main(int argc, char *argv[]) {
29  try {
30  using namespace kaldi;
31  using namespace fst;
32  typedef kaldi::int32 int32;
33  typedef kaldi::uint64 uint64;
34 
35  const char *usage =
36  "Reads a table of FSTs; for each element, finds the best path and \n"
37  "prints out the output-symbol sequence (if --output-side=true), or \n"
38  "input-symbol sequence otherwise.\n"
39  "\n"
40  "Usage:\n"
41  " fsts-to-transcripts [options] <fsts-rspecifier>"
42  " <transcriptions-wspecifier>\n"
43  "e.g.:\n"
44  " fsts-to-transcripts ark:train.fsts ark,t:train.text\n";
45 
46  ParseOptions po(usage);
47 
48  bool output_side = true;
49 
50  po.Register("output-side", &output_side, "If true, extract the symbols on "
51  "the output side of the FSTs, else the input side.");
52 
53  po.Read(argc, argv);
54 
55  if (po.NumArgs() != 2) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  std::string fst_rspecifier = po.GetArg(1),
61  transcript_wspecifier = po.GetArg(2);
62 
63 
64  SequentialTableReader<VectorFstHolder> fst_reader(fst_rspecifier);
65  Int32VectorWriter transcript_writer(transcript_wspecifier);
66 
67  int32 n_done = 0, n_err = 0;
68  for (; !fst_reader.Done(); fst_reader.Next()) {
69  std::string key = fst_reader.Key();
70  const VectorFst<StdArc> &fst = fst_reader.Value();
71 
72 
73  VectorFst<StdArc> shortest_path;
74  ShortestPath(fst, &shortest_path); // the OpenFst algorithm ShortestPath.
75 
76  if (shortest_path.NumStates() == 0) {
77  KALDI_WARN << "Input FST (after shortest path) was empty. Producing "
78  << "no output for key " << key;
79  n_err++;
80  continue;
81  }
82 
83  std::vector<int32> transcript;
84  bool ans;
85  if (output_side) ans = fst::GetLinearSymbolSequence<StdArc, int32>(
86  shortest_path, NULL, &transcript, NULL);
87  else
88  ans = fst::GetLinearSymbolSequence<StdArc, int32>(
89  shortest_path, &transcript, NULL, NULL);
90  if (!ans) {
91  KALDI_ERR << "GetLinearSymbolSequence returned false (code error);";
92  }
93  transcript_writer.Write(key, transcript);
94  n_done++;
95  }
96 
97  KALDI_LOG << "Converted " << n_done << " FSTs, " << n_err << " with errors";
98  return (n_done != 0 ? 0 : 1);
99  } catch(const std::exception &e) {
100  std::cerr << e.what();
101  return -1;
102  }
103 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int main(int argc, char *argv[])
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_LOG
Definition: kaldi-error.h:153