lattice-limit-depth.cc
Go to the documentation of this file.
1 // latbin/lattice-limit-depth.cc
2 
3 // Copyright 2013 Ehsan Variani
4 // 2013 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 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "fstext/fstext-lib.h"
25 #include "lat/kaldi-lattice.h"
26 #include "lat/lattice-functions.h"
27 
28 int main(int argc, char *argv[]) {
29  try {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32  typedef kaldi::int64 int64;
33 
34  using fst::VectorFst;
35  using fst::StdArc;
36  typedef StdArc::StateId StateId;
37 
38  const char *usage =
39  "Limit the number of arcs crossing any frame, to a specified maximum.\n"
40  "Requires an acoustic scale, because forward-backward Viterbi probs are\n"
41  "needed, which will be affected by this.\n"
42  "\n"
43  "Usage: lattice-limit-depth [options] <lattice-rspecifier> <lattice-wspecifier>\n"
44  "E.g.: lattice-limit-depth --max-arcs-per-frame=1000 --acoustic-scale=0.1 ark:- ark:-\n";
45 
46  ParseOptions po(usage);
47 
48  int32 max_arcs_per_frame = 1000;
49  BaseFloat acoustic_scale = 1.0;
50 
51  po.Register("max-arcs-per-frame", &max_arcs_per_frame,
52  "Maximum number of arcs that are allowed to cross any given "
53  "frame");
54  po.Register("acoustic-scale", &acoustic_scale,
55  "Scaling factor for acoustic likelihoods");
56 
57  po.Read(argc, argv);
58 
59  if (po.NumArgs() != 2) {
60  po.PrintUsage();
61  exit(1);
62  }
63 
64  KALDI_ASSERT(acoustic_scale != 0.0);
65 
66  std::string lats_rspecifier = po.GetArg(1),
67  lats_wspecifier = po.GetArg(2);
68  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
69  CompactLatticeWriter clat_writer(lats_wspecifier);
70 
71  int64 num_done = 0;
72  double sum_depth_in = 0.0, sum_depth_out = 0.0, total_t = 0.0;
73  for (; !clat_reader.Done(); clat_reader.Next()) {
74  CompactLattice clat = clat_reader.Value();
75  std::string key = clat_reader.Key();
76 
77  fst::ScaleLattice(fst::AcousticLatticeScale(acoustic_scale), &clat);
78 
80 
81  int32 t;
82  BaseFloat depth_in = CompactLatticeDepth(clat, &t);
83 
84  CompactLatticeLimitDepth(max_arcs_per_frame, &clat);
85 
86  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale),
87  &clat);
88 
89  BaseFloat depth_out = CompactLatticeDepth(clat);
90 
91  KALDI_VLOG(2) << "For key " << key << ", depth changed from "
92  << depth_in << " to " << depth_out << " over "
93  << t << " frames.";
94 
95  total_t += t;
96  sum_depth_in += t * depth_in;
97  sum_depth_out += t * depth_out;
98 
99  clat_writer.Write(key, clat);
100 
101  num_done++;
102  }
103  KALDI_LOG << "Done " << num_done << " lattices.";
104  KALDI_LOG << "Overall density changed from " << (sum_depth_in / total_t)
105  << " to " << (sum_depth_out / total_t);
106  if (num_done != 0) return 0;
107  else return 1;
108  } catch (const std::exception &e) {
109  std::cerr << e.what();
110  return -1;
111  }
112 }
fst::StdArc::StateId StateId
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Lattice::StateId StateId
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)
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
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
BaseFloat CompactLatticeDepth(const CompactLattice &clat, int32 *num_frames)
Returns the depth of the lattice, defined as the average number of arcs crossing any given frame...
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
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 main(int argc, char *argv[])
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void CompactLatticeLimitDepth(int32 max_depth_per_frame, CompactLattice *clat)
This function limits the depth of the lattice, per frame: that means, it does not allow more than a s...
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
#define KALDI_LOG
Definition: kaldi-error.h:153