lattice-lmrescore-const-arpa.cc
Go to the documentation of this file.
1 // latbin/lattice-lmrescore-const-arpa.cc
2 
3 // Copyright 2014 Guoguo Chen
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "base/kaldi-common.h"
22 #include "fstext/fstext-lib.h"
23 #include "lat/kaldi-lattice.h"
24 #include "lat/lattice-functions.h"
25 #include "lm/const-arpa-lm.h"
26 #include "util/common-utils.h"
27 
28 int main(int argc, char *argv[]) {
29  try {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32  typedef kaldi::int64 int64;
33 
34  const char *usage =
35  "Rescores lattice with the ConstArpaLm format language model. The LM\n"
36  "will be wrapped into the DeterministicOnDemandFst interface and the\n"
37  "rescoring is done by composing with the wrapped LM using a special\n"
38  "type of composition algorithm. Determinization will be applied on\n"
39  "the composed lattice.\n"
40  "\n"
41  "Usage: lattice-lmrescore-const-arpa [options] lattice-rspecifier \\\n"
42  " const-arpa-in lattice-wspecifier\n"
43  " e.g.: lattice-lmrescore-const-arpa --lm-scale=-1.0 ark:in.lats \\\n"
44  " const_arpa ark:out.lats\n";
45 
46  ParseOptions po(usage);
47  BaseFloat lm_scale = 1.0;
48 
49  po.Register("lm-scale", &lm_scale, "Scaling factor for language model "
50  "costs; frequently 1.0 or -1.0");
51 
52  po.Read(argc, argv);
53 
54  if (po.NumArgs() != 3) {
55  po.PrintUsage();
56  exit(1);
57  }
58 
59  std::string lats_rspecifier = po.GetArg(1),
60  lm_rxfilename = po.GetArg(2),
61  lats_wspecifier = po.GetArg(3);
62 
63  // Reads the language model in ConstArpaLm format.
64  ConstArpaLm const_arpa;
65  ReadKaldiObject(lm_rxfilename, &const_arpa);
66 
67  // Reads and writes as compact lattice.
68  SequentialCompactLatticeReader compact_lattice_reader(lats_rspecifier);
69  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
70 
71  int32 n_done = 0, n_fail = 0;
72  for (; !compact_lattice_reader.Done(); compact_lattice_reader.Next()) {
73  std::string key = compact_lattice_reader.Key();
74  CompactLattice clat = compact_lattice_reader.Value();
75  compact_lattice_reader.FreeCurrent();
76 
77  if (lm_scale != 0.0) {
78  // Before composing with the LM FST, we scale the lattice weights
79  // by the inverse of "lm_scale". We'll later scale by "lm_scale".
80  // We do it this way so we can determinize and it will give the
81  // right effect (taking the "best path" through the LM) regardless
82  // of the sign of lm_scale.
83  fst::ScaleLattice(fst::GraphLatticeScale(1.0/lm_scale), &clat);
84  ArcSort(&clat, fst::OLabelCompare<CompactLatticeArc>());
85 
86  // Wraps the ConstArpaLm format language model into FST. We re-create it
87  // for each lattice to prevent memory usage increasing with time.
88  ConstArpaLmDeterministicFst const_arpa_fst(const_arpa);
89 
90  // Composes lattice with language model.
91  CompactLattice composed_clat;
93  &const_arpa_fst, &composed_clat);
94 
95  // Determinizes the composed lattice.
96  Lattice composed_lat;
97  ConvertLattice(composed_clat, &composed_lat);
98  Invert(&composed_lat);
99  CompactLattice determinized_clat;
100  DeterminizeLattice(composed_lat, &determinized_clat);
101  fst::ScaleLattice(fst::GraphLatticeScale(lm_scale), &determinized_clat);
102  if (determinized_clat.Start() == fst::kNoStateId) {
103  KALDI_WARN << "Empty lattice for utterance " << key
104  << " (incompatible LM?)";
105  n_fail++;
106  } else {
107  compact_lattice_writer.Write(key, determinized_clat);
108  n_done++;
109  }
110  } else {
111  // Zero scale so nothing to do.
112  n_done++;
113  compact_lattice_writer.Write(key, clat);
114  }
115  }
116 
117  KALDI_LOG << "Done " << n_done << " lattices, failed for " << n_fail;
118  return (n_done != 0 ? 0 : 1);
119  } catch(const std::exception &e) {
120  std::cerr << e.what();
121  return -1;
122  }
123 }
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].
int main(int argc, char *argv[])
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)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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
void ComposeCompactLatticeDeterministic(const CompactLattice &clat, fst::DeterministicOnDemandFst< fst::StdArc > *det_fst, CompactLattice *composed_clat)
This function Composes a CompactLattice format lattice with a DeterministicOnDemandFst<fst::StdFst> f...
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.
#define KALDI_WARN
Definition: kaldi-error.h:150
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
std::vector< std::vector< double > > GraphLatticeScale(double lmwt)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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...
#define KALDI_LOG
Definition: kaldi-error.h:153
This class wraps a ConstArpaLm format language model with the interface defined in DeterministicOnDem...