decoder-wrappers.h
Go to the documentation of this file.
1 // decoder/decoder-wrappers.h
2 
3 // Copyright 2014 Johns Hopkins University (author: Daniel Povey)
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 #ifndef KALDI_DECODER_DECODER_WRAPPERS_H_
21 #define KALDI_DECODER_DECODER_WRAPPERS_H_
22 
23 #include "itf/options-itf.h"
27 
28 // This header contains declarations from various convenience functions that are called
29 // from binary-level programs such as gmm-decode-faster.cc, gmm-align-compiled.cc, and
30 // so on.
31 
32 namespace kaldi {
33 
34 
35 struct AlignConfig {
38  bool careful;
39 
40  AlignConfig(): beam(200.0), retry_beam(0.0), careful(false) { }
41 
42  void Register(OptionsItf *opts) {
43  opts->Register("beam", &beam, "Decoding beam used in alignment");
44  opts->Register("retry-beam", &retry_beam,
45  "Decoding beam for second try at alignment");
46  opts->Register("careful", &careful,
47  "If true, do 'careful' alignment, which is better at detecting "
48  "alignment failure (involves loop to start of decoding graph).");
49  }
50 };
51 
52 
60  const AlignConfig &config,
61  const std::string &utt,
62  BaseFloat acoustic_scale, // affects scores written to scores_writer, if
63  // present
64  fst::VectorFst<fst::StdArc> *fst, // non-const in case config.careful ==
65  // true, we add loop.
66  DecodableInterface *decodable, // not const but is really an input.
67  Int32VectorWriter *alignment_writer,
68  BaseFloatWriter *scores_writer,
69  int32 *num_done,
70  int32 *num_error,
71  int32 *num_retried,
72  double *tot_like,
73  int64 *frame_count,
74  BaseFloatVectorWriter *per_frame_acwt_writer = NULL);
75 
76 
77 
90  fst::VectorFst<fst::StdArc> *fst);
91 
93 template <typename FST>
95  LatticeIncrementalDecoderTpl<FST> &decoder, // not const but is really an input.
96  DecodableInterface &decodable, // not const but is really an input.
97  const TransitionModel &trans_model,
98  const fst::SymbolTable *word_syms,
99  std::string utt,
100  double acoustic_scale,
101  bool determinize,
102  bool allow_partial,
103  Int32VectorWriter *alignments_writer,
104  Int32VectorWriter *words_writer,
105  CompactLatticeWriter *compact_lattice_writer,
106  LatticeWriter *lattice_writer,
107  double *like_ptr); // puts utterance's likelihood in like_ptr on success.
108 
109 
120 template <typename FST>
122  LatticeFasterDecoderTpl<FST> &decoder, // not const but is really an input.
123  DecodableInterface &decodable, // not const but is really an input.
124  const TransitionModel &trans_model,
125  const fst::SymbolTable *word_syms,
126  std::string utt,
127  double acoustic_scale,
128  bool determinize,
129  bool allow_partial,
130  Int32VectorWriter *alignments_writer,
131  Int32VectorWriter *words_writer,
132  CompactLatticeWriter *compact_lattice_writer,
133  LatticeWriter *lattice_writer,
134  double *like_ptr); // puts utterance's likelihood in like_ptr on success.
135 
136 
143  public:
144  // Initializer sets various variables.
145  // NOTE: we "take ownership" of "decoder" and "decodable". These
146  // are deleted by the destructor. On error, "num_err" is incremented.
148  LatticeFasterDecoder *decoder,
149  DecodableInterface *decodable,
150  const TransitionModel &trans_model,
151  const fst::SymbolTable *word_syms,
152  const std::string &utt,
153  BaseFloat acoustic_scale,
154  bool determinize,
155  bool allow_partial,
156  Int32VectorWriter *alignments_writer,
157  Int32VectorWriter *words_writer,
158  CompactLatticeWriter *compact_lattice_writer,
159  LatticeWriter *lattice_writer,
160  double *like_sum, // on success, adds likelihood to this.
161  int64 *frame_sum, // on success, adds #frames to this.
162  int32 *num_done, // on success (including partial decode), increments this.
163  int32 *num_err, // on failure, increments this.
164  int32 *num_partial); // If partial decode (final-state not reached), increments this.
165  void operator () (); // The decoding happens here.
166  ~DecodeUtteranceLatticeFasterClass(); // Output happens here.
167  private:
168  // The following variables correspond to inputs:
172  const fst::SymbolTable *word_syms_;
173  std::string utt_;
181  double *like_sum_;
182  int64 *frame_sum_;
186 
187  // The following variables are stored by the computation.
188  bool computed_; // operator () was called.
189  bool success_; // decoding succeeded (possibly partial)
190  bool partial_; // decoding was partial.
191  CompactLattice *clat_; // Stored output, if determinize_ == true.
192  Lattice *lat_; // Stored output, if determinize_ == false.
193 };
194 
195 // This function DecodeUtteranceLatticeSimple is used in several decoders, and
196 // we have moved it here. Note: this is really "binary-level" code as it
197 // involves table readers and writers; we've just put it here as there is no
198 // other obvious place to put it. If determinize == false, it writes to
199 // lattice_writer, else to compact_lattice_writer. The writers for
200 // alignments and words will only be written to if they are open.
202  LatticeSimpleDecoder &decoder, // not const but is really an input.
203  DecodableInterface &decodable, // not const but is really an input.
204  const TransitionModel &trans_model,
205  const fst::SymbolTable *word_syms,
206  std::string utt,
207  double acoustic_scale,
208  bool determinize,
209  bool allow_partial,
210  Int32VectorWriter *alignments_writer,
211  Int32VectorWriter *words_writer,
212  CompactLatticeWriter *compact_lattice_writer,
213  LatticeWriter *lattice_writer,
214  double *like_ptr); // puts utterance's likelihood in like_ptr on success.
215 
216 
217 
218 } // end namespace kaldi.
219 
220 
221 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool DecodeUtteranceLatticeIncremental(LatticeIncrementalDecoderTpl< FST > &decoder, DecodableInterface &decodable, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, std::string utt, double acoustic_scale, bool determinize, bool allow_partial, Int32VectorWriter *alignment_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_ptr)
TODO.
DecodableInterface provides a link between the (acoustic-modeling and feature-processing) code and th...
Definition: decodable-itf.h:82
CompactLatticeWriter * compact_lattice_writer_
This is an extention to the "normal" lattice-generating decoder.
void Register(OptionsItf *opts)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
bool DecodeUtteranceLatticeFaster(LatticeFasterDecoderTpl< FST > &decoder, DecodableInterface &decodable, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, std::string utt, double acoustic_scale, bool determinize, bool allow_partial, Int32VectorWriter *alignment_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_ptr)
This function DecodeUtteranceLatticeFaster is used in several decoders, and we have moved it here...
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
This class basically does the same job as the function DecodeUtteranceLatticeFaster, but in a way that allows us to build a multi-threaded command line program more easily.
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
Simplest possible decoder, included largely for didactic purposes and as a means to debug more highly...
bool DecodeUtteranceLatticeSimple(LatticeSimpleDecoder &decoder, DecodableInterface &decodable, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, std::string utt, double acoustic_scale, bool determinize, bool allow_partial, Int32VectorWriter *alignment_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_ptr)
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
void ModifyGraphForCarefulAlignment(fst::VectorFst< fst::StdArc > *fst)
This function modifies the decoding graph for what we call "careful alignment".
This is the "normal" lattice-generating decoder.
void AlignUtteranceWrapper(const AlignConfig &config, const std::string &utt, BaseFloat acoustic_scale, fst::VectorFst< fst::StdArc > *fst, DecodableInterface *decodable, Int32VectorWriter *alignment_writer, BaseFloatWriter *scores_writer, int32 *num_done, int32 *num_error, int32 *num_retried, double *tot_like, int64 *frame_count, BaseFloatVectorWriter *per_frame_acwt_writer)
AlignUtteranceWapper is a wrapper for alignment code used in training, that is called from many diffe...