lattice-lmrescore-rnnlm.cc
Go to the documentation of this file.
1 // latbin/lattice-lmrescore-rnnlm.cc
2 
3 // Copyright 2015 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/kaldi-rnnlm.h"
26 #include "lm/mikolov-rnnlm-lib.h"
27 #include "util/common-utils.h"
28 
29 int main(int argc, char *argv[]) {
30  try {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  typedef kaldi::int64 int64;
34 
35  const char *usage =
36  "Rescores lattice with rnnlm. The LM will be wrapped into the\n"
37  "DeterministicOnDemandFst interface and the rescoring is done by\n"
38  "composing with the wrapped LM using a special type of composition\n"
39  "algorithm. Determinization will be applied on the composed lattice.\n"
40  "\n"
41  "Usage: lattice-lmrescore-rnnlm [options] [unk_prob_rspecifier] \\\n"
42  " <word-symbol-table-rxfilename> <lattice-rspecifier> \\\n"
43  " <rnnlm-rxfilename> <lattice-wspecifier>\n"
44  " e.g.: lattice-lmrescore-rnnlm --lm-scale=-1.0 words.txt \\\n"
45  " ark:in.lats rnnlm ark:out.lats\n";
46 
47  ParseOptions po(usage);
48  int32 max_ngram_order = 3;
49  BaseFloat lm_scale = 1.0;
50 
51  po.Register("lm-scale", &lm_scale, "Scaling factor for language model "
52  "costs; frequently 1.0 or -1.0");
53  po.Register("max-ngram-order", &max_ngram_order, "If positive, limit the "
54  "rnnlm context to the given number, -1 means we are not going "
55  "to limit it.");
56 
58  opts.Register(&po);
59 
60  po.Read(argc, argv);
61 
62  if (po.NumArgs() != 4 && po.NumArgs() != 5) {
63  po.PrintUsage();
64  exit(1);
65  }
66 
67  std::string lats_rspecifier, unk_prob_rspecifier,
68  word_symbols_rxfilename, rnnlm_rxfilename, lats_wspecifier;
69  if (po.NumArgs() == 4) {
70  unk_prob_rspecifier = "";
71  word_symbols_rxfilename = po.GetArg(1);
72  lats_rspecifier = po.GetArg(2);
73  rnnlm_rxfilename = po.GetArg(3);
74  lats_wspecifier = po.GetArg(4);
75  } else if (po.NumArgs() == 5) {
76  unk_prob_rspecifier = po.GetArg(1);
77  word_symbols_rxfilename = po.GetArg(2);
78  lats_rspecifier = po.GetArg(3);
79  rnnlm_rxfilename = po.GetArg(4);
80  lats_wspecifier = po.GetArg(5);
81  }
82 
83  // Reads the language model.
84  KaldiRnnlmWrapper rnnlm(opts, unk_prob_rspecifier,
85  word_symbols_rxfilename, rnnlm_rxfilename);
86 
87  // Reads and writes as compact lattice.
88  SequentialCompactLatticeReader compact_lattice_reader(lats_rspecifier);
89  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
90 
91  int32 n_done = 0, n_fail = 0;
92  for (; !compact_lattice_reader.Done(); compact_lattice_reader.Next()) {
93  std::string key = compact_lattice_reader.Key();
94  CompactLattice &clat = compact_lattice_reader.Value();
95 
96  if (lm_scale != 0.0) {
97  // Before composing with the LM FST, we scale the lattice weights
98  // by the inverse of "lm_scale". We'll later scale by "lm_scale".
99  // We do it this way so we can determinize and it will give the
100  // right effect (taking the "best path" through the LM) regardless
101  // of the sign of lm_scale.
102  fst::ScaleLattice(fst::GraphLatticeScale(1.0 / lm_scale), &clat);
103  ArcSort(&clat, fst::OLabelCompare<CompactLatticeArc>());
104 
105  // Wraps the rnnlm into FST. We re-create it for each lattice to prevent
106  // memory usage increasing with time.
107  RnnlmDeterministicFst rnnlm_fst(max_ngram_order, &rnnlm);
108 
109  // Composes lattice with language model.
110  CompactLattice composed_clat;
111  ComposeCompactLatticeDeterministic(clat, &rnnlm_fst, &composed_clat);
112 
113  // Determinizes the composed lattice.
114  Lattice composed_lat;
115  ConvertLattice(composed_clat, &composed_lat);
116  Invert(&composed_lat);
117  CompactLattice determinized_clat;
118  DeterminizeLattice(composed_lat, &determinized_clat);
119  fst::ScaleLattice(fst::GraphLatticeScale(lm_scale), &determinized_clat);
120  if (determinized_clat.Start() == fst::kNoStateId) {
121  KALDI_WARN << "Empty lattice for utterance " << key
122  << " (incompatible LM?)";
123  n_fail++;
124  } else {
125  compact_lattice_writer.Write(key, determinized_clat);
126  n_done++;
127  }
128  } else {
129  // Zero scale so nothing to do.
130  n_done++;
131  compact_lattice_writer.Write(key, clat);
132  }
133  }
134 
135  KALDI_LOG << "Done " << n_done << " lattices, failed for " << n_fail;
136  return (n_done != 0 ? 0 : 1);
137  } catch(const std::exception &e) {
138  std::cerr << e.what();
139  return -1;
140  }
141 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main(int argc, char *argv[])
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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
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...
void Register(OptionsItf *opts)
Definition: kaldi-rnnlm.h:39
#define KALDI_LOG
Definition: kaldi-error.h:153