lattice-align-words-lexicon.cc File Reference
Include dependency graph for lattice-align-words-lexicon.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-words-lexicon.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), kaldi::ReadLexiconForWordAlign(), ParseOptions::Register(), WordAlignLatticeLexiconOpts::Register(), Input::Stream(), kaldi::TopSortCompactLatticeIfNeeded(), SequentialTableReader< Holder >::Value(), kaldi::WordAlignLatticeLexicon(), 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  "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
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 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_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
#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
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
#define KALDI_LOG
Definition: kaldi-error.h:153