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.

156 lines
4.6 KiB

  1. // lat/kaldi-lattice.h
  2. // Copyright 2009-2011 Microsoft Corporation
  3. // See ../../COPYING for clarification regarding multiple authors
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  13. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  14. // MERCHANTABLITY OR NON-INFRINGEMENT.
  15. // See the Apache 2 License for the specific language governing permissions and
  16. // limitations under the License.
  17. #ifndef KALDI_LAT_KALDI_LATTICE_H_
  18. #define KALDI_LAT_KALDI_LATTICE_H_
  19. #include "base/kaldi-common.h"
  20. #include "fstext/fstext-lib.h"
  21. // #include "util/common-utils.h"
  22. namespace kaldi {
  23. // will import some things above...
  24. typedef fst::LatticeWeightTpl<BaseFloat> LatticeWeight;
  25. // careful: kaldi::int32 is not always the same C type as fst::int32
  26. typedef fst::CompactLatticeWeightTpl<LatticeWeight, int32> CompactLatticeWeight;
  27. typedef fst::CompactLatticeWeightCommonDivisorTpl<LatticeWeight, int32>
  28. CompactLatticeWeightCommonDivisor;
  29. typedef fst::ArcTpl<LatticeWeight> LatticeArc;
  30. typedef fst::ArcTpl<CompactLatticeWeight> CompactLatticeArc;
  31. typedef fst::VectorFst<LatticeArc> Lattice;
  32. typedef fst::VectorFst<CompactLatticeArc> CompactLattice;
  33. // The following functions for writing and reading lattices in binary or text
  34. // form are provided here in case you need to include lattices in larger,
  35. // Kaldi-type objects with their own Read and Write functions. Caution: these
  36. // functions return false on stream failure rather than throwing an exception as
  37. // most similar Kaldi functions would do.
  38. bool WriteCompactLattice(std::ostream& os, bool binary,
  39. const CompactLattice& clat);
  40. bool WriteLattice(std::ostream& os, bool binary, const Lattice& lat);
  41. // the following function requires that *clat be
  42. // NULL when called.
  43. bool ReadCompactLattice(std::istream& is, bool binary, CompactLattice** clat);
  44. // the following function requires that *lat be
  45. // NULL when called.
  46. bool ReadLattice(std::istream& is, bool binary, Lattice** lat);
  47. class CompactLatticeHolder {
  48. public:
  49. typedef CompactLattice T;
  50. CompactLatticeHolder() { t_ = NULL; }
  51. static bool Write(std::ostream& os, bool binary, const T& t) {
  52. // Note: we don't include the binary-mode header when writing
  53. // this object to disk; this ensures that if we write to single
  54. // files, the result can be read by OpenFst.
  55. return WriteCompactLattice(os, binary, t);
  56. }
  57. bool Read(std::istream& is);
  58. static bool IsReadInBinary() { return true; }
  59. T& Value() {
  60. KALDI_ASSERT(t_ != NULL && "Called Value() on empty CompactLatticeHolder");
  61. return *t_;
  62. }
  63. void Clear() {
  64. delete t_;
  65. t_ = NULL;
  66. }
  67. void Swap(CompactLatticeHolder* other) { std::swap(t_, other->t_); }
  68. bool ExtractRange(const CompactLatticeHolder& other,
  69. const std::string& range) {
  70. KALDI_ERR << "ExtractRange is not defined for this type of holder.";
  71. return false;
  72. }
  73. ~CompactLatticeHolder() { Clear(); }
  74. private:
  75. T* t_;
  76. };
  77. class LatticeHolder {
  78. public:
  79. typedef Lattice T;
  80. LatticeHolder() { t_ = NULL; }
  81. static bool Write(std::ostream& os, bool binary, const T& t) {
  82. // Note: we don't include the binary-mode header when writing
  83. // this object to disk; this ensures that if we write to single
  84. // files, the result can be read by OpenFst.
  85. return WriteLattice(os, binary, t);
  86. }
  87. bool Read(std::istream& is);
  88. static bool IsReadInBinary() { return true; }
  89. T& Value() {
  90. KALDI_ASSERT(t_ != NULL && "Called Value() on empty LatticeHolder");
  91. return *t_;
  92. }
  93. void Clear() {
  94. delete t_;
  95. t_ = NULL;
  96. }
  97. void Swap(LatticeHolder* other) { std::swap(t_, other->t_); }
  98. bool ExtractRange(const LatticeHolder& other, const std::string& range) {
  99. KALDI_ERR << "ExtractRange is not defined for this type of holder.";
  100. return false;
  101. }
  102. ~LatticeHolder() { Clear(); }
  103. private:
  104. T* t_;
  105. };
  106. // typedef TableWriter<LatticeHolder> LatticeWriter;
  107. // typedef SequentialTableReader<LatticeHolder> SequentialLatticeReader;
  108. // typedef RandomAccessTableReader<LatticeHolder> RandomAccessLatticeReader;
  109. //
  110. // typedef TableWriter<CompactLatticeHolder> CompactLatticeWriter;
  111. // typedef SequentialTableReader<CompactLatticeHolder>
  112. // SequentialCompactLatticeReader; typedef
  113. // RandomAccessTableReader<CompactLatticeHolder>
  114. // RandomAccessCompactLatticeReader;
  115. } // namespace kaldi
  116. #endif // KALDI_LAT_KALDI_LATTICE_H_