lattice-mbr-decode.cc File Reference
#include "util/common-utils.h"
#include "lat/sausages.h"
#include "hmm/posterior.h"
Include dependency graph for lattice-mbr-decode.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 24 of file lattice-mbr-decode.cc.

References SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), MinimumBayesRisk::GetBayesRisk(), MinimumBayesRisk::GetOneBest(), MinimumBayesRisk::GetOneBestTimes(), ParseOptions::GetOptArg(), MinimumBayesRisk::GetSausageStats(), MinimumBayesRisk::GetSausageTimes(), KALDI_ERR, KALDI_LOG, 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().

24  {
25  try {
26  using namespace kaldi;
27  typedef kaldi::int32 int32;
28 
29  const char *usage =
30  "Do Minimum Bayes Risk decoding (decoding that aims to minimize the \n"
31  "expected word error rate). Possible outputs include the 1-best path\n"
32  "(i.e. the word-sequence, as a sequence of ints per utterance), the\n"
33  "computed Bayes Risk for each utterance, and the sausage stats as\n"
34  "(for each utterance) std::vector<std::vector<std::pair<int32, float> > >\n"
35  "for which we use the same I/O routines as for posteriors (type Posterior).\n"
36  "times-wspecifier writes pairs of (start-time, end-time) in frames, for\n"
37  "each sausage position, or for each one-best entry if --one-best-times=true.\n"
38  "Note: use ark:/dev/null or the empty string for unwanted outputs.\n"
39  "Note: times will only be very meaningful if you first use lattice-word-align.\n"
40  "If you need ctm-format output, don't use this program but use lattice-to-ctm-conf\n"
41  "with --decode-mbr=true.\n"
42  "\n"
43  "Usage: lattice-mbr-decode [options] lattice-rspecifier "
44  "transcriptions-wspecifier [ bayes-risk-wspecifier "
45  "[ sausage-stats-wspecifier [ times-wspecifier] ] ] \n"
46  " e.g.: lattice-mbr-decode --acoustic-scale=0.1 ark:1.lats "
47  "'ark,t:|int2sym.pl -f 2- words.txt > text' ark:/dev/null ark:1.sau\n";
48 
49  ParseOptions po(usage);
50  BaseFloat acoustic_scale = 1.0;
51  BaseFloat lm_scale = 1.0;
52  bool one_best_times = false;
53 
54  std::string word_syms_filename;
55  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for "
56  "acoustic likelihoods");
57  po.Register("lm-scale", &lm_scale, "Scaling factor for language model "
58  "probabilities");
59  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for "
60  "words [for debug output]");
61  po.Register("one-best-times", &one_best_times, "If true, output times "
62  "corresponding to one-best, not whole sausage.");
63 
64  po.Read(argc, argv);
65 
66  if (po.NumArgs() < 2 || po.NumArgs() > 5) {
67  po.PrintUsage();
68  exit(1);
69  }
70 
71  std::string lats_rspecifier = po.GetArg(1),
72  trans_wspecifier = po.GetArg(2),
73  bayes_risk_wspecifier = po.GetOptArg(3),
74  sausage_stats_wspecifier = po.GetOptArg(4),
75  times_wspecifier = po.GetOptArg(5);
76 
77  // Read as compact lattice.
78  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
79 
80  Int32VectorWriter trans_writer(trans_wspecifier);
81  BaseFloatWriter bayes_risk_writer(bayes_risk_wspecifier);
82  // Note: type Posterior = vector<vector<pair<int32,BaseFloat> > >
83  // happens to be the same as needed for the sausage stats.
84  PosteriorWriter sausage_stats_writer(sausage_stats_wspecifier);
85 
86  BaseFloatPairVectorWriter times_writer(times_wspecifier);
87 
88  fst::SymbolTable *word_syms = NULL;
89  if (word_syms_filename != "")
90  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
91  KALDI_ERR << "Could not read symbol table from file "
92  << word_syms_filename;
93 
94  int32 n_done = 0, n_words = 0;
95  BaseFloat tot_bayes_risk = 0.0;
96 
97  for (; !clat_reader.Done(); clat_reader.Next()) {
98  std::string key = clat_reader.Key();
99  CompactLattice clat = clat_reader.Value();
100  clat_reader.FreeCurrent();
101  fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &clat);
102 
103  MinimumBayesRisk mbr(clat);
104 
105  if (trans_wspecifier != "")
106  trans_writer.Write(key, mbr.GetOneBest());
107  if (bayes_risk_wspecifier != "")
108  bayes_risk_writer.Write(key, mbr.GetBayesRisk());
109  if (sausage_stats_wspecifier != "")
110  sausage_stats_writer.Write(key, mbr.GetSausageStats());
111  if (times_wspecifier != "")
112  times_writer.Write(key, one_best_times ? mbr.GetOneBestTimes() :
113  mbr.GetSausageTimes());
114 
115  n_done++;
116  n_words += mbr.GetOneBest().size();
117  tot_bayes_risk += mbr.GetBayesRisk();
118  }
119 
120  KALDI_LOG << "Done " << n_done << " lattices.";
121  KALDI_LOG << "Average Bayes Risk per sentence is "
122  << (tot_bayes_risk / n_done) << " and per word, "
123  << (tot_bayes_risk / n_words);
124 
125  delete word_syms;
126  return (n_done != 0 ? 0 : 1);
127  } catch(const std::exception &e) {
128  std::cerr << e.what();
129  return -1;
130  }
131 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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 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
This class does the word-level Minimum Bayes Risk computation, and gives you either the 1-best MBR ou...
Definition: sausages.h:77
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
#define KALDI_LOG
Definition: kaldi-error.h:153