fstaddselfloops.cc
Go to the documentation of this file.
1 // fstbin/fstaddselfloops.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "fst/fstlib.h"
25 #include "fstext/fstext-utils.h"
26 #include "fstext/kaldi-fst-io.h"
27 
28 /* some test examples:
29  pushd ~/tmpdir
30  ( echo 3; echo 4) > in.list
31  ( echo 5; echo 6) > out.list
32  ( echo "0 0 0 0"; echo "0 0" ) | fstcompile | fstaddselfloops in.list out.list | fstprint
33  ( echo "0 1 0 1"; echo " 0 2 1 0"; echo "1 0"; echo "2 0"; ) | fstcompile | fstaddselfloops in.list out.list | fstprint
34 */
35 
36 int main(int argc, char *argv[]) {
37  try {
38  using namespace kaldi;
39  using namespace fst;
40  using kaldi::int32;
41 
42  const char *usage =
43  "Adds self-loops to states of an FST to propagate disambiguation symbols through it\n"
44  "They are added on each final state and each state with non-epsilon output symbols\n"
45  "on at least one arc out of the state. Useful in conjunction with predeterminize\n"
46  "\n"
47  "Usage: fstaddselfloops in-disambig-list out-disambig-list [in.fst [out.fst] ]\n"
48  "E.g: fstaddselfloops in.list out.list < in.fst > withloops.fst\n"
49  "in.list and out.list are lists of integers, one per line, of the\n"
50  "same length.\n";
51 
52  ParseOptions po(usage);
53  po.Read(argc, argv);
54 
55  if (po.NumArgs() < 2 || po.NumArgs() > 4) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  std::string disambig_in_rxfilename = po.GetArg(1),
61  disambig_out_rxfilename = po.GetArg(2),
62  fst_in_filename = po.GetOptArg(3),
63  fst_out_filename = po.GetOptArg(4);
64 
65  VectorFst<StdArc> *fst = ReadFstKaldi(fst_in_filename);
66 
67  std::vector<int32> disambig_in;
68  if (!ReadIntegerVectorSimple(disambig_in_rxfilename, &disambig_in))
69  KALDI_ERR << "fstaddselfloops: Could not read disambiguation symbols from "
70  << kaldi::PrintableRxfilename(disambig_in_rxfilename);
71 
72  std::vector<int32> disambig_out;
73  if (!ReadIntegerVectorSimple(disambig_out_rxfilename, &disambig_out))
74  KALDI_ERR << "fstaddselfloops: Could not read disambiguation symbols from "
75  << kaldi::PrintableRxfilename(disambig_out_rxfilename);
76 
77  if (disambig_in.size() != disambig_out.size())
78  KALDI_ERR << "fstaddselfloops: mismatch in size of disambiguation symbols";
79 
80  AddSelfLoops(fst, disambig_in, disambig_out);
81 
82  WriteFstKaldi(*fst, fst_out_filename);
83 
84  delete fst;
85 
86  return 0;
87  } catch(const std::exception &e) {
88  std::cerr << e.what();
89  return -1;
90  }
91  return 0;
92 }
93 
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].
void AddSelfLoops(const TransitionModel &trans_model, const std::vector< int32 > &disambig_syms, BaseFloat self_loop_scale, bool reorder, bool check_no_self_loops, fst::VectorFst< fst::StdArc > *fst)
For context, see AddSelfLoops().
Definition: hmm-utils.cc:602
int main(int argc, char *argv[])
kaldi::int32 int32
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
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
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).
void WriteFstKaldi(std::ostream &os, bool binary, const VectorFst< Arc > &t)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61
bool ReadIntegerVectorSimple(const std::string &rxfilename, std::vector< int32 > *list)
ReadFromList attempts to read this list of integers, one per line, from the given file...
std::string GetOptArg(int param) const