lattice-align-words.cc
Go to the documentation of this file.
1 // latbin/lattice-align-words.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 "lat/kaldi-lattice.h"
24 #include "lat/word-align-lattice.h"
25 #include "lat/lattice-functions.h"
26 
27 int main(int argc, char *argv[]) {
28  try {
29  using namespace kaldi;
30  using fst::StdArc;
31  using kaldi::int32;
32 
33  const char *usage =
34  "Convert lattices so that the arcs in the CompactLattice format correspond with\n"
35  "words (i.e. aligned with word boundaries). Note: it will generally be more\n"
36  "efficient if you apply 'lattice-push' before this program.\n"
37  "Usage: lattice-align-words [options] <word-boundary-file> <model> <lattice-rspecifier> <lattice-wspecifier>\n"
38  " e.g.: lattice-align-words --silence-label=4320 --partial-word-label=4324 \\\n"
39  " data/lang/phones/word_boundary.int final.mdl ark:1.lats ark:aligned.lats\n"
40  "Note: word-boundary file has format (on each line):\n"
41  "<integer-phone-id> [begin|end|singleton|internal|nonword]\n"
42  "See also: lattice-align-words-lexicon, for use in cases where phones\n"
43  "don't have word-position information.\n";
44 
45  ParseOptions po(usage);
46  BaseFloat max_expand = 0.0;
47  bool output_if_error = true;
48  bool do_test = false;
49 
50  po.Register("output-error-lats", &output_if_error, "Output lattices that aligned "
51  "with errors (e.g. due to force-out");
52  po.Register("test", &do_test, "Test the algorithm while running it.");
53  po.Register("max-expand", &max_expand, "If >0, the maximum amount by which this "
54  "program will expand lattices before refusing to continue. E.g. 10."
55  "This can be used to prevent this program consuming excessive memory "
56  "if there is a mismatch on the command-line or a 'problem' lattice.");
57 
59  opts.Register(&po);
60 
61  po.Read(argc, argv);
62 
63  if (po.NumArgs() != 4) {
64  po.PrintUsage();
65  exit(1);
66  }
67 
68  std::string
69  word_boundary_rxfilename = po.GetArg(1),
70  model_rxfilename = po.GetArg(2),
71  lats_rspecifier = po.GetArg(3),
72  lats_wspecifier = po.GetArg(4);
73 
74  TransitionModel tmodel;
75  ReadKaldiObject(model_rxfilename, &tmodel);
76 
77  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
78  CompactLatticeWriter clat_writer(lats_wspecifier);
79 
80  WordBoundaryInfo info(opts, word_boundary_rxfilename);
81 
82  int32 num_done = 0, num_err = 0;
83 
84  for (; !clat_reader.Done(); clat_reader.Next()) {
85  std::string key = clat_reader.Key();
86  const CompactLattice &clat = clat_reader.Value();
87 
88  CompactLattice aligned_clat;
89  int32 max_states;
90  if (max_expand > 0) max_states = 1000 + max_expand * clat.NumStates();
91  else max_states = 0;
92 
93  bool ok = WordAlignLattice(clat, tmodel, info, max_states, &aligned_clat);
94 
95  if (do_test && ok)
96  TestWordAlignedLattice(clat, tmodel, info, aligned_clat);
97 
98  if (!ok) {
99  num_err++;
100  if (!output_if_error)
101  KALDI_WARN << "Lattice for " << key
102  << " did not align correctly, producing no output.";
103  else {
104  if (aligned_clat.Start() != fst::kNoStateId) {
105  KALDI_WARN << "Outputting partial lattice for " << key;
106  TopSortCompactLatticeIfNeeded(&aligned_clat);
107  clat_writer.Write(key, aligned_clat);
108  } else {
109  KALDI_WARN << "Empty aligned lattice for " << key
110  << ", producing no output.";
111  }
112  }
113  } else {
114  if (aligned_clat.Start() == fst::kNoStateId) {
115  num_err++;
116  KALDI_WARN << "Lattice was empty for key " << key;
117  } else {
118  num_done++;
119  KALDI_VLOG(2) << "Aligned lattice for " << key;
120  TopSortCompactLatticeIfNeeded(&aligned_clat);
121  clat_writer.Write(key, aligned_clat);
122  }
123  }
124  }
125  KALDI_LOG << "Successfully aligned " << num_done << " lattices; "
126  << num_err << " had errors.";
127  return (num_done > num_err ? 0 : 1); // We changed the error condition slightly here,
128  // if there are errors in the word-boundary phones we can get situations
129  // where most lattices give an error.
130  } catch(const std::exception &e) {
131  std::cerr << e.what();
132  return -1;
133  }
134 }
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
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
bool WordAlignLattice(const CompactLattice &lat, const TransitionModel &tmodel, const WordBoundaryInfo &info, int32 max_states, CompactLattice *lat_out)
Align lattice so that each arc has the transition-ids on it that correspond to the word that is on th...
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
static bool TestWordAlignedLattice(const WordAlignLatticeLexiconInfo &lexicon_info, const TransitionModel &tmodel, CompactLattice clat, CompactLattice aligned_clat, bool allow_duplicate_paths)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
int main(int argc, char *argv[])
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 Register(OptionsItf *opts)
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
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
#define KALDI_LOG
Definition: kaldi-error.h:153