nbest-to-linear.cc
Go to the documentation of this file.
1 // latbin/nbest-to-linear.cc
2 
3 // Copyright 2012 Johns Hopkins University (Author: Daniel Povey)
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 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29  typedef kaldi::int32 int32;
30  typedef kaldi::int64 int64;
31  using fst::SymbolTable;
32  using fst::VectorFst;
33  using fst::StdArc;
34 
35  const char *usage =
36  "Takes as input lattices/n-bests which must be linear (single path);\n"
37  "convert from lattice to up to 4 archives containing transcriptions, alignments,\n"
38  "and acoustic and LM costs (note: use ark:/dev/null for unwanted outputs)\n"
39  "Usage: nbest-to-linear [options] <nbest-rspecifier> <alignments-wspecifier> "
40  "[<transcriptions-wspecifier> [<lm-cost-wspecifier> [<ac-cost-wspecifier>]]]\n"
41  " e.g.: lattice-to-nbest --n=10 ark:1.lats ark:- | \\\n"
42  " nbest-to-linear ark:1.lats ark,t:1.ali 'ark,t:|int2sym.pl -f 2- words.txt > text'\n";
43 
44  ParseOptions po(usage);
45 
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() < 2 || po.NumArgs() > 5) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  std::string lats_rspecifier = po.GetArg(1),
54  ali_wspecifier = po.GetArg(2),
55  trans_wspecifier = po.GetOptArg(3),
56  lm_cost_wspecifier = po.GetOptArg(4),
57  ac_cost_wspecifier = po.GetOptArg(5);
58 
59  SequentialLatticeReader lattice_reader(lats_rspecifier);
60 
61  Int32VectorWriter ali_writer(ali_wspecifier);
62  Int32VectorWriter trans_writer(trans_wspecifier);
63  BaseFloatWriter lm_cost_writer(lm_cost_wspecifier);
64  BaseFloatWriter ac_cost_writer(ac_cost_wspecifier);
65 
66  int32 n_done = 0, n_err = 0;
67 
68  for (; !lattice_reader.Done(); lattice_reader.Next()) {
69  std::string key = lattice_reader.Key();
70  Lattice lat = lattice_reader.Value();
71 
72  std::vector<int32> ilabels;
73  std::vector<int32> olabels;
74  LatticeWeight weight;
75 
76  if (!GetLinearSymbolSequence(lat, &ilabels, &olabels, &weight)) {
77  KALDI_WARN << "Lattice/nbest for key " << key << " had wrong format: "
78  "note, this program expects input with one path, e.g. from "
79  "lattice-to-nbest.";
80  n_err++;
81  } else {
82  if (ali_wspecifier != "") ali_writer.Write(key, ilabels);
83  if (trans_wspecifier != "") trans_writer.Write(key, olabels);
84  if (lm_cost_wspecifier != "") lm_cost_writer.Write(key, weight.Value1());
85  if (ac_cost_wspecifier!= "") ac_cost_writer.Write(key, weight.Value2());
86  n_done++;
87  }
88  }
89  KALDI_LOG << "Done " << n_done << " n-best entries, "
90  << n_err << " had errors.";
91  return (n_done != 0 ? 0 : 1);
92  } catch(const std::exception &e) {
93  std::cerr << e.what();
94  return -1;
95  }
96 }
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].
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
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
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_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.
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