lattice-1best.cc
Go to the documentation of this file.
1 // latbin/lattice-1best.cc
2 
3 // Copyright 2009-2012 Stefan Kombrink Johns Hopkins University (Author: Daniel Povey)
4 // 2018 Music Technology Group, Universitat Pompeu Fabra (Rong Gong)
5 
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 
23 #include "base/kaldi-common.h"
24 #include "util/common-utils.h"
25 #include "fstext/fstext-lib.h"
26 #include "lat/kaldi-lattice.h"
27 #include "lat/lattice-functions.h"
28 
29 int main(int argc, char *argv[]) {
30  try {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  typedef kaldi::int64 int64;
34  using fst::VectorFst;
35  using fst::StdArc;
36 
37  const char *usage =
38  "Compute best path through lattices and write out as FSTs\n"
39  "Note: differs from lattice-nbest with --n=1 because we won't\n"
40  "append -1 to the utterance-ids. Differs from lattice-best-path\n"
41  "because output is FST.\n"
42  "\n"
43  "Usage: lattice-1best [options] <lattice-rspecifier> <lattice-wspecifier>\n"
44  " e.g.: lattice-1best --acoustic-scale=0.1 ark:1.lats ark:1best.lats\n";
45 
46  ParseOptions po(usage);
47  BaseFloat acoustic_scale = 1.0;
48  BaseFloat lm_scale = 1.0;
49  BaseFloat word_ins_penalty = 0.0;
50 
51  po.Register("acoustic-scale", &acoustic_scale,
52  "Scaling factor for acoustic likelihoods");
53  po.Register("lm-scale", &lm_scale,
54  "Scaling factor for language model scores.");
55  po.Register("word-ins-penalty", &word_ins_penalty,
56  "Word insertion penality.");
57 
58  po.Read(argc, argv);
59 
60  if (po.NumArgs() != 2) {
61  po.PrintUsage();
62  exit(1);
63  }
64 
65  std::string lats_rspecifier = po.GetArg(1),
66  lats_wspecifier = po.GetArg(2);
67 
68  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
69 
70  // Write as compact lattice.
71  CompactLatticeWriter compact_1best_writer(lats_wspecifier);
72 
73  int32 n_done = 0, n_err = 0;
74 
75  if (acoustic_scale == 0.0 || lm_scale == 0.0)
76  KALDI_ERR << "Do not use exactly zero acoustic or LM scale (cannot be inverted)";
77  for (; !clat_reader.Done(); clat_reader.Next()) {
78  std::string key = clat_reader.Key();
79  CompactLattice clat = clat_reader.Value();
80  clat_reader.FreeCurrent();
81  fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &clat);
82  if (word_ins_penalty > 0.0) {
83  AddWordInsPenToCompactLattice(word_ins_penalty, &clat);
84  }
85 
86  CompactLattice best_path;
87  CompactLatticeShortestPath(clat, &best_path);
88 
89  if (best_path.Start() == fst::kNoStateId) {
90  KALDI_WARN << "Possibly empty lattice for utterance-id " << key
91  << "(no output)";
92  n_err++;
93  } else {
94  if (word_ins_penalty > 0.0) {
95  AddWordInsPenToCompactLattice(-word_ins_penalty, &best_path);
96  }
97  fst::ScaleLattice(fst::LatticeScale(1.0 / lm_scale, 1.0/acoustic_scale),
98  &best_path);
99  compact_1best_writer.Write(key, best_path);
100  n_done++;
101  }
102  }
103  KALDI_LOG << "Done converting " << n_done << " to best path, "
104  << n_err << " had errors.";
105  return (n_done != 0 ? 0 : 1);
106  } catch(const std::exception &e) {
107  std::cerr << e.what();
108  return -1;
109  }
110 }
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 CompactLatticeShortestPath(const CompactLattice &clat, CompactLattice *shortest_path)
A form of the shortest-path/best-path algorithm that&#39;s specially coded for CompactLattice.
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...
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)
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.
void AddWordInsPenToCompactLattice(BaseFloat word_ins_penalty, CompactLattice *clat)
This function add the word insertion penalty to graph score of each word in the compact lattice...
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153