make-h-transducer.cc
Go to the documentation of this file.
1 // bin/make-h-transducer.cc
2 // Copyright 2009-2011 Microsoft Corporation
3 
4 // See ../../COPYING for clarification regarding multiple authors
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
14 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
15 // MERCHANTABLITY OR NON-INFRINGEMENT.
16 // See the Apache 2 License for the specific language governing permissions and
17 // limitations under the License.
18 
19 #include "hmm/transition-model.h"
20 #include "hmm/hmm-utils.h"
21 #include "tree/context-dep.h"
22 #include "util/common-utils.h"
23 #include "fst/fstlib.h"
24 #include "fstext/table-matcher.h"
25 #include "fstext/fstext-utils.h"
26 #include "fstext/context-fst.h"
27 
28 
29 int main(int argc, char *argv[]) {
30  try {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  using fst::SymbolTable;
34  using fst::VectorFst;
35  using fst::StdArc;
36 
37  const char *usage =
38  "Make H transducer from transition-ids to context-dependent phones, \n"
39  " without self-loops [use add-self-loops to add them]\n"
40  "Usage: make-h-transducer <ilabel-info-file> <tree-file> <transition-gmm/acoustic-model> [<H-fst-out>]\n"
41  "e.g.: \n"
42  " make-h-transducer ilabel_info 1.tree 1.mdl > H.fst\n";
43  ParseOptions po(usage);
44 
45  HTransducerConfig hcfg;
46  std::string disambig_out_filename;
47  hcfg.Register(&po);
48  po.Register("disambig-syms-out", &disambig_out_filename, "List of disambiguation symbols on input of H [to be output from this program]");
49 
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() < 3 || po.NumArgs() > 4) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string ilabel_info_filename = po.GetArg(1);
58  std::string tree_filename = po.GetArg(2);
59  std::string model_filename = po.GetArg(3);
60  std::string fst_out_filename;
61  if (po.NumArgs() >= 4) fst_out_filename = po.GetArg(4);
62  if (fst_out_filename == "-") fst_out_filename = "";
63 
64  std::vector<std::vector<int32> > ilabel_info;
65  {
66  bool binary_in;
67  Input ki(ilabel_info_filename, &binary_in);
68  fst::ReadILabelInfo(ki.Stream(), binary_in, &ilabel_info);
69  }
70 
71  ContextDependency ctx_dep;
72  ReadKaldiObject(tree_filename, &ctx_dep);
73 
74  TransitionModel trans_model;
75  ReadKaldiObject(model_filename, &trans_model);
76 
77  std::vector<int32> disambig_syms_out;
78 
79  // The work gets done here.
80  fst::VectorFst<fst::StdArc> *H = GetHTransducer (ilabel_info,
81  ctx_dep,
82  trans_model,
83  hcfg,
84  &disambig_syms_out);
85 #if _MSC_VER
86  if (fst_out_filename == "")
87  _setmode(_fileno(stdout), _O_BINARY);
88 #endif
89 
90  if (disambig_out_filename != "") { // if option specified..
91  if (disambig_out_filename == "-")
92  disambig_out_filename = "";
93  if (! WriteIntegerVectorSimple(disambig_out_filename, disambig_syms_out))
94  KALDI_ERR << "Could not write disambiguation symbols to "
95  << (disambig_out_filename == "" ?
96  "standard output" : disambig_out_filename);
97  }
98 
99  if (! H->Write(fst_out_filename) )
100  KALDI_ERR << "make-h-transducer: error writing FST to "
101  << (fst_out_filename == "" ?
102  "standard output" : fst_out_filename);
103 
104  delete H;
105  return 0;
106  } catch(const std::exception &e) {
107  std::cerr << e.what();
108  return -1;
109  }
110 }
111 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void Register(OptionsItf *opts)
Definition: hmm-utils.h:47
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
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
void ReadILabelInfo(std::istream &is, bool binary, vector< vector< int32 > > *info)
Utility function for reading ilabel-info vectors from disk.
Definition: context-fst.cc:335
std::istream & Stream()
Definition: kaldi-io.cc:826
Configuration class for the GetHTransducer() function; see The HTransducerConfig configuration class ...
Definition: hmm-utils.h:36
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.
fst::VectorFst< fst::StdArc > * GetHTransducer(const std::vector< std::vector< int32 > > &ilabel_info, const ContextDependencyInterface &ctx_dep, const TransitionModel &trans_model, const HTransducerConfig &config, std::vector< int32 > *disambig_syms_left)
Returns the H tranducer; result owned by caller.
Definition: hmm-utils.cc:254
int NumArgs() const
Number of positional parameters (c.f. argc-1).
bool WriteIntegerVectorSimple(const std::string &wxfilename, const std::vector< int32 > &list)
WriteToList attempts to write this list of integers, one per line, to the given file, in text format.
int main(int argc, char *argv[])