lattice-scale.cc File Reference
Include dependency graph for lattice-scale.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 27 of file lattice-scale.cc.

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

27  {
28  try {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31  typedef kaldi::int64 int64;
32  using fst::SymbolTable;
33  using fst::VectorFst;
34  using fst::StdArc;
35 
36  const char *usage =
37  "Apply scaling to lattice weights\n"
38  "Usage: lattice-scale [options] lattice-rspecifier lattice-wspecifier\n"
39  " e.g.: lattice-scale --lm-scale=0.0 ark:1.lats ark:scaled.lats\n";
40 
41  ParseOptions po(usage);
42  bool write_compact = true;
43  BaseFloat acoustic_scale = 1.0;
44  BaseFloat inv_acoustic_scale = 1.0;
45  BaseFloat lm_scale = 1.0;
46  BaseFloat acoustic2lm_scale = 0.0;
47  BaseFloat lm2acoustic_scale = 0.0;
48 
49  po.Register("write-compact", &write_compact, "If true, write in normal (compact) form.");
50  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
51  po.Register("inv-acoustic-scale", &inv_acoustic_scale, "An alternative way "
52  "of setting the acoustic scale: you can set its inverse.");
53  po.Register("lm-scale", &lm_scale, "Scaling factor for graph/lm costs");
54  po.Register("acoustic2lm-scale", &acoustic2lm_scale, "Add this times original acoustic costs to LM costs");
55  po.Register("lm2acoustic-scale", &lm2acoustic_scale, "Add this times original LM costs to acoustic costs");
56 
57  po.Read(argc, argv);
58 
59  if (po.NumArgs() != 2) {
60  po.PrintUsage();
61  exit(1);
62  }
63 
64  std::string lats_rspecifier = po.GetArg(1),
65  lats_wspecifier = po.GetArg(2);
66 
67  int32 n_done = 0;
68 
69  KALDI_ASSERT(acoustic_scale == 1.0 || inv_acoustic_scale == 1.0);
70  if (inv_acoustic_scale != 1.0)
71  acoustic_scale = 1.0 / inv_acoustic_scale;
72 
73  std::vector<std::vector<double> > scale(2);
74  scale[0].resize(2);
75  scale[1].resize(2);
76  scale[0][0] = lm_scale;
77  scale[0][1] = acoustic2lm_scale;
78  scale[1][0] = lm2acoustic_scale;
79  scale[1][1] = acoustic_scale;
80 
81  if (write_compact) {
82  SequentialCompactLatticeReader compact_lattice_reader(lats_rspecifier);
83 
84  // Write as compact lattice.
85  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
86 
87  for (; !compact_lattice_reader.Done(); compact_lattice_reader.Next()) {
88  CompactLattice lat = compact_lattice_reader.Value();
89  ScaleLattice(scale, &lat);
90  compact_lattice_writer.Write(compact_lattice_reader.Key(), lat);
91  n_done++;
92  }
93  } else {
94  SequentialLatticeReader lattice_reader(lats_rspecifier);
95 
96  // Write as regular lattice.
97  LatticeWriter lattice_writer(lats_wspecifier);
98 
99  for (; !lattice_reader.Done(); lattice_reader.Next()) {
100  Lattice lat = lattice_reader.Value();
101  ScaleLattice(scale, &lat);
102  lattice_writer.Write(lattice_reader.Key(), lat);
103  n_done++;
104  }
105  }
106 
107  KALDI_LOG << "Done " << n_done << " lattices.";
108  return (n_done != 0 ? 0 : 1);
109  } catch(const std::exception &e) {
110  std::cerr << e.what();
111  return -1;
112  }
113 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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...
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define KALDI_LOG
Definition: kaldi-error.h:153