lattice-prune.cc
Go to the documentation of this file.
1 // latbin/lattice-prune.cc
2 
3 // Copyright 2009-2013 Microsoft Corporation
4 // 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  using fst::SymbolTable;
34  using fst::VectorFst;
35  using fst::StdArc;
36 
37  const char *usage =
38  "Apply beam pruning to lattices\n"
39  "Usage: lattice-prune [options] lattice-rspecifier lattice-wspecifier\n"
40  " e.g.: lattice-prune --acoustic-scale=0.1 --beam=4.0 ark:1.lats ark:pruned.lats\n";
41 
42  ParseOptions po(usage);
43  BaseFloat acoustic_scale = 1.0;
44  BaseFloat inv_acoustic_scale = 1.0;
45  BaseFloat beam = 10.0;
46 
47  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
48  po.Register("inv-acoustic-scale", &inv_acoustic_scale, "An alternative way of setting the "
49  "acoustic scale: you can set its inverse.");
50  po.Register("beam", &beam, "Pruning beam [applied after acoustic scaling]");
51 
52  po.Read(argc, argv);
53 
54  if (po.NumArgs() != 2) {
55  po.PrintUsage();
56  exit(1);
57  }
58 
59  KALDI_ASSERT(acoustic_scale == 1.0 || inv_acoustic_scale == 1.0);
60  if (inv_acoustic_scale != 1.0)
61  acoustic_scale = 1.0 / inv_acoustic_scale;
62 
63  std::string lats_rspecifier = po.GetArg(1),
64  lats_wspecifier = po.GetArg(2);
65 
66 
67 
68  SequentialCompactLatticeReader compact_lattice_reader(lats_rspecifier);
69  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
70 
71  int32 n_done = 0, n_err = 0;
72  int64 n_arcs_in = 0, n_arcs_out = 0,
73  n_states_in = 0, n_states_out = 0;
74 
75  if (acoustic_scale == 0.0)
76  KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
77 
78  for (; !compact_lattice_reader.Done(); compact_lattice_reader.Next()) {
79  std::string key = compact_lattice_reader.Key();
80  CompactLattice clat = compact_lattice_reader.Value();
81  compact_lattice_reader.FreeCurrent();
82  fst::ScaleLattice(fst::AcousticLatticeScale(acoustic_scale), &clat);
83  int64 narcs = NumArcs(clat), nstates = clat.NumStates();
84  n_arcs_in += narcs;
85  n_states_in += nstates;
86  CompactLattice pruned_clat(clat);
87  if (!PruneLattice(beam, &pruned_clat)) {
88  KALDI_WARN << "Error pruning lattice for utterance " << key;
89  n_err++;
90  }
91  int64 pruned_narcs = NumArcs(pruned_clat),
92  pruned_nstates = pruned_clat.NumStates();
93  n_arcs_out += pruned_narcs;
94  n_states_out += pruned_nstates;
95  KALDI_LOG << "For utterance " << key << ", pruned #states from "
96  << nstates << " to " << pruned_nstates << " and #arcs from "
97  << narcs << " to " << pruned_narcs;
98  fst::ScaleLattice(fst::AcousticLatticeScale(1.0/acoustic_scale), &pruned_clat);
99  compact_lattice_writer.Write(key, pruned_clat);
100  n_done++;
101  }
102 
103  BaseFloat den = (n_done > 0 ? static_cast<BaseFloat>(n_done) : 1.0);
104  KALDI_LOG << "Overall, pruned from on average " << (n_states_in/den) << " to "
105  << (n_states_out/den) << " states, and from " << (n_arcs_in/den)
106  << " to " << (n_arcs_out/den) << " arcs, over " << n_done
107  << " utterances.";
108  KALDI_LOG << "Done " << n_done << " lattices.";
109  return (n_done != 0 ? 0 : 1);
110  } catch(const std::exception &e) {
111  std::cerr << e.what();
112  return -1;
113  }
114 }
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)
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
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...
int main(int argc, char *argv[])
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.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
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).
bool PruneLattice(BaseFloat beam, LatType *lat)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Arc::StateId NumArcs(const ExpandedFst< Arc > &fst)
Returns the total number of arcs in an FST.
#define KALDI_LOG
Definition: kaldi-error.h:153