lattice-difference.cc
Go to the documentation of this file.
1 // latbin/lattice-difference.cc
2 
3 // Copyright 2009-2011 Chao Weng
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  using fst::SymbolTable;
31  using fst::VectorFst;
32  using fst::StdArc;
33 
34  const char *usage =
35  "Compute FST difference on lattices (remove sequences in first lattice\n"
36  " that appear in second lattice)\n"
37  "Useful for the denominator lattice for MCE.\n"
38  "Usage: lattice-difference [options] "
39  "lattice1-rspecifier lattice2-rspecifier lattice-wspecifier\n"
40  " e.g.: lattice-difference ark:den.lats ark:num.lats ark:den_mce.lats\n";
41 
42  ParseOptions po(usage);
43  po.Read(argc, argv);
44 
45  if (po.NumArgs() != 3) {
46  po.PrintUsage();
47  exit(1);
48  }
49 
50  std::string lats1_rspecifier = po.GetArg(1);
51  std::string lats2_rspecifier = po.GetArg(2);
52  std::string lats_wspecifier = po.GetArg(3);
53 
54  SequentialCompactLatticeReader compact_lattice_reader1(lats1_rspecifier);
55  RandomAccessCompactLatticeReader compact_lattice_reader2(lats2_rspecifier);
56 
57  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
58 
59  int32 n_done = 0, n_no_lat = 0, n_only_transcription = 0;
60 
61  for (; !compact_lattice_reader1.Done(); compact_lattice_reader1.Next()) {
62  std::string key = compact_lattice_reader1.Key();
63  const CompactLattice &clat1 = compact_lattice_reader1.Value();
64  if (compact_lattice_reader2.HasKey(key)) {
65  CompactLattice clat2 (compact_lattice_reader2.Value(key));
66  // "Difference" requires clat2 to be unweighted, deterministic and epsilon-free.
67  // So we remove the weights, remove epsilons and determinize.
68  RemoveWeights(&clat2);
69  RmEpsilon(&clat2);
70  { CompactLattice clat_tmp(clat2); Determinize(clat_tmp, &clat2); }
71 
72  CompactLattice clat_out;
73  Difference(clat1, clat2, &clat_out);
74  if (clat_out.Start() == 0) {
75  compact_lattice_writer.Write(key, clat_out);
76  n_done++;
77  } else {
78  // In this case, the lattice only contains the transcription
79  KALDI_WARN << "Skipping utterance " << key
80  << " because difference is empty.";
81  n_only_transcription++;
82  }
83  } else {
84  KALDI_WARN << "No lattice found for utterance " << key << " in "
85  << lats2_rspecifier;
86  n_no_lat++;
87  }
88  }
89 
90  KALDI_LOG << "Total " << n_done << " lattices written; "
91  << n_only_transcription
92  << " lattices had empty difference; "
93  << n_no_lat << " missing lattices in second archive ";
94  return (n_done != 0 ? 0 : 1);
95  } catch(const std::exception &e) {
96  std::cerr << e.what();
97  return -1;
98  }
99 }
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].
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
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)
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_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).
void RemoveWeights(MutableFst< Arc > *ifst)
#define KALDI_LOG
Definition: kaldi-error.h:153
int main(int argc, char *argv[])