28 using namespace kaldi;
30 typedef kaldi::int64 int64;
31 using fst::SymbolTable;
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" 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";
45 BaseFloat acoustic_scale = 1.0, lm_scale = 1.0;
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)");
65 if (po.NumArgs() != 2) {
70 std::string lats_rspecifier = po.GetArg(1),
71 lats_wspecifier = po.GetArg(2);
81 int64 n_paths_out = 0;
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();
91 std::vector<Lattice> nbest_lats;
95 fst::ShortestPath(lat, &nbest_lat, n);
97 fst::UniformArcSelector<LatticeArc> uniform_selector;
98 fst::RandGenOptions<fst::UniformArcSelector<LatticeArc> > opts(uniform_selector);
100 fst::RandGen(lat, &nbest_lat, opts);
105 if (nbest_lats.empty()) {
106 KALDI_WARN <<
"Possibly empty lattice for utterance-id " << key
107 <<
"(no N-best entries)";
109 for (int32 k = 0; k < static_cast<int32>(nbest_lats.size()); k++) {
110 std::ostringstream s;
111 s << key <<
"-" << (k+1);
113 std::string nbest_key = s.str();
118 compact_nbest_writer.Write(nbest_key, nbest_clat);
121 n_paths_out += nbest_lats.size();
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();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
A templated class for writing objects to an archive or script file; see The Table concept...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
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...
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...
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
fst::VectorFst< LatticeArc > Lattice
fst::VectorFst< CompactLatticeArc > CompactLattice
#define KALDI_ASSERT(cond)