lattice-lmrescore-const-arpa.cc File Reference
Include dependency graph for lattice-lmrescore-const-arpa.cc:

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 28 of file lattice-lmrescore-const-arpa.cc.

References kaldi::ComposeCompactLatticeDeterministic(), fst::ConvertLattice(), fst::DeterminizeLattice(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), fst::GraphLatticeScale(), KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), fst::ScaleLattice(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

28  {
29  try {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32  typedef kaldi::int64 int64;
33 
34  const char *usage =
35  "Rescores lattice with the ConstArpaLm format language model. The LM\n"
36  "will be wrapped into the DeterministicOnDemandFst interface and the\n"
37  "rescoring is done by composing with the wrapped LM using a special\n"
38  "type of composition algorithm. Determinization will be applied on\n"
39  "the composed lattice.\n"
40  "\n"
41  "Usage: lattice-lmrescore-const-arpa [options] lattice-rspecifier \\\n"
42  " const-arpa-in lattice-wspecifier\n"
43  " e.g.: lattice-lmrescore-const-arpa --lm-scale=-1.0 ark:in.lats \\\n"
44  " const_arpa ark:out.lats\n";
45 
46  ParseOptions po(usage);
47  BaseFloat lm_scale = 1.0;
48 
49  po.Register("lm-scale", &lm_scale, "Scaling factor for language model "
50  "costs; frequently 1.0 or -1.0");
51 
52  po.Read(argc, argv);
53 
54  if (po.NumArgs() != 3) {
55  po.PrintUsage();
56  exit(1);
57  }
58 
59  std::string lats_rspecifier = po.GetArg(1),
60  lm_rxfilename = po.GetArg(2),
61  lats_wspecifier = po.GetArg(3);
62 
63  // Reads the language model in ConstArpaLm format.
64  ConstArpaLm const_arpa;
65  ReadKaldiObject(lm_rxfilename, &const_arpa);
66 
67  // Reads and writes as compact lattice.
68  SequentialCompactLatticeReader compact_lattice_reader(lats_rspecifier);
69  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
70 
71  int32 n_done = 0, n_fail = 0;
72  for (; !compact_lattice_reader.Done(); compact_lattice_reader.Next()) {
73  std::string key = compact_lattice_reader.Key();
74  CompactLattice clat = compact_lattice_reader.Value();
75  compact_lattice_reader.FreeCurrent();
76 
77  if (lm_scale != 0.0) {
78  // Before composing with the LM FST, we scale the lattice weights
79  // by the inverse of "lm_scale". We'll later scale by "lm_scale".
80  // We do it this way so we can determinize and it will give the
81  // right effect (taking the "best path" through the LM) regardless
82  // of the sign of lm_scale.
83  fst::ScaleLattice(fst::GraphLatticeScale(1.0/lm_scale), &clat);
84  ArcSort(&clat, fst::OLabelCompare<CompactLatticeArc>());
85 
86  // Wraps the ConstArpaLm format language model into FST. We re-create it
87  // for each lattice to prevent memory usage increasing with time.
88  ConstArpaLmDeterministicFst const_arpa_fst(const_arpa);
89 
90  // Composes lattice with language model.
91  CompactLattice composed_clat;
93  &const_arpa_fst, &composed_clat);
94 
95  // Determinizes the composed lattice.
96  Lattice composed_lat;
97  ConvertLattice(composed_clat, &composed_lat);
98  Invert(&composed_lat);
99  CompactLattice determinized_clat;
100  DeterminizeLattice(composed_lat, &determinized_clat);
101  fst::ScaleLattice(fst::GraphLatticeScale(lm_scale), &determinized_clat);
102  if (determinized_clat.Start() == fst::kNoStateId) {
103  KALDI_WARN << "Empty lattice for utterance " << key
104  << " (incompatible LM?)";
105  n_fail++;
106  } else {
107  compact_lattice_writer.Write(key, determinized_clat);
108  n_done++;
109  }
110  } else {
111  // Zero scale so nothing to do.
112  n_done++;
113  compact_lattice_writer.Write(key, clat);
114  }
115  }
116 
117  KALDI_LOG << "Done " << n_done << " lattices, failed for " << n_fail;
118  return (n_done != 0 ? 0 : 1);
119  } catch(const std::exception &e) {
120  std::cerr << e.what();
121  return -1;
122  }
123 }
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
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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
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
void ComposeCompactLatticeDeterministic(const CompactLattice &clat, fst::DeterministicOnDemandFst< fst::StdArc > *det_fst, CompactLattice *composed_clat)
This function Composes a CompactLattice format lattice with a DeterministicOnDemandFst<fst::StdFst> f...
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
std::vector< std::vector< double > > GraphLatticeScale(double lmwt)
bool DeterminizeLattice(const Fst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< Weight > > *ofst, DeterminizeLatticeOptions opts, bool *debug_ptr)
This function implements the normal version of DeterminizeLattice, in which the output strings are re...
#define KALDI_LOG
Definition: kaldi-error.h:153
This class wraps a ConstArpaLm format language model with the interface defined in DeterministicOnDem...