lattice-scale.cc
Go to the documentation of this file.
1 // latbin/lattice-scale.cc
2 
3 // Copyright 2009-2013 Microsoft Corporation
4 // Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "fstext/fstext-lib.h"
25 #include "lat/kaldi-lattice.h"
26 
27 int main(int argc, char *argv[]) {
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
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
int main(int argc, char *argv[])
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
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.
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define KALDI_LOG
Definition: kaldi-error.h:153