fsts-union.cc File Reference
Include dependency graph for fsts-union.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 27 of file fsts-union.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

27  {
28  try {
29  using namespace kaldi;
30  using namespace fst;
31  typedef kaldi::int32 int32;
32  typedef kaldi::uint64 uint64;
33 
34  const char *usage =
35  "Reads a kaldi archive of FSTs. Performs the FST operation union on\n"
36  "all fsts sharing the same key. Assumes the archive is sorted by key.\n"
37  "\n"
38  "Usage: fsts-union [options] <fsts-rspecifier> <fsts-wspecifier>\n"
39  " e.g.: fsts-union ark:keywords_tmp.fsts ark,t:keywords.fsts\n"
40  "\n"
41  "see also: fstunion (from the OpenFst toolkit)\n";
42 
43  ParseOptions po(usage);
44 
45  po.Read(argc, argv);
46 
47  if (po.NumArgs() != 2) {
48  po.PrintUsage();
49  exit(1);
50  }
51 
52  std::string fsts_rspecifier = po.GetArg(1),
53  fsts_wspecifier = po.GetArg(2);
54 
55 
56  SequentialTableReader<VectorFstHolder> fst_reader(fsts_rspecifier);
57  TableWriter<VectorFstHolder> fst_writer(fsts_wspecifier);
58 
59  int32 n_out_done = 0,
60  n_in_done = 0;
61  std::string res_key = "";
62  VectorFst<StdArc> res_fst;
63 
64  for (; !fst_reader.Done(); fst_reader.Next()) {
65  std::string key = fst_reader.Key();
66  VectorFst<StdArc> fst(fst_reader.Value());
67 
68  n_in_done++;
69  if (key == res_key) {
70  fst::Union(&res_fst, fst);
71  } else {
72  if (res_key != "") {
73  VectorFst<StdArc> out_fst;
74  fst::Determinize(res_fst, &out_fst);
75  fst::Minimize(&out_fst);
76  fst::RmEpsilon(&out_fst);
77  fst_writer.Write(res_key, out_fst);
78  n_out_done++;
79  }
80  res_fst = fst;
81  res_key = key;
82  }
83  }
84  if (res_key != "") {
85  VectorFst<StdArc> out_fst;
86  fst::Determinize(res_fst, &out_fst);
87  fst::Minimize(&out_fst);
88  fst::RmEpsilon(&out_fst);
89  fst_writer.Write(res_key, out_fst);
90  n_out_done++;
91  }
92 
93  KALDI_LOG << "Applied fst union on " << n_in_done
94  << " FSTs, produced " << n_out_done << " FSTs";
95  return (n_out_done != 0 ? 0 : 1);
96  } catch(const std::exception &e) {
97  std::cerr << e.what();
98  return -1;
99  }
100 }
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
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
#define KALDI_LOG
Definition: kaldi-error.h:153