lattice-to-fst.cc File Reference
Include dependency graph for lattice-to-fst.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 26 of file lattice-to-fst.cc.

References fst::ConvertLattice(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), KALDI_LOG, SequentialTableReader< Holder >::Key(), fst::LatticeScale(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), fst::RemoveAlignmentsFromCompactLattice(), fst::RemoveEpsLocal(), fst::ScaleLattice(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29  typedef kaldi::int32 int32;
30  typedef kaldi::int64 int64;
31  using fst::SymbolTable;
32  using fst::VectorFst;
33  using fst::StdArc;
34  using std::vector;
35  BaseFloat acoustic_scale = 0.0;
36  BaseFloat lm_scale = 0.0;
37  bool rm_eps = true;
38 
39  const char *usage =
40  "Turn lattices into normal FSTs, retaining only the word labels\n"
41  "By default, removes all weights and also epsilons (configure with\n"
42  "with --acoustic-scale, --lm-scale and --rm-eps)\n"
43  "Usage: lattice-to-fst [options] lattice-rspecifier fsts-wspecifier\n"
44  " e.g.: lattice-to-fst ark:1.lats ark:1.fsts\n";
45 
46  ParseOptions po(usage);
47  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
48  po.Register("lm-scale", &lm_scale, "Scaling factor for graph/lm costs");
49  po.Register("rm-eps", &rm_eps, "Remove epsilons in resulting FSTs (in lazy way; may not remove all)");
50 
51  po.Read(argc, argv);
52 
53  if (po.NumArgs() != 2) {
54  po.PrintUsage();
55  exit(1);
56  }
57 
58  vector<vector<double> > scale = fst::LatticeScale(lm_scale, acoustic_scale);
59 
60  std::string lats_rspecifier = po.GetArg(1),
61  fsts_wspecifier = po.GetArg(2);
62 
63  SequentialCompactLatticeReader lattice_reader(lats_rspecifier);
64  TableWriter<fst::VectorFstHolder> fst_writer(fsts_wspecifier);
65 
66  int32 n_done = 0; // there is no failure mode, barring a crash.
67 
68  for (; !lattice_reader.Done(); lattice_reader.Next()) {
69  std::string key = lattice_reader.Key();
70  CompactLattice clat = lattice_reader.Value();
71  lattice_reader.FreeCurrent();
72  ScaleLattice(scale, &clat); // typically scales to zero.
73  RemoveAlignmentsFromCompactLattice(&clat); // remove the alignments...
74  fst::VectorFst<StdArc> fst;
75  {
76  Lattice lat;
77  ConvertLattice(clat, &lat); // convert to non-compact form.. won't introduce
78  // extra states because already removed alignments.
79  ConvertLattice(lat, &fst); // this adds up the (lm,acoustic) costs to get
80  // the normal (tropical) costs.
81  Project(&fst, fst::PROJECT_OUTPUT); // Because in the standard Lattice format,
82  // the words are on the output, and we want the word labels.
83  }
84  if (rm_eps) RemoveEpsLocal(&fst);
85 
86  fst_writer.Write(key, fst);
87  n_done++;
88  }
89  KALDI_LOG << "Done converting " << n_done << " lattices to word-level FSTs";
90  return (n_done != 0 ? 0 : 1);
91  } catch(const std::exception &e) {
92  std::cerr << e.what();
93  return -1;
94  }
95 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void RemoveAlignmentsFromCompactLattice(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *fst)
Removes state-level alignments (the strings that are part of the weights).
void RemoveEpsLocal(MutableFst< Arc > *fst)
RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is g...
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
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
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...
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...
Definition: kaldi-table.h:287
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
#define KALDI_LOG
Definition: kaldi-error.h:153