lattice-incremental-online-decoder.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // decoder/lattice-incremental-online-decoder.h
  2. // Copyright 2019 Zhehuai Chen
  3. //
  4. // See ../../COPYING for clarification regarding multiple authors
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  14. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  15. // MERCHANTABLITY OR NON-INFRINGEMENT.
  16. // See the Apache 2 License for the specific language governing permissions and
  17. // limitations under the License.
  18. // see note at the top of lattice-faster-decoder.h, about how to maintain this
  19. // file in sync with lattice-faster-decoder.h
  20. #ifndef KALDI_DECODER_LATTICE_INCREMENTAL_ONLINE_DECODER_H_
  21. #define KALDI_DECODER_LATTICE_INCREMENTAL_ONLINE_DECODER_H_
  22. #include "util/stl-utils.h"
  23. #include "util/hash-list.h"
  24. #include "fst/fstlib.h"
  25. #include "itf/decodable-itf.h"
  26. #include "fstext/fstext-lib.h"
  27. #include "lat/determinize-lattice-pruned.h"
  28. #include "lat/kaldi-lattice.h"
  29. #include "decoder/lattice-incremental-decoder.h"
  30. namespace kaldi {
  31. /** LatticeIncrementalOnlineDecoderTpl is as LatticeIncrementalDecoderTpl but also
  32. supports an efficient way to get the best path (see the function
  33. BestPathEnd()), which is useful in endpointing and in situations where you
  34. might want to frequently access the best path.
  35. This is only templated on the FST type, since the Token type is required to
  36. be BackpointerToken. Actually it only makes sense to instantiate
  37. LatticeIncrementalDecoderTpl with Token == BackpointerToken if you do so indirectly via
  38. this child class.
  39. */
  40. template <typename FST>
  41. class LatticeIncrementalOnlineDecoderTpl:
  42. public LatticeIncrementalDecoderTpl<FST, decoder::BackpointerToken> {
  43. public:
  44. using Arc = typename FST::Arc;
  45. using Label = typename Arc::Label;
  46. using StateId = typename Arc::StateId;
  47. using Weight = typename Arc::Weight;
  48. using Token = decoder::BackpointerToken;
  49. using ForwardLinkT = decoder::ForwardLink<Token>;
  50. // Instantiate this class once for each thing you have to decode.
  51. // This version of the constructor does not take ownership of
  52. // 'fst'.
  53. LatticeIncrementalOnlineDecoderTpl(const FST &fst,
  54. const TransitionInformation &trans_model,
  55. const LatticeIncrementalDecoderConfig &config):
  56. LatticeIncrementalDecoderTpl<FST, Token>(fst, trans_model, config) { }
  57. // This version of the initializer takes ownership of 'fst', and will delete
  58. // it when this object is destroyed.
  59. LatticeIncrementalOnlineDecoderTpl(const LatticeIncrementalDecoderConfig &config,
  60. FST *fst,
  61. const TransitionInformation &trans_model):
  62. LatticeIncrementalDecoderTpl<FST, Token>(config, fst, trans_model) { }
  63. struct BestPathIterator {
  64. void *tok;
  65. int32 frame;
  66. // note, "frame" is the frame-index of the frame you'll get the
  67. // transition-id for next time, if you call TraceBackBestPath on this
  68. // iterator (assuming it's not an epsilon transition). Note that this
  69. // is one less than you might reasonably expect, e.g. it's -1 for
  70. // the nonemitting transitions before the first frame.
  71. BestPathIterator(void *t, int32 f): tok(t), frame(f) { }
  72. bool Done() { return tok == NULL; }
  73. };
  74. /// Outputs an FST corresponding to the single best path through the lattice.
  75. /// This is quite efficient because it doesn't get the entire raw lattice and find
  76. /// the best path through it; instead, it uses the BestPathEnd and BestPathIterator
  77. /// so it basically traces it back through the lattice.
  78. /// Returns true if result is nonempty (using the return status is deprecated,
  79. /// it will become void). If "use_final_probs" is true AND we reached the
  80. /// final-state of the graph then it will include those as final-probs, else
  81. /// it will treat all final-probs as one.
  82. bool GetBestPath(Lattice *ofst,
  83. bool use_final_probs = true) const;
  84. /// This function returns an iterator that can be used to trace back
  85. /// the best path. If use_final_probs == true and at least one final state
  86. /// survived till the end, it will use the final-probs in working out the best
  87. /// final Token, and will output the final cost to *final_cost (if non-NULL),
  88. /// else it will use only the forward likelihood, and will put zero in
  89. /// *final_cost (if non-NULL).
  90. /// Requires that NumFramesDecoded() > 0.
  91. BestPathIterator BestPathEnd(bool use_final_probs,
  92. BaseFloat *final_cost = NULL) const;
  93. /// This function can be used in conjunction with BestPathEnd() to trace back
  94. /// the best path one link at a time (e.g. this can be useful in endpoint
  95. /// detection). By "link" we mean a link in the graph; not all links cross
  96. /// frame boundaries, but each time you see a nonzero ilabel you can interpret
  97. /// that as a frame. The return value is the updated iterator. It outputs
  98. /// the ilabel and olabel, and the (graph and acoustic) weight to the "arc" pointer,
  99. /// while leaving its "nextstate" variable unchanged.
  100. BestPathIterator TraceBackBestPath(
  101. BestPathIterator iter, LatticeArc *arc) const;
  102. KALDI_DISALLOW_COPY_AND_ASSIGN(LatticeIncrementalOnlineDecoderTpl);
  103. };
  104. typedef LatticeIncrementalOnlineDecoderTpl<fst::StdFst> LatticeIncrementalOnlineDecoder;
  105. } // end namespace kaldi.
  106. #endif