lattice-push.cc
Go to the documentation of this file.
1 // latbin/lattice-push.cc
2 
3 // Copyright 2013 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 #include "lat/push-lattice.h"
26 
27 
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  using fst::SymbolTable;
35  using fst::VectorFst;
36  using fst::StdArc;
37 
38  const char *usage =
39  "Push lattices, in CompactLattice format, so that the strings are as\n"
40  "close to the start as possible, and the lowest cost weight for each\n"
41  "state except the start state is (0, 0). This can be helpful prior to\n"
42  "word-alignment (in this case, only strings need to be pushed)\n"
43  "\n"
44  "Usage: lattice-push [options] lattice-rspecifier lattice-wspecifier\n"
45  " e.g.: lattice-push ark:1.lats ark:2.lats\n";
46 
47  ParseOptions po(usage);
48 
49  bool push_strings = true;
50  bool push_weights = true;
51 
52  po.Register("push-strings", &push_strings, "If true, push the strings in the "
53  "lattice to the start.");
54  po.Register("push-weights", &push_weights, "If true, push the weights in the "
55  "lattice to the start.");
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 
68  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
69  CompactLatticeWriter clat_writer(lats_wspecifier);
70 
71  int32 n_done = 0, n_err = 0;
72 
73 
74  for (; !clat_reader.Done(); clat_reader.Next()) {
75  std::string key = clat_reader.Key();
76  CompactLattice clat = clat_reader.Value();
77  KALDI_VLOG(1) << "Processing lattice for utterance " << key;
78  if (push_strings && !PushCompactLatticeStrings(&clat)) {
79  KALDI_WARN << "Failure in pushing lattice strings (bad lattice?), "
80  << "for key " << key;
81  n_err++;
82  continue;
83  }
84  if (push_weights && !PushCompactLatticeWeights(&clat)) {
85  KALDI_WARN << "Failure in pushing lattice weights (bad lattice?),"
86  << "for key " << key ;
87  n_err++;
88  continue;
89  }
90  if (clat.NumStates() == 0) {
91  KALDI_WARN << "Empty lattice for key " << key;
92  n_err++;
93  continue;
94  }
95  clat_writer.Write(key, clat);
96  n_done++;
97  }
98  KALDI_LOG << "Pushed " << n_done << " lattices, errors on " << n_err;
99  return (n_done != 0 ? 0 : 1);
100  } catch(const std::exception &e) {
101  std::cerr << e.what();
102  return -1;
103  }
104 }
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
bool PushCompactLatticeStrings(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
This function pushes the transition-ids as far towards the start as they will go. ...
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)
bool PushCompactLatticeWeights(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
This function pushes the weights in the CompactLattice so that all states except possibly the start s...
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[])
Definition: lattice-push.cc:29
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.
#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
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