lattice-interp.cc
Go to the documentation of this file.
1 // latbin/lattice-interp.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation; Saarland University
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 two archives of lattices (indexed by utterances) and combines\n"
37  "the individual lattice pairs (one from each archive). Keeps the alignments\n"
38  "from the first lattice. Equivalent to\n"
39  "projecting the second archive on words (lattice-project), then composing\n"
40  "the pairs of lattices (lattice-compose), then scaling graph and acoustic\n"
41  "costs by 0.5 (lattice-scale). You can control the individual scales with\n"
42  "--alpha, which is the scale of the first lattices (the second is 1-alpha).\n"
43  "Usage: lattice-interp [options] lattice-rspecifier-a lattice-rspecifier-b"
44  " lattice-wspecifier\n"
45  " e.g.: lattice-compose ark:1.lats ark:2.lats ark:composed.lats\n";
46 
47  ParseOptions po(usage);
48  BaseFloat alpha = 0.5; // Scale of 1st in the pair.
49 
50  po.Register("alpha", &alpha, "Scale of the first lattice in the pair (should be in range [0, 1])");
51  po.Read(argc, argv);
52 
53  if (po.NumArgs() != 3) {
54  po.PrintUsage();
55  exit(1);
56  }
57 
58  std::string lats_rspecifier1 = po.GetArg(1),
59  lats_rspecifier2 = po.GetArg(2),
60  lats_wspecifier = po.GetArg(3);
61 
62  SequentialLatticeReader lattice_reader1(lats_rspecifier1);
63  RandomAccessCompactLatticeReader lattice_reader2(lats_rspecifier2);
64 
65  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
66 
67  int32 n_processed = 0, n_empty = 0, n_success = 0, n_no_2ndlat=0;
68 
69  for (; !lattice_reader1.Done(); lattice_reader1.Next()) {
70  std::string key = lattice_reader1.Key();
71  Lattice lat1 = lattice_reader1.Value();
72  lattice_reader1.FreeCurrent();
73  ScaleLattice(fst::LatticeScale(alpha, alpha), &lat1);
74  ArcSort(&lat1, fst::OLabelCompare<LatticeArc>());
75 
76  if (lattice_reader2.HasKey(key)) {
77  n_processed++;
78  CompactLattice clat2 = lattice_reader2.Value(key);
80 
81  Lattice lat2;
82  ConvertLattice(clat2, &lat2);
83  fst::Project(&lat2, fst::PROJECT_OUTPUT); // project on words.
84  ScaleLattice(fst::LatticeScale(1.0-alpha, 1.0-alpha), &lat2);
85  ArcSort(&lat2, fst::ILabelCompare<LatticeArc>());
86 
87  Lattice lat3;
88  Compose(lat1, lat2, &lat3);
89  if (lat3.Start() == fst::kNoStateId) { // empty composition.
90  KALDI_WARN << "For utterance " << key << ", composed result is empty.";
91  n_empty++;
92  } else {
93  n_success++;
94  CompactLattice clat3;
95  ConvertLattice(lat3, &clat3);
96  compact_lattice_writer.Write(key, clat3);
97  }
98  } else {
99  KALDI_WARN << "No lattice found for utterance " << key << " in "
100  << lats_rspecifier2 << ". Not producing output";
101  n_no_2ndlat++;
102  }
103  }
104  KALDI_LOG << "Done " << n_processed << " lattices; "
105  << n_success << " had nonempty result, " << n_empty
106  << " had empty composition; in " << n_no_2ndlat
107  << ", had empty second lattice.";
108  return (n_success != 0 ? 0 : 1);
109  } catch(const std::exception &e) {
110  std::cerr << e.what();
111  return -1;
112  }
113 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void RemoveAlignmentsFromCompactLattice(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *fst)
Removes state-level alignments (the strings that are part of the weights).
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
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
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 ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
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
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
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.
bool HasKey(const std::string &key)
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