nbest-to-lattice.cc
Go to the documentation of this file.
1 // latbin/nbest-to-lattice.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 
27 bool GetUtteranceId(const std::string &nbest_id, std::string *utterance_id) {
28  size_t pos = nbest_id.find_last_of('-');
29  if (pos == std::string::npos || pos == 0) return false;
30  else{
31  *utterance_id = std::string(nbest_id, 0, pos);
32  return true;
33  }
34 }
35 
36 int main(int argc, char *argv[]) {
37  try {
38  using namespace kaldi;
39  typedef kaldi::int32 int32;
40 
41  const char *usage =
42  "Read in a Table containing N-best entries from a lattices (i.e. individual\n"
43  "lattices with a linear structure, one for each N-best entry, indexed by\n"
44  "utt_id_a-1, utt_id_a-2, etc., and take the union of them for each utterance\n"
45  "id (e.g. utt_id_a), outputting a lattice for each.\n"
46  "Usage: nbest-to-lattice <nbest-rspecifier> <lattices-wspecifier>\n"
47  " e.g.: nbest-to-lattice ark:1.nbest ark:1.lats\n";
48 
49  ParseOptions po(usage);
50 
51  po.Read(argc, argv);
52 
53  if (po.NumArgs() != 2) {
54  po.PrintUsage();
55  exit(1);
56  }
57 
58  std::string nbest_rspecifier = po.GetArg(1),
59  lats_wspecifier = po.GetArg(2);
60 
61 
62  SequentialCompactLatticeReader compact_nbest_reader(nbest_rspecifier);
63  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
64 
65  int32 n_nbest_done = 0, n_utt_done = 0;
66 
67  // The variable "cur_union" will be the union of FSTs for a
68  // particular utterance-id, if we have any "pending".
69  CompactLattice cur_union;
70  std::string cur_utt_id;
71 
72  for (; !compact_nbest_reader.Done(); compact_nbest_reader.Next()) {
73  std::string nbest_id = compact_nbest_reader.Key();
74  const CompactLattice &this_nbest = compact_nbest_reader.Value();
75  std::string utt_id;
76  if (!GetUtteranceId(nbest_id, &utt_id)) {
77  KALDI_ERR << "Invalid n-best id " << nbest_id << ": make sure you "
78  "are giving N-bests to nbest-to-lattice.";
79  }
80  if (utt_id != cur_utt_id) { // change in utterance.
81  if (cur_utt_id != "") {
82  compact_lattice_writer.Write(cur_utt_id, cur_union);
83  cur_union.DeleteStates();
84  }
85  n_utt_done++; // We increment this when we start processing a
86  // new utterance.
87  cur_utt_id = utt_id;
88  }
89  Union(&cur_union, this_nbest);
90  n_nbest_done++;
91  }
92 
93  if (cur_utt_id != "")
94  compact_lattice_writer.Write(cur_utt_id, cur_union);
95 
96  KALDI_LOG << "Done joining n-best into lattices for "
97  << n_utt_done << " utterances, with on average "
98  << (n_nbest_done/static_cast<BaseFloat>(n_utt_done))
99  << " N-best paths per utterance.";
100  return (n_utt_done != 0 ? 0 : 1);
101  } catch(const std::exception &e) {
102  std::cerr << e.what();
103  return -1;
104  }
105 }
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].
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
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.
#define KALDI_ERR
Definition: kaldi-error.h:147
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).
int main(int argc, char *argv[])
bool GetUtteranceId(const std::string &nbest_id, std::string *utterance_id)
#define KALDI_LOG
Definition: kaldi-error.h:153