lattice-1best.cc File Reference
Include dependency graph for lattice-1best.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 29 of file lattice-1best.cc.

References kaldi::AddWordInsPenToCompactLattice(), kaldi::CompactLatticeShortestPath(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), fst::LatticeScale(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), fst::ScaleLattice(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

29  {
30  try {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  typedef kaldi::int64 int64;
34  using fst::VectorFst;
35  using fst::StdArc;
36 
37  const char *usage =
38  "Compute best path through lattices and write out as FSTs\n"
39  "Note: differs from lattice-nbest with --n=1 because we won't\n"
40  "append -1 to the utterance-ids. Differs from lattice-best-path\n"
41  "because output is FST.\n"
42  "\n"
43  "Usage: lattice-1best [options] <lattice-rspecifier> <lattice-wspecifier>\n"
44  " e.g.: lattice-1best --acoustic-scale=0.1 ark:1.lats ark:1best.lats\n";
45 
46  ParseOptions po(usage);
47  BaseFloat acoustic_scale = 1.0;
48  BaseFloat lm_scale = 1.0;
49  BaseFloat word_ins_penalty = 0.0;
50 
51  po.Register("acoustic-scale", &acoustic_scale,
52  "Scaling factor for acoustic likelihoods");
53  po.Register("lm-scale", &lm_scale,
54  "Scaling factor for language model scores.");
55  po.Register("word-ins-penalty", &word_ins_penalty,
56  "Word insertion penality.");
57 
58  po.Read(argc, argv);
59 
60  if (po.NumArgs() != 2) {
61  po.PrintUsage();
62  exit(1);
63  }
64 
65  std::string lats_rspecifier = po.GetArg(1),
66  lats_wspecifier = po.GetArg(2);
67 
68  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
69 
70  // Write as compact lattice.
71  CompactLatticeWriter compact_1best_writer(lats_wspecifier);
72 
73  int32 n_done = 0, n_err = 0;
74 
75  if (acoustic_scale == 0.0 || lm_scale == 0.0)
76  KALDI_ERR << "Do not use exactly zero acoustic or LM scale (cannot be inverted)";
77  for (; !clat_reader.Done(); clat_reader.Next()) {
78  std::string key = clat_reader.Key();
79  CompactLattice clat = clat_reader.Value();
80  clat_reader.FreeCurrent();
81  fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &clat);
82  if (word_ins_penalty > 0.0) {
83  AddWordInsPenToCompactLattice(word_ins_penalty, &clat);
84  }
85 
86  CompactLattice best_path;
87  CompactLatticeShortestPath(clat, &best_path);
88 
89  if (best_path.Start() == fst::kNoStateId) {
90  KALDI_WARN << "Possibly empty lattice for utterance-id " << key
91  << "(no output)";
92  n_err++;
93  } else {
94  if (word_ins_penalty > 0.0) {
95  AddWordInsPenToCompactLattice(-word_ins_penalty, &best_path);
96  }
97  fst::ScaleLattice(fst::LatticeScale(1.0 / lm_scale, 1.0/acoustic_scale),
98  &best_path);
99  compact_1best_writer.Write(key, best_path);
100  n_done++;
101  }
102  }
103  KALDI_LOG << "Done converting " << n_done << " to best path, "
104  << n_err << " had errors.";
105  return (n_done != 0 ? 0 : 1);
106  } catch(const std::exception &e) {
107  std::cerr << e.what();
108  return -1;
109  }
110 }
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
void CompactLatticeShortestPath(const CompactLattice &clat, CompactLattice *shortest_path)
A form of the shortest-path/best-path algorithm that&#39;s specially coded for CompactLattice.
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
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
void AddWordInsPenToCompactLattice(BaseFloat word_ins_penalty, CompactLattice *clat)
This function add the word insertion penalty to graph score of each word in the compact lattice...
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
#define KALDI_LOG
Definition: kaldi-error.h:153