lattice-add-trans-probs.cc
Go to the documentation of this file.
1 // latbin/lattice-add-trans-probs.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
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 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "fstext/fstext-lib.h"
24 #include "lat/kaldi-lattice.h"
25 #include "lat/lattice-functions.h"
26 #include "hmm/transition-model.h"
27 #include "hmm/hmm-utils.h"
28 
29 int main(int argc, char *argv[]) {
30  try {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  typedef kaldi::int64 int64;
34  using fst::SymbolTable;
35  using fst::VectorFst;
36  using fst::StdArc;
37 
38  const char *usage =
39  "Add transition probabilities into graph part of lattice scores,\n"
40  "controlled by options --transition-scale and --self-loop-scale, which\n"
41  "for compatibility with the original graph, would normally be set to the same\n"
42  "values used in graph compilatoin\n"
43  "\n"
44  "Usage: lattice-add-trans-probs [options] model lattice-rspecifier lattice-wspecifier\n"
45  " e.g.: lattice-add-trans-probs --transition-scale=1.0 --self-loop-scale=0.1 1.mdl ark:in.lats ark:out.lats\n";
46 
47  ParseOptions po(usage);
48 
49  BaseFloat transition_scale = 1.0, self_loop_scale = 1.0;
50 
51  po.Register("transition-scale", &transition_scale,
52  "Scale for transition probabilities (excluding self-loops)");
53  po.Register("self-loop-scale", &self_loop_scale,
54  "Probability scale for self-loop vs. non-self-loop "
55  "probability mass.");
56 
57  po.Read(argc, argv);
58 
59  if (po.NumArgs() != 3) {
60  po.PrintUsage();
61  exit(1);
62  }
63 
64  std::string
65  model_rxfilename = po.GetArg(1),
66  lats_rspecifier = po.GetArg(2),
67  lats_wspecifier = po.GetArg(3);
68 
69  int32 n_done = 0;
70 
71  TransitionModel trans_model;
72 
73  ReadKaldiObject(model_rxfilename, &trans_model);
74 
75  SequentialLatticeReader lattice_reader(lats_rspecifier); // read as
76  // regular lattice.
77  CompactLatticeWriter clat_writer(lats_wspecifier); // write as compact.
78  for (; !lattice_reader.Done(); lattice_reader.Next(), n_done++) {
79  Lattice lat(lattice_reader.Value());
80  AddTransitionProbs(trans_model, transition_scale, self_loop_scale, &lat);
81  CompactLattice clat;
82  ConvertLattice(lat, &clat);
83  clat_writer.Write(lattice_reader.Key(), clat);
84  n_done++;
85  }
86  KALDI_LOG << "Done adding transition probabilities to " << n_done << " lattices.";
87  return (n_done != 0 ? 0 : 1);
88  } catch(const std::exception &e) {
89  std::cerr << e.what();
90  return -1;
91  }
92 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
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 AddTransitionProbs(const TransitionModel &trans_model, const std::vector< int32 > &disambig_syms, BaseFloat transition_scale, BaseFloat self_loop_scale, fst::VectorFst< fst::StdArc > *fst)
Adds transition-probs, with the supplied scales (see Scaling of transition and acoustic probabilities...
Definition: hmm-utils.cc:1088
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
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153