30 using namespace kaldi;
32 typedef kaldi::int64 int64;
33 using fst::SymbolTable;
39 "Add lm_scale * [cost of best path through LM FST] to graph-cost of\n" 40 "paths through lattice. Does this by composing with LM FST, then\n" 41 "lattice-determinizing (it has to negate weights first if lm_scale<0)\n" 42 "Usage: lattice-lmrescore [options] <lattice-rspecifier> <lm-fst-in> <lattice-wspecifier>\n" 43 " e.g.: lattice-lmrescore --lm-scale=-1.0 ark:in.lats 'fstproject --project_output=true data/lang/G.fst|' ark:out.lats\n";
47 int32 num_states_cache = 50000;
49 po.Register(
"lm-scale", &lm_scale,
"Scaling factor for language model costs; frequently 1.0 or -1.0");
50 po.Register(
"num-states-cache", &num_states_cache,
51 "Number of states we cache when mapping LM FST to lattice type. " 52 "More -> more memory but faster.");
56 if (po.NumArgs() != 3) {
61 std::string lats_rspecifier = po.GetArg(1),
62 fst_rxfilename = po.GetArg(2),
63 lats_wspecifier = po.GetArg(3);
67 VectorFst<StdArc> *std_lm_fst =
ReadFstKaldi(fst_rxfilename);
68 if (std_lm_fst->Properties(fst::kILabelSorted,
true) == 0) {
70 fst::ILabelCompare<StdArc> ilabel_comp;
71 fst::ArcSort(std_lm_fst, ilabel_comp);
77 fst::CacheOptions cache_opts(
true, num_states_cache);
78 fst::MapFstOptions mapfst_opts(cache_opts);
80 fst::MapFst<StdArc, LatticeArc, fst::StdToLatticeMapper<BaseFloat> >
81 lm_fst(*std_lm_fst, mapper, mapfst_opts);
90 true, fst::SEQUENCE_FILTER,
105 int32 n_done = 0, n_fail = 0;
107 for (; !lattice_reader.Done(); lattice_reader.Next()) {
108 std::string key = lattice_reader.Key();
109 Lattice lat = lattice_reader.Value();
110 lattice_reader.FreeCurrent();
111 if (lm_scale != 0.0) {
119 ArcSort(&lat, fst::OLabelCompare<LatticeArc>());
126 TableCompose(lat, lm_fst, &composed_lat, &lm_compose_cache);
128 Invert(&composed_lat);
132 if (determinized_lat.Start() == fst::kNoStateId) {
133 KALDI_WARN <<
"Empty lattice for utterance " << key <<
" (incompatible LM?)";
136 compact_lattice_writer.Write(key, determinized_lat);
144 compact_lattice_writer.Write(key, compact_lat);
148 KALDI_LOG <<
"Done " << n_done <<
" lattices, failed for " << n_fail;
149 return (n_done != 0 ? 0 : 1);
150 }
catch(
const std::exception &e) {
151 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void TableCompose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const TableComposeOptions &opts=TableComposeOptions())
A templated class for writing objects to an archive or script file; see The Table concept...
TableComposeCache lets us do multiple compositions while caching the same matcher.
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
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...
fst::VectorFst< LatticeArc > Lattice
TableMatcher is a matcher specialized for the case where the output side of the left FST always has e...
fst::VectorFst< CompactLatticeArc > CompactLattice
std::vector< std::vector< double > > GraphLatticeScale(double lmwt)
Class StdToLatticeMapper maps a normal arc (StdArc) to a LatticeArc by putting the StdArc weight as t...
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
bool DeterminizeLattice(const Fst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< Weight > > *ofst, DeterminizeLatticeOptions opts, bool *debug_ptr)
This function implements the normal version of DeterminizeLattice, in which the output strings are re...