lattice-add-penalty.cc
Go to the documentation of this file.
1 // latbin/lattice-add-penalty.cc
2 
3 // Copyright 2013 Bagher BabaAli
4 // Johns Hopkins University (Author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "lat/lattice-functions.h"
22 
23 int main(int argc, char *argv[]) {
24  using namespace kaldi;
25  typedef kaldi::int64 int64;
26  try {
27  const char *usage =
28  "Add word insertion penalty to the lattice.\n"
29  "Note: penalties are negative log-probs, base e, and are added to the\n"
30  "'language model' part of the cost.\n"
31  "\n"
32  "Usage: lattice-add-penalty [options] <lattice-rspecifier> <lattice-wspecifier>\n"
33  " e.g.: lattice-add-penalty --word-ins-penalty=1.0 ark:- ark:-\n";
34 
35  ParseOptions po(usage);
36 
37  BaseFloat word_ins_penalty = 0.0;
38 
39  po.Register("word-ins-penalty", &word_ins_penalty, "Word insertion penalty");
40 
41  po.Read(argc, argv);
42 
43  if (po.NumArgs() != 2) {
44  po.PrintUsage();
45  exit(1);
46  }
47 
48  std::string lats_rspecifier = po.GetArg(1),
49  lats_wspecifier = po.GetArg(2);
50 
51  SequentialCompactLatticeReader clat_reader(lats_rspecifier);
52  CompactLatticeWriter clat_writer(lats_wspecifier); // write as compact.
53 
54  int64 n_done = 0;
55 
56  for (; !clat_reader.Done(); clat_reader.Next()) {
57  CompactLattice clat(clat_reader.Value());
58  AddWordInsPenToCompactLattice(word_ins_penalty, &clat);
59  clat_writer.Write(clat_reader.Key(), clat);
60  n_done++;
61  }
62  KALDI_LOG << "Done adding word insertion penalty to " << n_done << " lattices.";
63  return (n_done != 0 ? 0 : 1);
64  } catch(const std::exception &e) {
65  std::cerr << e.what();
66  return -1;
67  }
68 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main(int argc, char *argv[])
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
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.
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).
#define KALDI_LOG
Definition: kaldi-error.h:153