27 int main(
int argc,
char *argv[]) {
30 using fst::SymbolTable;
35 "Do forward-backward and collect posteriors over lattices.\n" 36 "Usage: lattice-to-post [options] lats-rspecifier posts-wspecifier [loglikes-wspecifier]\n" 37 " e.g.: lattice-to-post --acoustic-scale=0.1 ark:1.lats ark:1.post\n" 38 "See also: lattice-to-ctm-conf, post-to-pdf-post, lattice-arc-post\n";
42 po.
Register(
"acoustic-scale", &acoustic_scale,
43 "Scaling factor for acoustic likelihoods");
45 "Scaling factor for \"graph costs\" (including LM costs)");
53 if (acoustic_scale == 0.0)
54 KALDI_ERR <<
"Do not use a zero acoustic scale (cannot be inverted)";
56 std::string lats_rspecifier = po.
GetArg(1),
57 posteriors_wspecifier = po.
GetArg(2),
67 double total_like = 0.0, lat_like;
68 double total_ac_like = 0.0, lat_ac_like;
69 double total_time = 0, lat_time;
71 for (; !lattice_reader.
Done(); lattice_reader.
Next()) {
72 std::string key = lattice_reader.
Key();
77 if (acoustic_scale != 1.0 || lm_scale != 1.0)
80 kaldi::uint64 props = lat.Properties(fst::kFstProperties,
false);
81 if (!(props & fst::kTopSorted)) {
82 if (fst::TopSort(&lat) ==
false)
83 KALDI_ERR <<
"Cycles detected in lattice.";
88 total_like += lat_like;
89 lat_time = post.size();
90 total_time += lat_time;
91 total_ac_like += lat_ac_like;
93 KALDI_VLOG(2) <<
"Processed lattice for utterance: " << key <<
"; found " 94 << lat.NumStates() <<
" states and " <<
fst::NumArcs(lat)
95 <<
" arcs. Average log-likelihood = " << (lat_like/lat_time)
96 <<
" over " << lat_time <<
" frames. Average acoustic log-like" 97 <<
" per frame is " << (lat_ac_like/lat_time);
99 if (loglikes_writer.
IsOpen())
100 loglikes_writer.
Write(key, lat_like);
102 posterior_writer.
Write(key, post);
106 KALDI_LOG <<
"Overall average log-like/frame is " 107 << (total_like/total_time) <<
" over " << total_time
108 <<
" frames. Average acoustic like/frame is " 109 << (total_ac_like/total_time);
110 KALDI_LOG <<
"Done " << n_done <<
" lattices.";
111 return (n_done != 0 ? 0 : 1);
112 }
catch(
const std::exception &e) {
113 std::cerr << e.what();
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
BaseFloat LatticeForwardBackward(const Lattice &lat, Posterior *post, double *acoustic_like_sum)
This function does the forward-backward over lattices and computes the posterior probabilities of the...
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...
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
fst::VectorFst< LatticeArc > Lattice
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
Arc::StateId NumArcs(const ExpandedFst< Arc > &fst)
Returns the total number of arcs in an FST.
int main(int argc, char *argv[])
std::string GetOptArg(int param) const