nbest-to-lattice.cc File Reference
Include dependency graph for nbest-to-lattice.cc:

Go to the source code of this file.

Functions

bool GetUtteranceId (const std::string &nbest_id, std::string *utterance_id)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ GetUtteranceId()

bool GetUtteranceId ( const std::string &  nbest_id,
std::string *  utterance_id 
)

Definition at line 27 of file nbest-to-lattice.cc.

Referenced by main().

27  {
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 }

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 36 of file nbest-to-lattice.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), GetUtteranceId(), KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

36  {
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
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
#define KALDI_ERR
Definition: kaldi-error.h:147
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
bool GetUtteranceId(const std::string &nbest_id, std::string *utterance_id)
#define KALDI_LOG
Definition: kaldi-error.h:153