lattice-depth-per-frame.cc
Go to the documentation of this file.
1 // latbin/lattice-depth-per-frame.cc
2 
3 // Copyright 2013 Ehsan Variani
4 // 2013,2016 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  "For each lattice, compute a vector of length (num-frames) saying how\n"
40  "may arcs cross each frame. See also lattice-depth\n"
41  "Usage: lattice-depth-per-frame <lattice-rspecifier> <depth-wspecifier> [<lattice-wspecifier>]\n"
42  "The final <lattice-wspecifier> allows you to write the input lattices out\n"
43  "in case you want to do something else with them as part of the same pipe.\n"
44  "E.g.: lattice-depth-per-frame ark:- ark,t:-\n";
45 
46  ParseOptions po(usage);
47 
48  po.Read(argc, argv);
49 
50  if (po.NumArgs() < 2 || po.NumArgs() > 3) {
51  po.PrintUsage();
52  exit(1);
53  }
54 
55  std::string lats_rspecifier = po.GetArg(1);
56  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
57 
58  std::string depth_wspecifier = po.GetArg(2);
59  Int32VectorWriter lats_depth_writer(depth_wspecifier);
60 
61  std::string lattice_wspecifier = po.GetOptArg(3);
62  CompactLatticeWriter clat_writer(lattice_wspecifier);
63 
64  int64 num_done = 0;
65 
66  for (; !clat_reader.Done(); clat_reader.Next()) {
67  CompactLattice clat = clat_reader.Value();
68  std::string key = clat_reader.Key();
69 
71 
72  std::vector<int32> depth_per_frame;
73  CompactLatticeDepthPerFrame(clat, &depth_per_frame);
74 
75  lats_depth_writer.Write(key, depth_per_frame);
76 
77  if (!lattice_wspecifier.empty())
78  clat_writer.Write(key, clat);
79 
80  num_done++;
81  }
82  KALDI_LOG << "Done " << num_done << " lattices.";
83  if (num_done != 0) return 0;
84  else return 1;
85  } catch (const std::exception &e) {
86  std::cerr << e.what();
87  return -1;
88  }
89 }
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
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
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 NumArgs() const
Number of positional parameters (c.f. argc-1).
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
void CompactLatticeDepthPerFrame(const CompactLattice &clat, std::vector< int32 > *depth_per_frame)
This function returns, for each frame, the number of arcs crossing that frame.
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153
std::string GetOptArg(int param) const