lattice-depth.cc
Go to the documentation of this file.
1 // latbin/lattice-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  "Compute the lattice depths in terms of the average number of arcs that\n"
40  "cross a frame. See also lattice-depth-per-frame\n"
41  "Usage: lattice-depth <lattice-rspecifier> [<depth-wspecifier>]\n"
42  "E.g.: lattice-depth ark:- ark,t:-\n";
43 
44  ParseOptions po(usage);
45 
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() < 1 || po.NumArgs() > 2) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  std::string lats_rspecifier = po.GetArg(1);
54  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
55 
56  std::string depth_wspecifier = po.GetOptArg(2);
57  BaseFloatWriter lats_depth_writer(depth_wspecifier);
58 
59  int64 num_done = 0;
60  double sum_depth = 0.0, total_t = 0.0;
61  for (; !clat_reader.Done(); clat_reader.Next()) {
62  CompactLattice clat = clat_reader.Value();
63  std::string key = clat_reader.Key();
64 
66 
67  int32 t;
68  BaseFloat depth = CompactLatticeDepth(clat, &t);
69 
70  if (depth_wspecifier != "")
71  lats_depth_writer.Write(key, depth);
72 
73  sum_depth += depth * t;
74  total_t += t;
75  num_done++;
76  }
77  KALDI_LOG << "Done " << num_done << " lattices.";
78  // Warning: the script egs/s5/*/steps/oracle_wer.sh parses the next line.
79  KALDI_LOG << "Overall density is " << (sum_depth / total_t) << " over "
80  << total_t << " frames.";
81  if (num_done != 0) return 0;
82  else return 1;
83  } catch (const std::exception &e) {
84  std::cerr << e.what();
85  return -1;
86  }
87 }
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
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...
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).
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
#define KALDI_LOG
Definition: kaldi-error.h:153
std::string GetOptArg(int param) const