You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
6.0 KiB

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