lattice-to-nbest.cc File Reference
Include dependency graph for lattice-to-nbest.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 26 of file lattice-to-nbest.cc.

References fst::ConvertLattice(), fst::ConvertNbestToVector(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), fst::LatticeScale(), rnnlm::n, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), fst::ScaleLattice(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29  typedef kaldi::int32 int32;
30  typedef kaldi::int64 int64;
31  using fst::SymbolTable;
32  using fst::VectorFst;
33  using fst::StdArc;
34 
35  const char *usage =
36  "Work out N-best paths in lattices and write out as FSTs\n"
37  "Note: only guarantees distinct word sequences if distinct paths in\n"
38  "input lattices had distinct word-sequences (this will not be true if\n"
39  "you produced lattices with --determinize-lattice=false, i.e. state-level\n"
40  "lattices).\n"
41  "Usage: lattice-to-nbest [options] <lattice-rspecifier> <lattice-wspecifier>\n"
42  " e.g.: lattice-to-nbest --acoustic-scale=0.1 --n=10 ark:1.lats ark:nbest.lats\n";
43 
44  ParseOptions po(usage);
45  BaseFloat acoustic_scale = 1.0, lm_scale = 1.0;
46  bool random = false;
47  int32 srand_seed = 0;
48  int32 n = 1;
49 
50  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
51  po.Register("lm-scale", &lm_scale, "Scaling factor for language model scores.");
52  po.Register("n", &n, "Number of distinct paths");
53  po.Register("random", &random,
54  "If true, generate n random paths instead of n-best paths"
55  "In this case, all costs in generated paths will be zero.");
56  po.Register("srand", &srand_seed, "Seed for random number generator "
57  "(only relevant if --random=true)");
58 
59 
60  po.Read(argc, argv);
61 
62  KALDI_ASSERT(n > 0);
63  srand(srand_seed);
64 
65  if (po.NumArgs() != 2) {
66  po.PrintUsage();
67  exit(1);
68  }
69 
70  std::string lats_rspecifier = po.GetArg(1),
71  lats_wspecifier = po.GetArg(2);
72 
73 
74  // Read as regular lattice.
75  SequentialLatticeReader lattice_reader(lats_rspecifier);
76 
77  // Write as compact lattice.
78  CompactLatticeWriter compact_nbest_writer(lats_wspecifier);
79 
80  int32 n_done = 0;
81  int64 n_paths_out = 0;
82 
83  if (acoustic_scale == 0.0 || lm_scale == 0.0)
84  KALDI_ERR << "Do not use a zero acoustic or LM scale (cannot be inverted)";
85  for (; !lattice_reader.Done(); lattice_reader.Next()) {
86  std::string key = lattice_reader.Key();
87  Lattice lat = lattice_reader.Value();
88  lattice_reader.FreeCurrent();
89  fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &lat);
90 
91  std::vector<Lattice> nbest_lats;
92  {
93  Lattice nbest_lat;
94  if (!random) {
95  fst::ShortestPath(lat, &nbest_lat, n);
96  } else {
97  fst::UniformArcSelector<LatticeArc> uniform_selector;
98  fst::RandGenOptions<fst::UniformArcSelector<LatticeArc> > opts(uniform_selector);
99  opts.npath = n;
100  fst::RandGen(lat, &nbest_lat, opts);
101  }
102  fst::ConvertNbestToVector(nbest_lat, &nbest_lats);
103  }
104 
105  if (nbest_lats.empty()) {
106  KALDI_WARN << "Possibly empty lattice for utterance-id " << key
107  << "(no N-best entries)";
108  } else {
109  for (int32 k = 0; k < static_cast<int32>(nbest_lats.size()); k++) {
110  std::ostringstream s;
111  s << key << "-" << (k+1); // so if key is "utt_id", the keys
112  // of the n-best are utt_id-1, utt_id-2, utt_id-3, etc.
113  std::string nbest_key = s.str();
114  fst::ScaleLattice(fst::LatticeScale(1.0/lm_scale, 1.0/acoustic_scale),
115  &(nbest_lats[k]));
116  CompactLattice nbest_clat;
117  ConvertLattice(nbest_lats[k], &nbest_clat); // write in compact form.
118  compact_nbest_writer.Write(nbest_key, nbest_clat);
119  }
120  n_done++;
121  n_paths_out += nbest_lats.size();
122  }
123  }
124 
125  KALDI_LOG << "Done applying N-best algorithm to " << n_done << " lattices with n = "
126  << n << ", average actual #paths is "
127  << (n_paths_out/(n_done+1.0e-20));
128  return (n_done != 0 ? 0 : 1);
129  } catch(const std::exception &e) {
130  std::cerr << e.what();
131  return -1;
132  }
133 }
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
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 ConvertNbestToVector(const Fst< Arc > &fst, std::vector< VectorFst< Arc > > *fsts_out)
This function converts an FST with a special structure, which is output by the OpenFst functions Shor...
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...
struct rnnlm::@11::@12 n
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
#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
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define KALDI_LOG
Definition: kaldi-error.h:153