add-self-loops.cc
Go to the documentation of this file.
1 // bin/add-self-loops.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2015 Johns Hopkins University (author: 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 #include "hmm/transition-model.h"
22 #include "hmm/hmm-utils.h"
23 #include "tree/context-dep.h"
24 #include "util/common-utils.h"
25 #include "fst/fstlib.h"
26 #include "fstext/table-matcher.h"
27 #include "fstext/fstext-utils.h"
28 #include "fstext/context-fst.h"
29 
32 int main(int argc, char *argv[]) {
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  bool check_no_self_loops = true;
101 
102  // The work gets done here.
103  AddSelfLoops(trans_model,
104  disambig_syms_in,
105  self_loop_scale,
106  reorder, check_no_self_loops, 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 }
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].
fst::StdArc StdArc
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
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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
int main(int argc, char *argv[])
Add self-loops and transition probabilities to transducer, expanding to transition-ids.
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).
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