lattice-prune.cc File Reference
Include dependency graph for lattice-prune.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-prune.cc.

References fst::AcousticLatticeScale(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), fst::NumArcs(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), kaldi::PruneLattice(), ParseOptions::Read(), ParseOptions::Register(), fst::ScaleLattice(), 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  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
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
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
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
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