lattice-combine.cc File Reference
#include <string>
#include <vector>
#include "util/common-utils.h"
#include "lat/lattice-functions.h"
#include "lat/kaldi-lattice.h"
#include "lat/sausages.h"
Include dependency graph for lattice-combine.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

bool CompactLatticeNormalize (CompactLattice *clat, BaseFloat weight)
 
void SplitStringToWeights (const string &full, const char *delim, vector< BaseFloat > *out)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 122 of file lattice-combine.cc.

References kaldi::CompactLatticeNormalize(), kaldi::DeletePointers(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), rnnlm::i, KALDI_ASSERT, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), fst::LatticeScale(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), fst::ScaleLattice(), kaldi::SplitStringToWeights(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

122  {
123  try {
124  using namespace kaldi;
125  typedef kaldi::int32 int32;
126 
127  const char *usage =
128  "Combine lattices generated by different systems by removing the total\n"
129  "cost of all paths (backward cost) from individual lattices and doing\n"
130  "a union of the reweighted lattices. Note: the acoustic and LM scales\n"
131  "that this program applies are not removed before outputting the lattices.\n"
132  "Intended for use in system combination prior to MBR decoding, see comments\n"
133  "in code.\n"
134  "Usage: lattice-combine [options] <lattice-rspecifier1> <lattice-rspecifier2>"
135  " [<lattice-rspecifier3> ... ] <lattice-wspecifier>\n"
136  "E.g.: lattice-combine 'ark:gunzip -c foo/lat.1.gz|' 'ark:gunzip -c bar/lat.1.gz|' ark:- | ...\n";
137 
138  ParseOptions po(usage);
139  BaseFloat acoustic_scale = 1.0, inv_acoustic_scale = 1.0, lm_scale = 1.0;
140  string weight_str;
141  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for "
142  "acoustic likelihoods");
143  po.Register("inv-acoustic-scale", &inv_acoustic_scale, "An alternative way "
144  "of setting the acoustic scale: you can set its inverse.");
145  po.Register("lm-scale", &lm_scale, "Scaling factor for language model "
146  "probabilities");
147  po.Register("lat-weights", &weight_str, "Colon-separated list of weights "
148  "for each rspecifier (which should sum to 1), e.g. '0.2:0.8'");
149 
150  po.Read(argc, argv);
151 
152  KALDI_ASSERT(acoustic_scale == 1.0 || inv_acoustic_scale == 1.0);
153  if (inv_acoustic_scale != 1.0)
154  acoustic_scale = 1.0 / inv_acoustic_scale;
155 
156 
157  int32 num_args = po.NumArgs();
158  if (num_args < 3) {
159  po.PrintUsage();
160  exit(1);
161  }
162 
163  string lats_rspecifier1 = po.GetArg(1),
164  lats_wspecifier = po.GetArg(num_args);
165 
166  // Output lattice
167  CompactLatticeWriter clat_writer(lats_wspecifier);
168 
169  // Input lattices
170  SequentialCompactLatticeReader clat_reader1(lats_rspecifier1);
171  vector<RandomAccessCompactLatticeReader*> clat_reader_vec(
172  num_args-2, static_cast<RandomAccessCompactLatticeReader*>(NULL));
173  vector<string> clat_rspec_vec(num_args-2);
174  for (int32 i = 2; i < num_args; ++i) {
175  clat_reader_vec[i-2] = new RandomAccessCompactLatticeReader(po.GetArg(i));
176  clat_rspec_vec[i-2] = po.GetArg(i);
177  }
178 
179  vector<BaseFloat> lat_weights(num_args-1, 1.0/(num_args-1));
180  if (!weight_str.empty())
181  SplitStringToWeights(weight_str, ":", &lat_weights);
182 
183  int32 n_utts = 0, n_total_lats = 0, n_success = 0, n_missing = 0,
184  n_other_errors = 0;
185  vector< vector<double> > lat_scale = fst::LatticeScale(lm_scale,
186  acoustic_scale);
187 
188  for (; !clat_reader1.Done(); clat_reader1.Next()) {
189  std::string key = clat_reader1.Key();
190  CompactLattice clat1 = clat_reader1.Value();
191  clat_reader1.FreeCurrent();
192  n_utts++;
193  n_total_lats++;
194  fst::ScaleLattice(lat_scale, &clat1);
195  bool success = CompactLatticeNormalize(&clat1, lat_weights[0]);
196  if (!success) {
197  KALDI_WARN << "Could not normalize lattice for system 1, utterance: "
198  << key;
199  n_other_errors++;
200  continue;
201  }
202 
203  for (int32 i = 0; i < num_args-2; ++i) {
204  if (clat_reader_vec[i]->HasKey(key)) {
205  CompactLattice clat2 = clat_reader_vec[i]->Value(key);
206  n_total_lats++;
207  fst::ScaleLattice(lat_scale, &clat2);
208  success = CompactLatticeNormalize(&clat2, lat_weights[i+1]);
209  if (!success) {
210  KALDI_WARN << "Could not normalize lattice for system "<< (i + 2)
211  << ", utterance: " << key;
212  n_other_errors++;
213  continue;
214  }
215  fst::Union(&clat1, clat2);
216  } else {
217  KALDI_WARN << "No lattice found for utterance " << key << " for "
218  << "system " << (i + 2) << ", rspecifier: "
219  << clat_rspec_vec[i];
220  n_missing++;
221  }
222  }
223 
224  clat_writer.Write(key, clat1);
225  n_success++;
226  }
227 
228  KALDI_LOG << "Processed " << n_utts << " utterances: with a total of "
229  << n_total_lats << " lattices across " << (num_args-1)
230  << " different systems";
231  KALDI_LOG << "Produced output for " << n_success << " utterances; "
232  << n_missing << " total missing lattices and " << n_other_errors
233  << " total lattices had errors in processing.";
234  DeletePointers(&clat_reader_vec);
235  // The success code we choose is that at least one lattice was output,
236  // and more lattices were "all there" than had at least one system missing.
237  return (n_success != 0 && n_missing < (n_success - n_missing) ? 0 : 1);
238  } catch(const std::exception &e) {
239  std::cerr << e.what();
240  return -1;
241  }
242 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:184
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...
bool CompactLatticeNormalize(CompactLattice *clat, BaseFloat weight)
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)
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
RandomAccessTableReader< CompactLatticeHolder > RandomAccessCompactLatticeReader
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void SplitStringToWeights(const string &full, const char *delim, vector< BaseFloat > *out)
#define KALDI_LOG
Definition: kaldi-error.h:153