lattice-copy-backoff.cc
Go to the documentation of this file.
1 // latbin/lattice-copy-backoff.cc
2 
3 // Copyright 2012 Johns Hopkins University (Author: Daniel Povey)
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 "util/common-utils.h"
23 #include "fstext/fstext-lib.h"
24 #include "lat/kaldi-lattice.h"
25 
26 int main(int argc, char *argv[]) {
27  using namespace kaldi;
28  typedef kaldi::int32 int32;
29  try {
30  const char *usage =
31  "Copy a table of lattices (1st argument), but for any keys that appear\n"
32  "in the table from the 2nd argument, use the one from the 2nd argument.\n"
33  "If the sets of keys are identical, this is equivalent to copying the 2nd\n"
34  "table. Note: the arguments are in this order due to the convention that\n"
35  "sequential access is always over the 1st argument.\n"
36  "\n"
37  "Usage: lattice-copy-backoff [options] <lat-rspecifier1> "
38  "<lat-rspecifier2> <lat-wspecifier>\n"
39  " e.g.: lattice-copy-backoff ark:bad_but_complete.lat "
40  "ark:good_but_incomplete.lat ark:out.lat\n";
41 
42  ParseOptions po(usage);
43 
44  po.Read(argc, argv);
45 
46  if (po.NumArgs() != 3) {
47  po.PrintUsage();
48  exit(1);
49  }
50 
51  std::string lats_rspecifier1 = po.GetArg(1),
52  lats_rspecifier2 = po.GetArg(2),
53  lats_wspecifier = po.GetArg(3);
54 
55  int32 n_done = 0, n_backed_off = 0;
56 
57  SequentialCompactLatticeReader lattice_reader1(lats_rspecifier1);
58  RandomAccessCompactLatticeReader lattice_reader2(lats_rspecifier2);
59  CompactLatticeWriter lattice_writer(lats_wspecifier);
60  for (; !lattice_reader1.Done(); lattice_reader1.Next(), n_done++) {
61  const std::string &key = lattice_reader1.Key();
62  if (lattice_reader2.HasKey(key)) {
63  lattice_writer.Write(key, lattice_reader2.Value(key));
64  } else {
65  lattice_writer.Write(key, lattice_reader1.Value());
66  KALDI_VLOG(1) << "Backed off to 1st archive for key " << key;
67  n_backed_off++;
68  }
69  }
70  KALDI_LOG << "Done copying " << n_done << " lattices; backed off to 1st "
71  << "archive for " << n_backed_off << " of those.";
72  return (n_done != 0 ? 0 : 1);
73  } catch(const std::exception &e) {
74  std::cerr << e.what();
75  return -1;
76  }
77 }
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
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
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.
bool HasKey(const std::string &key)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
#define KALDI_LOG
Definition: kaldi-error.h:153