lattice-align-words-lexicon.cc
Go to the documentation of this file.
1 // latbin/lattice-align-words-lexicon.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"
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). This is the newest form, that\n"
36  "reads in a lexicon in integer format, where each line is (integer id of)\n"
37  " word-in word-out phone1 phone2 ... phoneN\n"
38  "(note: word-in is word before alignment, word-out is after, e.g. for replacing\n"
39  "<eps> with SIL or vice versa)\n"
40  "This may be more efficient if you first apply 'lattice-push'.\n"
41  "Usage: lattice-align-words-lexicon [options] <lexicon-file> <model> <lattice-rspecifier> <lattice-wspecifier>\n"
42  " e.g.: lattice-align-words-lexicon --partial-word-label=4324 --max-expand 10.0 --test true \\\n"
43  " data/lang/phones/align_lexicon.int final.mdl ark:1.lats ark:aligned.lats\n"
44  "See also: lattice-align-words, which is only applicable if your phones have word-position\n"
45  "markers, i.e. each phone comes in 5 versions like AA_B, AA_I, AA_W, AA_S, AA.\n";
46 
47  ParseOptions po(usage);
48  bool output_if_error = true;
49  bool output_if_empty = false;
50 
51  po.Register("output-error-lats", &output_if_error, "Output lattices that aligned "
52  "with errors (e.g. due to force-out");
53  po.Register("output-if-empty", &output_if_empty, "If true: if algorithm gives "
54  "error and produces empty output, pass the input through.");
55 
57  opts.Register(&po);
58 
59  po.Read(argc, argv);
60 
61  if (po.NumArgs() != 4) {
62  po.PrintUsage();
63  exit(1);
64  }
65 
66  std::string
67  align_lexicon_rxfilename = po.GetArg(1),
68  model_rxfilename = po.GetArg(2),
69  lats_rspecifier = po.GetArg(3),
70  lats_wspecifier = po.GetArg(4);
71 
72  std::vector<std::vector<int32> > lexicon;
73  {
74  bool binary_in;
75  Input ki(align_lexicon_rxfilename, &binary_in);
76  KALDI_ASSERT(!binary_in && "Not expecting binary file for lexicon");
77  if (!ReadLexiconForWordAlign(ki.Stream(), &lexicon)) {
78  KALDI_ERR << "Error reading alignment lexicon from "
79  << align_lexicon_rxfilename;
80  }
81  }
82 
83  TransitionModel tmodel;
84  ReadKaldiObject(model_rxfilename, &tmodel);
85 
86  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
87  CompactLatticeWriter clat_writer(lats_wspecifier);
88 
89  WordAlignLatticeLexiconInfo lexicon_info(lexicon);
90  { std::vector<std::vector<int32> > temp; lexicon.swap(temp); }
91  // No longer needed.
92 
93  int32 num_done = 0, num_err = 0;
94 
95  for (; !clat_reader.Done(); clat_reader.Next()) {
96  std::string key = clat_reader.Key();
97  const CompactLattice &clat = clat_reader.Value();
98 
99  CompactLattice aligned_clat;
100 
101  bool ok = WordAlignLatticeLexicon(clat, tmodel, lexicon_info, opts,
102  &aligned_clat);
103 
104  if (!ok) {
105  num_err++;
106  if (output_if_empty && aligned_clat.NumStates() == 0 &&
107  clat.NumStates() != 0) {
108  KALDI_WARN << "Algorithm produced no output (due to --max-expand?), "
109  << "so passing input through as output, for key " << key;
110  TopSortCompactLatticeIfNeeded(&aligned_clat);
111  clat_writer.Write(key, clat);
112  continue;
113  }
114  if (!output_if_error)
115  KALDI_WARN << "Lattice for " << key << " did not align correctly";
116  else {
117  if (aligned_clat.Start() != fst::kNoStateId) {
118  KALDI_WARN << "Outputting partial lattice for " << key;
119  TopSortCompactLatticeIfNeeded(&aligned_clat);
120  clat_writer.Write(key, aligned_clat);
121  } else {
122  KALDI_WARN << "Empty aligned lattice for " << key
123  << ", producing no output.";
124  }
125  }
126  } else {
127  if (aligned_clat.Start() == fst::kNoStateId) {
128  num_err++;
129  KALDI_WARN << "Lattice was empty for key " << key;
130  } else {
131  num_done++;
132  KALDI_VLOG(2) << "Aligned lattice for " << key;
133  TopSortCompactLatticeIfNeeded(&aligned_clat);
134  clat_writer.Write(key, aligned_clat);
135  }
136  }
137  }
138  KALDI_LOG << "Successfully aligned " << num_done << " lattices; "
139  << num_err << " had errors.";
140  return (num_done > num_err ? 0 : 1); // We changed the error condition slightly here,
141  // if there are errors in the word-boundary phones we can get situations
142  // where most lattices give an error.
143  } catch(const std::exception &e) {
144  std::cerr << e.what();
145  return -1;
146  }
147 }
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 ReadLexiconForWordAlign(std::istream &is, std::vector< std::vector< int32 > > *lexicon)
Read the lexicon in the special format required for word alignment.
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 WordAlignLatticeLexicon(const CompactLattice &lat, const TransitionModel &tmodel, const WordAlignLatticeLexiconInfo &lexicon_info, const WordAlignLatticeLexiconOpts &opts, 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)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
std::istream & Stream()
Definition: kaldi-io.cc:826
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_ERR
Definition: kaldi-error.h:147
#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_ASSERT(cond)
Definition: kaldi-error.h:185
This class extracts some information from the lexicon and stores it in a suitable form for the word-a...
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
int main(int argc, char *argv[])
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
#define KALDI_LOG
Definition: kaldi-error.h:153