linear-to-nbest.cc
Go to the documentation of this file.
1 // latbin/linear-to-nbest.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 namespace kaldi {
27 void MakeLatticeFromLinear(const std::vector<int32> &ali,
28  const std::vector<int32> &words,
29  BaseFloat lm_cost,
30  BaseFloat ac_cost,
31  Lattice *lat_out) {
33  typedef LatticeArc::Weight Weight;
34  typedef LatticeArc::Label Label;
35  lat_out->DeleteStates();
36  StateId cur_state = lat_out->AddState(); // will be 0.
37  lat_out->SetStart(cur_state);
38  for (size_t i = 0; i < ali.size() || i < words.size(); i++) {
39  Label ilabel = (i < ali.size() ? ali[i] : 0);
40  Label olabel = (i < words.size() ? words[i] : 0);
41  StateId next_state = lat_out->AddState();
42  lat_out->AddArc(cur_state,
43  LatticeArc(ilabel, olabel, Weight::One(), next_state));
44  cur_state = next_state;
45  }
46  lat_out->SetFinal(cur_state, Weight(lm_cost, ac_cost));
47 }
48 }
49 
50 int main(int argc, char *argv[]) {
51  try {
52  using namespace kaldi;
53  typedef kaldi::int32 int32;
54  typedef kaldi::int64 int64;
55  using fst::SymbolTable;
56  using fst::VectorFst;
57  using fst::StdArc;
58 
59  const char *usage =
60  "This does the opposite of nbest-to-linear. It takes 4 archives,\n"
61  "containing alignments, word-sequences, and acoustic and LM costs,\n"
62  "and turns it into a single archive containing FSTs with a linear\n"
63  "structure. The program is called linear-to-nbest because very often\n"
64  "the archives concerned will represent N-best lists\n"
65  "Usage: linear-to-nbest [options] <alignments-rspecifier> "
66  "<transcriptions-rspecifier> (<lm-cost-rspecifier>|'') (<ac-cost-rspecifier>|'') "
67  "<nbest-wspecifier>\n"
68  "Note: if the rspecifiers for lm-cost or ac-cost are the empty string,\n"
69  "these value will default to zero.\n"
70  " e.g.: linear-to-nbest ark:1.ali 'ark:sym2int.pl -f 2- words.txt text|' "
71  "ark:1.lmscore ark:1.acscore "
72  "ark:1.nbest\n";
73 
74  ParseOptions po(usage);
75 
76  po.Read(argc, argv);
77 
78  if (po.NumArgs() != 5) {
79  po.PrintUsage();
80  exit(1);
81  }
82 
83  std::string ali_rspecifier = po.GetArg(1),
84  trans_rspecifier = po.GetArg(2),
85  lm_cost_rspecifier = po.GetArg(3),
86  ac_cost_rspecifier = po.GetArg(4),
87  lats_wspecifier = po.GetArg(5); // will probably represent N-best.
88 
89 
90 
91  SequentialInt32VectorReader ali_reader(ali_rspecifier);
92  RandomAccessInt32VectorReader trans_reader(trans_rspecifier);
93  RandomAccessBaseFloatReader lm_cost_reader(lm_cost_rspecifier);
94  RandomAccessBaseFloatReader ac_cost_reader(ac_cost_rspecifier);
95 
96  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
97 
98  int32 n_done = 0, n_err = 0;
99 
100  for (; !ali_reader.Done(); ali_reader.Next()) {
101  std::string key = ali_reader.Key();
102  if (!trans_reader.HasKey(key)) {
103  KALDI_ERR << "No transcription for key " << key;
104  n_err++;
105  continue;
106  }
107  if (lm_cost_rspecifier != "" && !lm_cost_reader.HasKey(key)) {
108  KALDI_ERR << "No LM cost for key " << key;
109  n_err++;
110  continue;
111  }
112  if (ac_cost_rspecifier != "" && !ac_cost_reader.HasKey(key)) {
113  KALDI_ERR << "No acoustic cost for key " << key;
114  n_err++;
115  continue;
116  }
117  const std::vector<int32> &ali = ali_reader.Value();
118  const std::vector<int32> &words = trans_reader.Value(key);
119  BaseFloat
120  ac_cost = (ac_cost_rspecifier == "") ? 0.0 : ac_cost_reader.Value(key),
121  lm_cost = (lm_cost_rspecifier == "") ? 0.0 : lm_cost_reader.Value(key);
122  Lattice lat;
123  MakeLatticeFromLinear(ali, words, lm_cost, ac_cost, &lat);
124  CompactLattice clat;
125  ConvertLattice(lat, &clat);
126 
127  compact_lattice_writer.Write(key, clat);
128  n_done++;
129  }
130  KALDI_LOG << "Done " << n_done << " n-best entries ,"
131  << n_err << " had errors.";
132  return (n_done != 0 ? 0 : 1);
133  } catch(const std::exception &e) {
134  std::cerr << e.what();
135  return -1;
136  }
137 }
int32 words[kMaxOrder]
fst::StdArc::StateId StateId
fst::StdArc::Label Label
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
fst::ArcTpl< LatticeWeight > LatticeArc
Definition: kaldi-lattice.h:40
Lattice::StateId StateId
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
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
void MakeLatticeFromLinear(const std::vector< int32 > &ali, const std::vector< int32 > &words, BaseFloat lm_cost, BaseFloat ac_cost, Lattice *lat_out)
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
const T & Value(const std::string &key)
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
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_ERR
Definition: kaldi-error.h:147
int main(int argc, char *argv[])
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
fst::StdArc::Label Label
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
fst::StdArc::Weight Weight
int NumArgs() const
Number of positional parameters (c.f. argc-1).
Arc::Weight Weight
Definition: kws-search.cc:31
#define KALDI_LOG
Definition: kaldi-error.h:153