29     using namespace kaldi;
    33         "Determinize lattices, keeping only the best path (sequence of acoustic states)\n"    34         "for each input-symbol sequence.  This version does pruning as part of the\n"    35         "determinization algorithm, which is more efficient and prevents blowup.\n"    36         "See http://kaldi-asr.org/doc/lattices.html for more information on lattices.\n"    38         "Usage: lattice-determinize-pruned [options] lattice-rspecifier lattice-wspecifier\n"    39         " e.g.: lattice-determinize-pruned --acoustic-scale=0.1 --beam=6.0 ark:in.lats ark:det.lats\n";
    42     bool write_compact = 
true;
    45     bool minimize = 
false;
    52     po.Register(
"write-compact", &write_compact, 
    53                 "If true, write in normal (compact) form. "    54                 "--write-compact=false allows you to retain frame-level "    55                 "acoustic score information, but this requires the input "    56                 "to be in non-compact form e.g. undeterminized lattice "    57                 "straight from decoding.");
    58     po.Register(
"acoustic-scale", &acoustic_scale,
    59                 "Scaling factor for acoustic likelihoods");
    60     po.Register(
"beam", &beam, 
"Pruning beam [applied after acoustic scaling].");
    61     po.Register(
"minimize", &minimize,
    62                 "If true, push and minimize after determinization");
    66     if (po.NumArgs() != 2) {
    71     std::string lats_rspecifier = po.GetArg(1),
    72         lats_wspecifier = po.GetArg(2);
    83       compact_lat_writer.
Open(lats_wspecifier);
    85       lat_writer.
Open(lats_wspecifier);
    87     int32 n_done = 0, n_warn = 0;
    90     double sum_depth_in = 0.0,
    91           sum_depth_out = 0.0, sum_t = 0.0;
    93     if (acoustic_scale == 0.0)
    94       KALDI_ERR << 
"Do not use a zero acoustic scale (cannot be inverted)";
    96     for (; !lat_reader.Done(); lat_reader.Next()) {
    97       std::string key = lat_reader.Key();
    98       Lattice lat = lat_reader.Value();
   100       KALDI_VLOG(2) << 
"Processing lattice " << key;
   103       unordered_map<std::pair<int32,int32>, std::pair<BaseFloat, int32>,
   109       lat_reader.FreeCurrent();
   111       if (!TopSort(&lat)) {
   112         KALDI_WARN << 
"Could not topologically sort lattice: this probably means it"   113             " has bad properties e.g. epsilon cycles.  Your LM or lexicon might "   114             "be broken, e.g. LM with epsilon cycles or lexicon with empty words.";
   116       fst::ArcSort(&lat, fst::ILabelCompare<LatticeArc>());
   119         KALDI_WARN << 
"For key " << key << 
", determinization did not succeed"   120             "(partial output will be pruned tighter than the specified beam.)";
   123       fst::Connect(&det_clat);
   124       if (det_clat.NumStates() == 0) {
   125         KALDI_WARN << 
"For key " << key << 
", determinized and trimmed lattice "   138       sum_depth_in += lat.NumStates();
   139       sum_depth_out += depth * t;
   144         compact_lat_writer.
Write(key, det_clat);
   152         lat_writer.
Write(key, out_lat);
   159       KALDI_LOG << 
"Average input-lattice depth (measured at at state level) is "   160                 << (sum_depth_in / sum_t) << 
", output depth is "   161                 << (sum_depth_out / sum_t) << 
", over " << sum_t << 
" frames "   162                 << 
" (average num-frames = " << (sum_t / n_done) << 
").";
   164     KALDI_LOG << 
"Done " << n_done << 
" lattices, determinization finished "   165               << 
"earlier than specified by the beam (or output was empty) on "   166               << n_warn << 
" of these.";
   167     return (n_done != 0 ? 0 : 1);
   168   } 
catch(
const std::exception &e) {
   169     std::cerr << e.what();
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
 
bool Open(const std::string &wspecifier)
 
bool DeterminizeLatticePruned(const ExpandedFst< ArcTpl< Weight > > &ifst, double beam, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *ofst, DeterminizeLatticePrunedOptions opts)
 
void ReplaceAcousticScoresFromMap(const unordered_map< std::pair< int32, int32 >, std::pair< BaseFloat, int32 >, PairHasher< int32 > > &acoustic_scores, Lattice *lat)
This function restores acoustic scores computed using the function ComputeAcousticScoresMap into the ...
 
bool PushCompactLatticeStrings(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
This function pushes the transition-ids as far towards the start as they will go. ...
 
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
 
bool PushCompactLatticeWeights(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
This function pushes the weights in the CompactLattice so that all states except possibly the start s...
 
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
 
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
 
bool MinimizeCompactLattice(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat, float delta)
This function minimizes the compact lattice. 
 
BaseFloat CompactLatticeDepth(const CompactLattice &clat, int32 *num_frames)
Returns the depth of the lattice, defined as the average number of arcs crossing any given frame...
 
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 Register(kaldi::OptionsItf *opts)
 
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...
 
fst::VectorFst< LatticeArc > Lattice
 
fst::VectorFst< CompactLatticeArc > CompactLattice
 
void ComputeAcousticScoresMap(const Lattice &lat, unordered_map< std::pair< int32, int32 >, std::pair< BaseFloat, int32 >, PairHasher< int32 > > *acoustic_scores)
This function computes the mapping from the pair (frame-index, transition-id) to the pair (sum-of-aco...
 
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted. 
 
A hashing function-object for pairs of ints.