lattice-best-path.cc
Go to the documentation of this file.
1 // latbin/lattice-best-path.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 
27 int main(int argc, char *argv[]) {
28  try {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31  typedef kaldi::int64 int64;
32  using fst::SymbolTable;
33  using fst::VectorFst;
34  using fst::StdArc;
35 
36  const char *usage =
37  "Generate 1-best path through lattices; output as transcriptions and alignments\n"
38  "Note: if you want output as FSTs, use lattice-1best; if you want output\n"
39  "with acoustic and LM scores, use lattice-1best | nbest-to-linear\n"
40  "Usage: lattice-best-path [options] <lattice-rspecifier> [ <transcriptions-wspecifier> [ <alignments-wspecifier>] ]\n"
41  " e.g.: lattice-best-path --acoustic-scale=0.1 ark:1.lats 'ark,t:|int2sym.pl -f 2- words.txt > text' ark:1.ali\n";
42 
43  ParseOptions po(usage);
44  BaseFloat acoustic_scale = 1.0;
45  BaseFloat lm_scale = 1.0;
46 
47  std::string word_syms_filename;
48  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
49  po.Register("lm-scale", &lm_scale, "Scaling factor for LM probabilities. "
50  "Note: the ratio acoustic-scale/lm-scale is all that matters.");
51  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
52 
53  po.Read(argc, argv);
54 
55  if (po.NumArgs() < 1 || po.NumArgs() > 3) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  std::string lats_rspecifier = po.GetArg(1),
61  transcriptions_wspecifier = po.GetOptArg(2),
62  alignments_wspecifier = po.GetOptArg(3);
63 
64  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
65 
66  Int32VectorWriter transcriptions_writer(transcriptions_wspecifier);
67 
68  Int32VectorWriter alignments_writer(alignments_wspecifier);
69 
70  fst::SymbolTable *word_syms = NULL;
71  if (word_syms_filename != "")
72  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
73  KALDI_ERR << "Could not read symbol table from file "
74  << word_syms_filename;
75 
76 
77  int32 n_done = 0, n_fail = 0;
78  int64 n_frame = 0;
79  LatticeWeight tot_weight = LatticeWeight::One();
80 
81  for (; !clat_reader.Done(); clat_reader.Next()) {
82  std::string key = clat_reader.Key();
83  CompactLattice clat = clat_reader.Value();
84  clat_reader.FreeCurrent();
85  fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &clat);
86  CompactLattice clat_best_path;
87  CompactLatticeShortestPath(clat, &clat_best_path); // A specialized
88  // implementation of shortest-path for CompactLattice.
89  Lattice best_path;
90  ConvertLattice(clat_best_path, &best_path);
91  if (best_path.Start() == fst::kNoStateId) {
92  KALDI_WARN << "Best-path failed for key " << key;
93  n_fail++;
94  } else {
95  std::vector<int32> alignment;
96  std::vector<int32> words;
97  LatticeWeight weight;
98  GetLinearSymbolSequence(best_path, &alignment, &words, &weight);
99  KALDI_LOG << "For utterance " << key << ", best cost "
100  << weight.Value1() << " + " << weight.Value2() << " = "
101  << (weight.Value1() + weight.Value2())
102  << " over " << alignment.size() << " frames.";
103  if (transcriptions_wspecifier != "")
104  transcriptions_writer.Write(key, words);
105  if (alignments_wspecifier != "")
106  alignments_writer.Write(key, alignment);
107  if (word_syms != NULL) {
108  std::cerr << key << ' ';
109  for (size_t i = 0; i < words.size(); i++) {
110  std::string s = word_syms->Find(words[i]);
111  if (s == "")
112  KALDI_ERR << "Word-id " << words[i] <<" not in symbol table.";
113  std::cerr << s << ' ';
114  }
115  std::cerr << '\n';
116  }
117  n_done++;
118  n_frame += alignment.size();
119  tot_weight = Times(tot_weight, weight);
120  }
121  }
122 
123  BaseFloat tot_weight_float = tot_weight.Value1() + tot_weight.Value2();
124  KALDI_LOG << "Overall cost per frame is " << (tot_weight_float/n_frame)
125  << " = " << (tot_weight.Value1()/n_frame) << " [graph]"
126  << " + " << (tot_weight.Value2()/n_frame) << " [acoustic]"
127  << " over " << n_frame << " frames.";
128  KALDI_LOG << "Done " << n_done << " lattices, failed for " << n_fail;
129 
130  delete word_syms;
131  if (n_done != 0) return 0;
132  else return 1;
133  } catch(const std::exception &e) {
134  std::cerr << e.what();
135  return -1;
136  }
137 }
int32 words[kMaxOrder]
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
static const LatticeWeightTpl One()
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
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
int main(int argc, char *argv[])
void CompactLatticeShortestPath(const CompactLattice &clat, CompactLattice *shortest_path)
A form of the shortest-path/best-path algorithm that&#39;s specially coded for CompactLattice.
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
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
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
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.
#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_LOG
Definition: kaldi-error.h:153
std::string GetOptArg(int param) const