lattice-align-phones.cc File Reference
Include dependency graph for lattice-align-phones.cc:

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 27 of file lattice-align-phones.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), kaldi::PhoneAlignLattice(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), PhoneAlignLatticeOptions::Register(), ParseOptions::Register(), kaldi::TopSortCompactLatticeIfNeeded(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

27  {
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
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 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
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
#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