All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fstrhocompose.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "fst/fstlib.h"
#include "fstext/table-matcher.h"
#include "fstext/fstext-utils.h"
#include "fstext/kaldi-fst-io.h"
Include dependency graph for fstrhocompose.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 56 of file fstrhocompose.cc.

References kaldi::ClassifyRspecifier(), kaldi::ClassifyWspecifier(), kaldi::ConvertStringToInteger(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, kaldi::kNoWspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), fst::ReadFstKaldi(), fst::RhoCompose(), SequentialTableReader< Holder >::Value(), TableWriter< Holder >::Write(), and fst::WriteFstKaldi().

56  {
57  try {
58  using namespace kaldi;
59  using namespace fst;
60  using kaldi::int32;
61  /*
62  fstrhocompose does composition, but treats the second FST
63  specially; the symbol "rho" is taken whenever the
64  composition algorithm fails to find a match for a label
65  in the second FST. We rewrite "rho" with whatever label on the
66  first FST we matched. This is done on both sides of the output
67  if the rho FST was an acceptor, and just on the input side otherwise
68  (I think)... but typically the rho FST will be an acceptor.
69  We can add options for this later if needed.
70  */
71 
72  const char *usage =
73  "Composition, where the right FST has \"rest\" (rho) transition\n"
74  "that are only taken where there was no match of a \"real\" label\n"
75  "You supply the label corresponding to rho.\n"
76  "\n"
77  "Usage: fstrhocompose rho-label (fst1-rxfilename|fst1-rspecifier) "
78  "(fst2-rxfilename|fst2-rspecifier) [(out-rxfilename|out-rspecifier)]\n"
79  "E.g.: fstrhocompose 54 a.fst b.fst c.fst\n"
80  "or: fstrhocompose 11 ark:a.fsts G.fst ark:b.fsts\n";
81 
82  ParseOptions po(usage);
83 
84  po.Read(argc, argv);
85 
86  if (po.NumArgs() < 3 || po.NumArgs() > 4) {
87  po.PrintUsage();
88  exit(1);
89  }
90 
91  std::string
92  rho_str = po.GetArg(1),
93  fst1_in_str = po.GetArg(2),
94  fst2_in_str = po.GetArg(3),
95  fst_out_str = po.GetOptArg(4);
96 
97  bool is_table_1 =
98  (ClassifyRspecifier(fst1_in_str, NULL, NULL) != kNoRspecifier),
99  is_table_2 =
100  (ClassifyRspecifier(fst2_in_str, NULL, NULL) != kNoRspecifier),
101  is_table_out =
102  (ClassifyWspecifier(fst_out_str, NULL, NULL, NULL) != kNoWspecifier);
103 
104  int32 rho_label;
105  if (!ConvertStringToInteger(rho_str, &rho_label)
106  || rho_label <= 0)
107  KALDI_ERR << "Invalid first argument (rho label), expect positive integer.";
108 
109  if (is_table_out != (is_table_1 || is_table_2))
110  KALDI_ERR << "Incompatible combination of archives and files";
111 
112  if (!is_table_1 && !is_table_2) { // Only dealing with files...
113  VectorFst<StdArc> *fst1 = ReadFstKaldi(fst1_in_str);
114 
115  VectorFst<StdArc> *fst2 = ReadFstKaldi(fst2_in_str);
116 
117  VectorFst<StdArc> composed_fst;
118 
119  RhoCompose(*fst1, *fst2, rho_label, &composed_fst);
120 
121  delete fst1;
122  delete fst2;
123 
124  WriteFstKaldi(composed_fst, fst_out_str);
125  return 0;
126  } else if (is_table_1 && !is_table_2) {
127 
128  VectorFst<StdArc> *fst2 = ReadFstKaldi(fst2_in_str);
129 
130  SequentialTableReader<VectorFstHolder> fst1_reader(fst1_in_str);
131  TableWriter<VectorFstHolder> fst_writer(fst_out_str);
132  int32 n_done = 0;
133  for (; !fst1_reader.Done(); fst1_reader.Next(), n_done++) {
134  VectorFst<StdArc> fst1(fst1_reader.Value());
135  VectorFst<StdArc> fst_out;
136  RhoCompose(fst1, *fst2, rho_label, &fst_out);
137  fst_writer.Write(fst1_reader.Key(), fst_out);
138  }
139  KALDI_LOG << "Composed " << n_done << " FSTs.";
140  return (n_done != 0 ? 0 : 1);
141  } else {
142  KALDI_ERR << "The combination of tables/non-tables that you "
143  << "supplied is not currently supported. Either implement this, "
144  << "ask the maintainers to implement it, or call this program "
145  << "differently.";
146  }
147  } catch(const std::exception &e) {
148  std::cerr << e.what();
149  return -1;
150  }
151 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
Definition: text-utils.h:118
Definition: graph.dox:21
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
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_ERR
Definition: kaldi-error.h:127
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *wxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
void WriteFstKaldi(std::ostream &os, bool binary, const VectorFst< Arc > &t)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
void RhoCompose(const Fst< Arc > &fst1, const Fst< Arc > &fst2, typename Arc::Label rho_label, MutableFst< Arc > *ofst)
#define KALDI_LOG
Definition: kaldi-error.h:133