All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
add-self-loops.cc File Reference
#include "hmm/transition-model.h"
#include "hmm/hmm-utils.h"
#include "tree/context-dep.h"
#include "util/common-utils.h"
#include "fst/fstlib.h"
#include "fstext/table-matcher.h"
#include "fstext/fstext-utils.h"
#include "fstext/context-fst.h"
Include dependency graph for add-self-loops.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 Add self-loops and transition probabilities to transducer, expanding to transition-ids. More...
 

Function Documentation

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

Add self-loops and transition probabilities to transducer, expanding to transition-ids.

Definition at line 32 of file add-self-loops.cc.

References kaldi::AddSelfLoops(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), KALDI_ERR, ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadIntegerVectorSimple(), kaldi::ReadKaldiObject(), and ParseOptions::Register().

32  {
33  try {
34  using namespace kaldi;
35  typedef kaldi::int32 int32;
36  using fst::SymbolTable;
37  using fst::VectorFst;
38  using fst::StdArc;
39 
40  const char *usage =
41  "Add self-loops and transition probabilities to transducer. Input transducer\n"
42  "has transition-ids on the input side, but only the forward transitions, not the\n"
43  "self-loops. Output transducer has transition-ids on the input side, but with\n"
44  "self-loops added. The --reorder option controls whether the loop is added before\n"
45  "the forward transition (if false), or afterward (if true). The default (true)\n"
46  "is recommended as the decoding will in that case be faster.\n"
47  "Usage: add-self-loops [options] transition-gmm/acoustic-model [fst-in] [fst-out]\n"
48  "e.g.: \n"
49  " add-self-loops --self-loop-scale=0.1 1.mdl HCLGa.fst HCLG.fst\n"
50  "or: add-self-loops --self-loop-scale=0.1 1.mdl <HCLGa.fst >HCLG.fst\n";
51 
52  BaseFloat self_loop_scale = 1.0;
53  bool reorder = true;
54  std::string disambig_in_filename;
55 
56  ParseOptions po(usage);
57  po.Register("self-loop-scale", &self_loop_scale,
58  "Scale for self-loop probabilities relative to LM.");
59  po.Register("disambig-syms", &disambig_in_filename,
60  "List of disambiguation symbols on input of fst-in [input file]");
61  po.Register("reorder", &reorder,
62  "If true, reorder symbols for more decoding efficiency");
63  po.Read(argc, argv);
64 
65  if (po.NumArgs() < 1 || po.NumArgs() > 3) {
66  po.PrintUsage();
67  exit(1);
68  }
69 
70  std::string model_in_filename = po.GetArg(1);
71  std::string fst_in_filename = po.GetOptArg(2);
72  if (fst_in_filename == "-") fst_in_filename = "";
73  std::string fst_out_filename = po.GetOptArg(3);
74  if (fst_out_filename == "-") fst_out_filename = "";
75 #if _MSC_VER
76  if (fst_in_filename == "")
77  _setmode(_fileno(stdin), _O_BINARY);
78  if (fst_out_filename == "")
79  _setmode(_fileno(stdout), _O_BINARY);
80 #endif
81 
82  std::vector<int32> disambig_syms_in;
83  if (disambig_in_filename != "") {
84  if (disambig_in_filename == "-") disambig_in_filename = "";
85  if (!ReadIntegerVectorSimple(disambig_in_filename, &disambig_syms_in))
86  KALDI_ERR << "add-self-loops: could not read disambig symbols from "
87  <<(disambig_in_filename == "" ?
88  "standard input" : disambig_in_filename);
89  }
90 
91  TransitionModel trans_model;
92  ReadKaldiObject(model_in_filename, &trans_model);
93 
94 
95  fst::VectorFst<fst::StdArc> *fst =
96  fst::VectorFst<fst::StdArc>::Read(fst_in_filename);
97  if (!fst)
98  KALDI_ERR << "add-self-loops: error reading input FST.";
99 
100 
101  // The work gets done here.
102  AddSelfLoops(trans_model,
103  disambig_syms_in,
104  self_loop_scale,
105  reorder,
106  fst);
107 
108  if (! fst->Write(fst_out_filename) )
109  KALDI_ERR << "add-self-loops: error writing FST to "
110  << (fst_out_filename == "" ?
111  "standard output" : fst_out_filename);
112 
113  delete fst;
114  return 0;
115  } catch(const std::exception &e) {
116  std::cerr << e.what();
117  return -1;
118  }
119 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
Definition: graph.dox:21
void AddSelfLoops(const TransitionModel &trans_model, const std::vector< int32 > &disambig_syms, BaseFloat self_loop_scale, bool reorder, fst::VectorFst< fst::StdArc > *fst)
For context, see AddSelfLoops().
Definition: hmm-utils.cc:559
fst::StdArc StdArc
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:829
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
#define KALDI_ERR
Definition: kaldi-error.h:127
bool ReadIntegerVectorSimple(std::string rxfilename, std::vector< int32 > *list)
ReadFromList attempts to read this list of integers, one per line, from the given file...