lattice-to-phone-lattice.cc
Go to the documentation of this file.
1 // latbin/lattice-to-phone-lattice.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
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 "fstext/fstext-lib.h"
24 #include "lat/kaldi-lattice.h"
25 #include "lat/lattice-functions.h"
26 #include "hmm/transition-model.h"
27 
28 int main(int argc, char *argv[]) {
29  try {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32  typedef kaldi::int64 int64;
33  using fst::SymbolTable;
34  using fst::VectorFst;
35  using fst::StdArc;
36 
37  const char *usage =
38  "Convert the words or transition-ids into phones, which are worked out\n"
39  "from the transition-ids. If --replace-words=true (true by default),\n"
40  "replaces the words with phones, otherwise replaces the transition-ids.\n"
41  "If --replace-words=false, it will preserve the alignment of transition-ids/phones\n"
42  "to words, so that if you do \n"
43  "lattice-align-words | lattice-to-phone-lattice --replace-words=false,\n"
44  "you can get the phones corresponding to each word in the lattice.\n"
45  "\n"
46  "Usage: lattice-to-phone-lattice [options] model lattice-rspecifier lattice-wspecifier\n"
47  " e.g.: lattice-to-phone-lattice 1.mdl ark:1.lats ark:phones.lats\n"
48  "See also: lattice-align-words, lattice-align-phones\n";
49 
50  ParseOptions po(usage);
51  bool replace_words = true;
52  po.Register("replace-words", &replace_words,
53  "If true, replace words with phones; otherwise replace "
54  "transition-ids with phones.");
55 
56  po.Read(argc, argv);
57 
58  if (po.NumArgs() != 3) {
59  po.PrintUsage();
60  exit(1);
61  }
62 
63  std::string model_rxfilename = po.GetArg(1),
64  lats_rspecifier = po.GetArg(2),
65  lats_wspecifier = po.GetArg(3);
66 
67  int32 n_done = 0;
68 
69  TransitionModel trans_model;
70 
71  ReadKaldiObject(model_rxfilename, &trans_model);
72 
73  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
74  CompactLatticeWriter clat_writer(lats_wspecifier); // write as compact.
75  for (; !clat_reader.Done(); clat_reader.Next()) {
76  if (replace_words) {
77  Lattice lat;
78  ConvertLattice(clat_reader.Value(), &lat);
79  ConvertLatticeToPhones(trans_model, &lat); // this function replaces words -> phones
80  CompactLattice clat;
81  ConvertLattice(lat, &clat);
82  clat_writer.Write(clat_reader.Key(), clat);
83  } else { // replace transition-ids with phones.
84  CompactLattice clat(clat_reader.Value());
85  ConvertCompactLatticeToPhones(trans_model, &clat);
86  // this function replaces transition-ids with phones. We do it in the
87  // CompactLattice form, in order to preserve the alignment of
88  // transition-id sequences/phones-sequences to words [e.g. if you just
89  // did lattice-align-words].
90  clat_writer.Write(clat_reader.Key(), clat);
91  }
92  n_done++;
93  }
94  KALDI_LOG << "Done converting " << n_done << " lattices.";
95  return (n_done != 0 ? 0 : 1);
96  } catch(const std::exception &e) {
97  std::cerr << e.what();
98  return -1;
99  }
100 }
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 ConvertCompactLatticeToPhones(const TransitionModel &trans, CompactLattice *clat)
Given a lattice, and a transition model to map pdf-ids to phones, replace the sequences of transition...
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
int main(int argc, char *argv[])
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
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).
void ConvertLatticeToPhones(const TransitionModel &trans, Lattice *lat)
Given a lattice, and a transition model to map pdf-ids to phones, replace the output symbols (presuma...
#define KALDI_LOG
Definition: kaldi-error.h:153