lattice-limit-depth.cc File Reference
Include dependency graph for lattice-limit-depth.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 28 of file lattice-limit-depth.cc.

References fst::AcousticLatticeScale(), kaldi::CompactLatticeDepth(), kaldi::CompactLatticeLimitDepth(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ASSERT, KALDI_LOG, KALDI_VLOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), fst::ScaleLattice(), kaldi::TopSortCompactLatticeIfNeeded(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

28  {
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
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
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
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
#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