fstcomposecontext.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "fst/fstlib.h"
#include "fstext/context-fst.h"
#include "fstext/grammar-context-fst.h"
#include "fstext/fstext-utils.h"
#include "fstext/kaldi-fst-io.h"
Include dependency graph for fstcomposecontext.cc:

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 66 of file fstcomposecontext.cc.

References fst::ComposeContext(), fst::ComposeContextLeftBiphone(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), rnnlm::i, KALDI_ERR, KALDI_WARN, ParseOptions::NumArgs(), kaldi::PrintableRxfilename(), kaldi::PrintableWxfilename(), ParseOptions::PrintUsage(), ParseOptions::Read(), fst::ReadFstKaldi(), kaldi::ReadIntegerVectorSimple(), ParseOptions::Register(), fst::WriteFstKaldi(), fst::WriteILabelInfo(), and kaldi::WriteIntegerVectorSimple().

66  {
67  try {
68  using namespace kaldi;
69  using namespace fst;
70  using kaldi::int32;
71  /*
72  # fstcomposecontext composes efficiently with a context fst
73  # that it generates. Without --disambig-syms specified, it
74  # assumes that all input symbols of in.fst are phones.
75  # It adds the subsequential symbol itself (it does not
76  # appear in the output so doesn't need to be specified by the user).
77  # the disambig.list is a list of disambiguation symbols on the LHS
78  # of in.fst. The symbols on the LHS of out.fst are indexes into
79  # the ilabels.list file, which is a kaldi-format file containing a
80  # vector<vector<int32> >, which specifies what the labels mean in
81  # terms of windows of symbols.
82  fstcomposecontext ilabels.sym [ in.fst [ out.fst ] ]
83  --disambig-syms=disambig.list
84  --context-size=3
85  --central-position=1
86  --binary=false
87  */
88 
89  const char *usage =
90  "Composes on the left with a dynamically created context FST\n"
91  "\n"
92  "Usage: fstcomposecontext <ilabels-output-file> [<in.fst> [<out.fst>] ]\n"
93  "E.g: fstcomposecontext ilabels.sym < LG.fst > CLG.fst\n";
94 
95 
96  ParseOptions po(usage);
97  bool binary = true;
98  std::string disambig_rxfilename,
99  disambig_wxfilename;
100  int32 context_width = 3, central_position = 1;
101  int32 nonterm_phones_offset = -1;
102  po.Register("binary", &binary,
103  "If true, output ilabels-output-file in binary format");
104  po.Register("read-disambig-syms", &disambig_rxfilename,
105  "List of disambiguation symbols on input of in.fst");
106  po.Register("write-disambig-syms", &disambig_wxfilename,
107  "List of disambiguation symbols on input of out.fst");
108  po.Register("context-size", &context_width, "Size of phone context window");
109  po.Register("central-position", &central_position,
110  "Designated central position in context window");
111  po.Register("nonterm-phones-offset", &nonterm_phones_offset,
112  "The integer id of #nonterm_bos in your phones.txt, if present "
113  "(only relevant for grammar-FST construction, see "
114  "doc/grammar.dox");
115 
116  po.Read(argc, argv);
117 
118  if (po.NumArgs() < 1 || po.NumArgs() > 3) {
119  po.PrintUsage();
120  exit(1);
121  }
122 
123  std::string ilabels_out_filename = po.GetArg(1),
124  fst_in_filename = po.GetOptArg(2),
125  fst_out_filename = po.GetOptArg(3);
126 
127  VectorFst<StdArc> *fst = ReadFstKaldi(fst_in_filename);
128 
129  if ( (disambig_wxfilename != "") && (disambig_rxfilename == "") )
130  KALDI_ERR << "fstcomposecontext: cannot specify --write-disambig-syms if "
131  "not specifying --read-disambig-syms\n";
132 
133  std::vector<int32> disambig_in;
134  if (disambig_rxfilename != "")
135  if (!ReadIntegerVectorSimple(disambig_rxfilename, &disambig_in))
136  KALDI_ERR << "fstcomposecontext: Could not read disambiguation symbols from "
137  << PrintableRxfilename(disambig_rxfilename);
138 
139  if (disambig_in.empty()) {
140  KALDI_WARN << "Disambiguation symbols list is empty; this likely "
141  << "indicates an error in data preparation.";
142  }
143 
144  std::vector<std::vector<int32> > ilabels;
145  VectorFst<StdArc> composed_fst;
146 
147  // Work gets done here (see context-fst.h)
148  if (nonterm_phones_offset < 0) {
149  // The normal case.
150  ComposeContext(disambig_in, context_width, central_position,
151  fst, &composed_fst, &ilabels);
152  } else {
153  // The grammar-FST case. See ../doc/grammar.dox for an intro.
154  if (context_width != 2 || central_position != 1) {
155  KALDI_ERR << "Grammar-fst graph creation only supports models with left-"
156  "biphone context. (--nonterm-phones-offset option was supplied).";
157  }
158  ComposeContextLeftBiphone(nonterm_phones_offset, disambig_in,
159  *fst, &composed_fst, &ilabels);
160  }
161  WriteILabelInfo(Output(ilabels_out_filename, binary).Stream(),
162  binary, ilabels);
163 
164  if (disambig_wxfilename != "") {
165  std::vector<int32> disambig_out;
166  for (size_t i = 0; i < ilabels.size(); i++)
167  if (ilabels[i].size() == 1 && ilabels[i][0] <= 0)
168  disambig_out.push_back(static_cast<int32>(i));
169  if (!WriteIntegerVectorSimple(disambig_wxfilename, disambig_out)) {
170  std::cerr << "fstcomposecontext: Could not write disambiguation symbols to "
171  << PrintableWxfilename(disambig_wxfilename) << '\n';
172  return 1;
173  }
174  }
175 
176  WriteFstKaldi(composed_fst, fst_out_filename);
177  delete fst;
178  return 0;
179  } catch(const std::exception &e) {
180  std::cerr << e.what();
181  return -1;
182  }
183 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void WriteILabelInfo(std::ostream &os, bool binary, const vector< vector< int32 > > &info)
Utility function for writing ilabel-info vectors to disk.
Definition: context-fst.cc:325
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
kaldi::int32 int32
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void ComposeContextLeftBiphone(int32 nonterm_phones_offset, const vector< int32 > &disambig_syms_in, const VectorFst< StdArc > &ifst, VectorFst< StdArc > *ofst, std::vector< std::vector< int32 > > *ilabels)
This is a variant of the function ComposeContext() which is to be used with our "grammar FST" framewo...
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
void ComposeContext(const vector< int32 > &disambig_syms_in, int32 context_width, int32 central_position, VectorFst< StdArc > *ifst, VectorFst< StdArc > *ofst, vector< vector< int32 > > *ilabels_out, bool project_ifst)
Used in the command-line tool fstcomposecontext.
Definition: context-fst.cc:246
void WriteFstKaldi(std::ostream &os, bool binary, const VectorFst< Arc > &t)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
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.
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
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
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...