lattice-align-phones.cc
Go to the documentation of this file.
1 // latbin/lattice-align-phones.cc
2 
3 // Copyright 2012-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 "lat/kaldi-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  "phones. The output symbols are still words, unless you specify --replace-output-symbols=true\n"
36  "Usage: lattice-align-phones [options] <model> <lattice-rspecifier> <lattice-wspecifier>\n"
37  " e.g.: lattice-align-phones final.mdl ark:1.lats ark:phone_aligned.lats\n"
38  "See also: lattice-to-phone-lattice, lattice-align-words, lattice-align-words-lexicon\n"
39  "Note: if you just want the phone alignment from a lattice, the easiest path is\n"
40  " lattice-1best | nbest-to-linear [keeping only alignment] | ali-to-phones\n"
41  "If you want the words and phones jointly (i.e. pronunciations of words, with word\n"
42  "alignment), try\n"
43  " lattice-1best | nbest-to-prons\n";
44 
45  ParseOptions po(usage);
46  bool output_if_error = true;
47 
48  po.Register("output-error-lats", &output_if_error, "Output lattices that aligned "
49  "with errors (e.g. due to force-out");
50 
52  opts.Register(&po);
53 
54  po.Read(argc, argv);
55 
56  if (po.NumArgs() != 3) {
57  po.PrintUsage();
58  exit(1);
59  }
60 
61  std::string
62  model_rxfilename = po.GetArg(1),
63  lats_rspecifier = po.GetArg(2),
64  lats_wspecifier = po.GetArg(3);
65 
66  TransitionModel tmodel;
67  ReadKaldiObject(model_rxfilename, &tmodel);
68 
69  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
70  CompactLatticeWriter clat_writer(lats_wspecifier);
71 
72  int32 num_done = 0, num_err = 0;
73 
74  for (; !clat_reader.Done(); clat_reader.Next()) {
75  std::string key = clat_reader.Key();
76  const CompactLattice &clat = clat_reader.Value();
77 
78  CompactLattice aligned_clat;
79  bool ok = PhoneAlignLattice(clat, tmodel, opts, &aligned_clat);
80 
81  if (!ok) {
82  num_err++;
83  if (!output_if_error)
84  KALDI_WARN << "Lattice for " << key << " did align correctly";
85  else {
86  if (aligned_clat.Start() != fst::kNoStateId) {
87  KALDI_LOG << "Outputting partial lattice for " << key;
88  TopSortCompactLatticeIfNeeded(&aligned_clat);
89  clat_writer.Write(key, aligned_clat);
90  }
91  }
92  } else {
93  if (aligned_clat.Start() == fst::kNoStateId) {
94  num_err++;
95  KALDI_WARN << "Lattice was empty for key " << key;
96  } else {
97  num_done++;
98  KALDI_VLOG(2) << "Aligned lattice for " << key;
99  TopSortCompactLatticeIfNeeded(&aligned_clat);
100  clat_writer.Write(key, aligned_clat);
101  }
102  }
103  }
104  KALDI_LOG << "Successfully aligned " << num_done << " lattices; "
105  << num_err << " had errors.";
106  return (num_done > num_err ? 0 : 1); // We changed the error condition slightly here,
107  // if there are errors in the word-boundary phones we can get situations
108  // where most lattices give an error.
109  } catch(const std::exception &e) {
110  std::cerr << e.what();
111  return -1;
112  }
113 }
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
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
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
bool PhoneAlignLattice(const CompactLattice &lat, const TransitionModel &tmodel, const PhoneAlignLatticeOptions &opts, CompactLattice *lat_out)
Outputs a lattice in which the arcs correspond exactly to sequences of phones, so the boundaries betw...
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
#define KALDI_LOG
Definition: kaldi-error.h:153
int main(int argc, char *argv[])