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.

260 lines
9.2 KiB

  1. // fstext/lattice-utils.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_FSTEXT_LATTICE_UTILS_H_
  18. #define KALDI_FSTEXT_LATTICE_UTILS_H_
  19. #include <vector>
  20. #include "fst/fstlib.h"
  21. #include "fstext/lattice-weight.h"
  22. namespace fst {
  23. // The template ConvertLattice does conversions to and from
  24. // LatticeWeight FSTs and CompactLatticeWeight FSTs, and
  25. // between float and double, and to convert from LatticeWeight
  26. // to TropicalWeight. It's used in the I/O code for lattices,
  27. // and for converting lattices to standard FSTs (e.g. for creating
  28. // decoding graphs from lattices).
  29. /**
  30. Convert lattice from a normal FST to a CompactLattice FST.
  31. This is a bit like converting to the Gallic semiring, except
  32. the semiring behaves in a different way (designed to take
  33. the best path).
  34. Note: the ilabels end up as the symbols on the arcs of the
  35. output acceptor, and the olabels go to the strings. To make
  36. it the other way around (useful for the speech-recognition
  37. application), set invert=true [the default].
  38. */
  39. template <class Weight, class Int>
  40. void ConvertLattice(
  41. const ExpandedFst<ArcTpl<Weight> >& ifst,
  42. MutableFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > >* ofst,
  43. bool invert = true);
  44. /**
  45. Convert lattice CompactLattice format to Lattice. This is a bit
  46. like converting from the Gallic semiring. As for any CompactLattice, "ifst"
  47. must be an acceptor (i.e., ilabels and olabels should be identical). If
  48. invert=false, the labels on "ifst" become the ilabels on "ofst" and the
  49. strings in the weights of "ifst" becomes the olabels. If invert=true
  50. [default], this is reversed (useful for speech recognition lattices; our
  51. standard non-compact format has the words on the output side to match HCLG).
  52. */
  53. template <class Weight, class Int>
  54. void ConvertLattice(
  55. const ExpandedFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > >& ifst,
  56. MutableFst<ArcTpl<Weight> >* ofst, bool invert = true);
  57. /**
  58. Convert between CompactLattices and Lattices of different floating point
  59. types... this works between any pair of weight types for which
  60. ConvertLatticeWeight is defined (c.f. lattice-weight.h), and also includes
  61. conversion from LatticeWeight to TropicalWeight.
  62. */
  63. template <class WeightIn, class WeightOut>
  64. void ConvertLattice(const ExpandedFst<ArcTpl<WeightIn> >& ifst,
  65. MutableFst<ArcTpl<WeightOut> >* ofst);
  66. // Now define some ConvertLattice functions that require two phases of
  67. // conversion (don't bother coding these separately as they will be used rarely.
  68. // Lattice with float to CompactLattice with double.
  69. template <class Int>
  70. void ConvertLattice(
  71. const ExpandedFst<ArcTpl<LatticeWeightTpl<float> > >& ifst,
  72. MutableFst<
  73. ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > >*
  74. ofst) {
  75. VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > >
  76. fst;
  77. ConvertLattice(ifst, &fst);
  78. ConvertLattice(fst, ofst);
  79. }
  80. // Lattice with double to CompactLattice with float.
  81. template <class Int>
  82. void ConvertLattice(
  83. const ExpandedFst<ArcTpl<LatticeWeightTpl<double> > >& ifst,
  84. MutableFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > >*
  85. ofst) {
  86. VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > >
  87. fst;
  88. ConvertLattice(ifst, &fst);
  89. ConvertLattice(fst, ofst);
  90. }
  91. /// Converts CompactLattice with double to Lattice with float.
  92. template <class Int>
  93. void ConvertLattice(
  94. const ExpandedFst<
  95. ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > >& ifst,
  96. MutableFst<ArcTpl<LatticeWeightTpl<float> > >* ofst) {
  97. VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > >
  98. fst;
  99. ConvertLattice(ifst, &fst);
  100. ConvertLattice(fst, ofst);
  101. }
  102. /// Converts CompactLattice with float to Lattice with double.
  103. template <class Int>
  104. void ConvertLattice(
  105. const ExpandedFst<
  106. ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > >& ifst,
  107. MutableFst<ArcTpl<LatticeWeightTpl<double> > >* ofst) {
  108. VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > >
  109. fst;
  110. ConvertLattice(ifst, &fst);
  111. ConvertLattice(fst, ofst);
  112. }
  113. /// Converts TropicalWeight to LatticeWeight (puts all the weight on
  114. /// the first float in the lattice's pair).
  115. template <class Real>
  116. void ConvertFstToLattice(const ExpandedFst<ArcTpl<TropicalWeight> >& ifst,
  117. MutableFst<ArcTpl<LatticeWeightTpl<Real> > >* ofst);
  118. /** Returns a default 2x2 matrix scaling factor for LatticeWeight */
  119. inline std::vector<std::vector<double> > DefaultLatticeScale() {
  120. std::vector<std::vector<double> > ans(2);
  121. ans[0].resize(2, 0.0);
  122. ans[1].resize(2, 0.0);
  123. ans[0][0] = ans[1][1] = 1.0;
  124. return ans;
  125. }
  126. inline std::vector<std::vector<double> > AcousticLatticeScale(double acwt) {
  127. std::vector<std::vector<double> > ans(2);
  128. ans[0].resize(2, 0.0);
  129. ans[1].resize(2, 0.0);
  130. ans[0][0] = 1.0;
  131. ans[1][1] = acwt;
  132. return ans;
  133. }
  134. inline std::vector<std::vector<double> > GraphLatticeScale(double lmwt) {
  135. std::vector<std::vector<double> > ans(2);
  136. ans[0].resize(2, 0.0);
  137. ans[1].resize(2, 0.0);
  138. ans[0][0] = lmwt;
  139. ans[1][1] = 1.0;
  140. return ans;
  141. }
  142. inline std::vector<std::vector<double> > LatticeScale(double lmwt,
  143. double acwt) {
  144. std::vector<std::vector<double> > ans(2);
  145. ans[0].resize(2, 0.0);
  146. ans[1].resize(2, 0.0);
  147. ans[0][0] = lmwt;
  148. ans[1][1] = acwt;
  149. return ans;
  150. }
  151. /** Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by
  152. viewing the pair (a, b) as a 2-vector and pre-multiplying by the 2x2 matrix
  153. in "scale". E.g. typically scale would equal
  154. [ 1 0;
  155. 0 acwt ]
  156. if we want to scale the acoustics by "acwt".
  157. */
  158. template <class Weight, class ScaleFloat>
  159. void ScaleLattice(const std::vector<std::vector<ScaleFloat> >& scale,
  160. MutableFst<ArcTpl<Weight> >* fst);
  161. /// Removes state-level alignments (the strings that are
  162. /// part of the weights).
  163. template <class Weight, class Int>
  164. void RemoveAlignmentsFromCompactLattice(
  165. MutableFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > >* fst);
  166. /// Returns true if lattice has alignments, i.e. it has
  167. /// any nonempty strings inside its weights.
  168. template <class Weight, class Int>
  169. bool CompactLatticeHasAlignment(
  170. const ExpandedFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > >& fst);
  171. /// Class StdToLatticeMapper maps a normal arc (StdArc)
  172. /// to a LatticeArc by putting the StdArc weight as the first
  173. /// element of the LatticeWeight. Useful when doing LM
  174. /// rescoring.
  175. template <class Real>
  176. class StdToLatticeMapper {
  177. typedef LatticeWeightTpl<Real> LatticeWeight;
  178. typedef ArcTpl<LatticeWeight> LatticeArc;
  179. public:
  180. LatticeArc operator()(const StdArc& arc) {
  181. // Note: we have to check whether the arc's weight is zero below,
  182. // and if so return (infinity, infinity) and not (infinity, zero),
  183. // because (infinity, zero) is not a valid LatticeWeight, which should
  184. // either be both finite, or both infinite (i.e. Zero()).
  185. return LatticeArc(
  186. arc.ilabel, arc.olabel,
  187. LatticeWeight(arc.weight.Value(), arc.weight == StdArc::Weight::Zero()
  188. ? arc.weight.Value()
  189. : 0.0),
  190. arc.nextstate);
  191. }
  192. MapFinalAction FinalAction() { return MAP_NO_SUPERFINAL; }
  193. MapSymbolsAction InputSymbolsAction() { return MAP_COPY_SYMBOLS; }
  194. MapSymbolsAction OutputSymbolsAction() { return MAP_COPY_SYMBOLS; }
  195. // I believe all properties are preserved.
  196. uint64 Properties(uint64 props) { return props; }
  197. };
  198. /// Class LatticeToStdMapper maps a LatticeArc to a normal arc (StdArc)
  199. /// by adding the elements of the LatticeArc weight.
  200. template <class Real>
  201. class LatticeToStdMapper {
  202. typedef LatticeWeightTpl<Real> LatticeWeight;
  203. typedef ArcTpl<LatticeWeight> LatticeArc;
  204. public:
  205. StdArc operator()(const LatticeArc& arc) {
  206. return StdArc(arc.ilabel, arc.olabel,
  207. StdArc::Weight(arc.weight.Value1() + arc.weight.Value2()),
  208. arc.nextstate);
  209. }
  210. MapFinalAction FinalAction() { return MAP_NO_SUPERFINAL; }
  211. MapSymbolsAction InputSymbolsAction() { return MAP_COPY_SYMBOLS; }
  212. MapSymbolsAction OutputSymbolsAction() { return MAP_COPY_SYMBOLS; }
  213. // I believe all properties are preserved.
  214. uint64 Properties(uint64 props) { return props; }
  215. };
  216. template <class Weight, class Int>
  217. void PruneCompactLattice(
  218. Weight beam,
  219. MutableFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > >* fst);
  220. } // end namespace fst
  221. #include "fstext/lattice-utils-inl.h"
  222. #endif // KALDI_FSTEXT_LATTICE_UTILS_H_