lattice-functions.h
Go to the documentation of this file.
1 // lat/lattice-functions.h
2 
3 // Copyright 2009-2012 Saarland University (author: Arnab Ghoshal)
4 // 2012-2013 Johns Hopkins University (Author: Daniel Povey);
5 // Bagher BabaAli
6 // 2014 Guoguo Chen
7 
8 // See ../../COPYING for clarification regarding multiple authors
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
18 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
19 // MERCHANTABLITY OR NON-INFRINGEMENT.
20 // See the Apache 2 License for the specific language governing permissions and
21 // limitations under the License.
22 
23 
24 #ifndef KALDI_LAT_LATTICE_FUNCTIONS_H_
25 #define KALDI_LAT_LATTICE_FUNCTIONS_H_
26 
27 #include <vector>
28 #include <map>
29 
30 #include "base/kaldi-common.h"
31 #include "hmm/posterior.h"
32 #include "fstext/fstext-lib.h"
33 #include "hmm/transition-model.h"
34 #include "lat/kaldi-lattice.h"
35 #include "itf/decodable-itf.h"
36 
37 namespace kaldi {
38 
54 void GetPerFrameAcousticCosts(const Lattice &nbest,
55  Vector<BaseFloat> *per_frame_loglikes);
56 
62 int32 LatticeStateTimes(const Lattice &lat, std::vector<int32> *times);
63 
69  std::vector<int32> *times);
70 
81  Posterior *arc_post,
82  double *acoustic_like_sum = NULL);
83 
84 // This function is something similar to LatticeForwardBackward(), but it is on
85 // the CompactLattice lattice format. Also we only need the alpha in the forward
86 // path, not the posteriors.
88  std::vector<double> *alpha);
89 
90 // A sibling of the function CompactLatticeAlphas()... We compute the beta from
91 // the backward path here.
93  std::vector<double> *beta);
94 
95 
96 // Computes (normal or Viterbi) alphas and betas; returns (total-prob, or
97 // best-path negated cost) Note: in either case, the alphas and betas are
98 // negated costs. Requires that lat be topologically sorted. This code
99 // will work for either CompactLattice or Latice.
100 template<typename LatticeType>
101 double ComputeLatticeAlphasAndBetas(const LatticeType &lat,
102  bool viterbi,
103  std::vector<double> *alpha,
104  std::vector<double> *beta);
105 
106 
110 
111 
114 void TopSortLatticeIfNeeded(Lattice *clat);
115 
120  int32 *num_frames = NULL);
121 
125  std::vector<int32> *depth_per_frame);
126 
127 
132 void CompactLatticeLimitDepth(int32 max_arcs_per_frame,
133  CompactLattice *clat);
134 
135 
140 void LatticeActivePhones(const Lattice &lat, const TransitionModel &trans,
141  const std::vector<int32> &sil_phones,
142  std::vector<std::set<int32> > *active_phones);
143 
154 void ConvertLatticeToPhones(const TransitionModel &trans_model,
155  Lattice *lat);
156 
159 template<class LatticeType>
160 bool PruneLattice(BaseFloat beam, LatticeType *lat);
161 
162 
167 void ConvertCompactLatticeToPhones(const TransitionModel &trans_model,
168  CompactLattice *clat);
169 
182 bool LatticeBoost(const TransitionModel &trans,
183  const std::vector<int32> &alignment,
184  const std::vector<int32> &silence_phones,
185  BaseFloat b,
186  BaseFloat max_silence_error,
187  Lattice *lat);
188 
189 
229  const TransitionModel &trans,
230  const std::vector<int32> &silence_phones,
231  const Lattice &lat,
232  const std::vector<int32> &num_ali,
233  std::string criterion,
234  bool one_silence_class,
235  Posterior *post);
236 
264  const TransitionModel &trans,
265  const Lattice &lat,
266  const std::vector<int32> &num_ali,
267  bool drop_frames,
268  bool convert_to_pdf_ids,
269  bool cancel,
270  Posterior *arc_post);
271 
272 
285  std::vector<int32> *words,
286  std::vector<int32> *begin_times,
287  std::vector<int32> *lengths);
288 
301  const TransitionModel &tmodel,
302  const CompactLattice &clat,
303  std::vector<int32> *words,
304  std::vector<int32> *begin_times,
305  std::vector<int32> *lengths,
306  std::vector<std::vector<int32> > *prons,
307  std::vector<std::vector<int32> > *phone_lengths);
308 
309 
313  CompactLattice *shortest_path);
314 
317 void AddWordInsPenToCompactLattice(BaseFloat word_ins_penalty,
318  CompactLattice *clat);
319 
324 bool RescoreCompactLattice(DecodableInterface *decodable,
325  CompactLattice *clat);
326 
327 
332 
338 
339 
353  const TransitionModel &tmodel,
354  BaseFloat speedup_factor,
355  DecodableInterface *decodable,
356  CompactLattice *clat);
357 
358 
365 bool RescoreLattice(DecodableInterface *decodable,
366  Lattice *lat);
367 
376  const CompactLattice& clat,
378  CompactLattice* composed_clat);
379 
405  const Lattice &lat,
406  unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
407  PairHasher<int32> > *acoustic_scores);
408 
420  const unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
421  PairHasher<int32> > &acoustic_scores,
422  Lattice *lat);
423 
424 } // namespace kaldi
425 
426 #endif // KALDI_LAT_LATTICE_FUNCTIONS_H_
int32 words[kMaxOrder]
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 LatticeStateTimes(const Lattice &lat, vector< int32 > *times)
This function iterates over the states of a topologically sorted lattice and counts the time instance...
double ComputeLatticeAlphasAndBetas(const LatticeType &lat, bool viterbi, vector< double > *alpha, vector< double > *beta)
void ReplaceAcousticScoresFromMap(const unordered_map< std::pair< int32, int32 >, std::pair< BaseFloat, int32 >, PairHasher< int32 > > &acoustic_scores, Lattice *lat)
This function restores acoustic scores computed using the function ComputeAcousticScoresMap into the ...
void TopSortLatticeIfNeeded(Lattice *lat)
Topologically sort the lattice if not already topologically sorted.
kaldi::int32 int32
void GetPerFrameAcousticCosts(const Lattice &nbest, Vector< BaseFloat > *per_frame_loglikes)
This function extracts the per-frame log likelihoods from a linear lattice (which we refer to as an &#39;...
bool RescoreCompactLatticeSpeedup(const TransitionModel &tmodel, BaseFloat speedup_factor, DecodableInterface *decodable, CompactLattice *clat)
This function is like RescoreCompactLattice, but it is modified to avoid computing probabilities on m...
bool RescoreCompactLattice(DecodableInterface *decodable, CompactLattice *clat)
This function *adds* the negated scores obtained from the Decodable object, to the acoustic scores on...
void ConvertCompactLatticeToPhones(const TransitionModel &trans, CompactLattice *clat)
Given a lattice, and a transition model to map pdf-ids to phones, replace the sequences of transition...
void LatticeActivePhones(const Lattice &lat, const TransitionModel &trans, const vector< int32 > &silence_phones, vector< std::set< int32 > > *active_phones)
Given a lattice, and a transition model to map pdf-ids to phones, outputs for each frame the set of p...
void CompactLatticeShortestPath(const CompactLattice &clat, CompactLattice *shortest_path)
A form of the shortest-path/best-path algorithm that&#39;s specially coded for CompactLattice.
float BaseFloat
Definition: kaldi-types.h:29
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:42
BaseFloat LatticeForwardBackwardMmi(const TransitionModel &tmodel, const Lattice &lat, const std::vector< int32 > &num_ali, bool drop_frames, bool convert_to_pdf_ids, bool cancel, Posterior *post)
This function can be used to compute posteriors for MMI, with a positive contribution for the numerat...
BaseFloat LatticeForwardBackward(const Lattice &lat, Posterior *post, double *acoustic_like_sum)
This function does the forward-backward over lattices and computes the posterior probabilities of the...
BaseFloat CompactLatticeDepth(const CompactLattice &clat, int32 *num_frames)
Returns the depth of the lattice, defined as the average number of arcs crossing any given frame...
bool ComputeCompactLatticeBetas(const CompactLattice &clat, vector< double > *beta)
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
int32 CompactLatticeStateTimes(const CompactLattice &lat, vector< int32 > *times)
As LatticeStateTimes, but in the CompactLattice format.
BaseFloat LatticeForwardBackwardMpeVariants(const TransitionModel &trans, const std::vector< int32 > &silence_phones, const Lattice &lat, const std::vector< int32 > &num_ali, std::string criterion, bool one_silence_class, Posterior *post)
This function implements either the MPFE (minimum phone frame error) or SMBR (state-level minimum bay...
void AddWordInsPenToCompactLattice(BaseFloat word_ins_penalty, CompactLattice *clat)
This function add the word insertion penalty to graph score of each word in the compact lattice...
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
void ComputeAcousticScoresMap(const Lattice &lat, unordered_map< std::pair< int32, int32 >, std::pair< BaseFloat, int32 >, PairHasher< int32 > > *acoustic_scores)
This function computes the mapping from the pair (frame-index, transition-id) to the pair (sum-of-aco...
bool PruneLattice(BaseFloat beam, LatType *lat)
void CompactLatticeLimitDepth(int32 max_depth_per_frame, CompactLattice *clat)
This function limits the depth of the lattice, per frame: that means, it does not allow more than a s...
bool LatticeBoost(const TransitionModel &trans, const std::vector< int32 > &alignment, const std::vector< int32 > &silence_phones, BaseFloat b, BaseFloat max_silence_error, Lattice *lat)
Boosts LM probabilities by b * [number of frame errors]; equivalently, adds -b*[number of frame error...
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
void CompactLatticeDepthPerFrame(const CompactLattice &clat, std::vector< int32 > *depth_per_frame)
This function returns, for each frame, the number of arcs crossing that frame.
void ConvertLatticeToPhones(const TransitionModel &trans, Lattice *lat)
Given a lattice, and a transition model to map pdf-ids to phones, replace the output symbols (presuma...
bool ComputeCompactLatticeAlphas(const CompactLattice &clat, vector< double > *alpha)
bool RescoreLattice(DecodableInterface *decodable, Lattice *lat)
This function *adds* the negated scores obtained from the Decodable object, to the acoustic scores on...
int32 LongestSentenceLength(const Lattice &lat)
This function returns the number of words in the longest sentence in a CompactLattice (i...
bool CompactLatticeToWordAlignment(const CompactLattice &clat, std::vector< int32 > *words, std::vector< int32 > *begin_times, std::vector< int32 > *lengths)
This function takes a CompactLattice that should only contain a single linear sequence (e...
bool CompactLatticeToWordProns(const TransitionModel &tmodel, const CompactLattice &clat, std::vector< int32 > *words, std::vector< int32 > *begin_times, std::vector< int32 > *lengths, std::vector< std::vector< int32 > > *prons, std::vector< std::vector< int32 > > *phone_lengths)
This function takes a CompactLattice that should only contain a single linear sequence (e...