lattice-utils.h
Go to the documentation of this file.
1 // fstext/lattice-utils.h
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #ifndef KALDI_FSTEXT_LATTICE_UTILS_H_
22 #define KALDI_FSTEXT_LATTICE_UTILS_H_
23 
24 #include "fst/fstlib.h"
25 #include "fstext/lattice-weight.h"
26 #include "fstext/factor.h"
27 
28 namespace fst {
29 
30 // The template ConvertLattice does conversions to and from
31 // LatticeWeight FSTs and CompactLatticeWeight FSTs, and
32 // between float and double, and to convert from LatticeWeight
33 // to TropicalWeight. It's used in the I/O code for lattices,
34 // and for converting lattices to standard FSTs (e.g. for creating
35 // decoding graphs from lattices).
36 
37 
48 template<class Weight, class Int>
49 void ConvertLattice(
50  const ExpandedFst<ArcTpl<Weight> > &ifst,
51  MutableFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > > *ofst,
52  bool invert = true);
53 
63 template<class Weight, class Int>
64 void ConvertLattice(
65  const ExpandedFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > > &ifst,
66  MutableFst<ArcTpl<Weight> > *ofst,
67  bool invert = true);
68 
69 
76 template<class WeightIn, class WeightOut>
77 void ConvertLattice(
78  const ExpandedFst<ArcTpl<WeightIn> > &ifst,
79  MutableFst<ArcTpl<WeightOut> > *ofst);
80 
81 
82 // Now define some ConvertLattice functions that require two phases of conversion (don't
83 // bother coding these separately as they will be used rarely.
84 
85 // Lattice with float to CompactLattice with double.
86 template<class Int>
87 void ConvertLattice(const ExpandedFst<ArcTpl<LatticeWeightTpl<float> > > &ifst,
88  MutableFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > > *ofst) {
89  VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > > fst;
90  ConvertLattice(ifst, &fst);
91  ConvertLattice(fst, ofst);
92 }
93 
94 // Lattice with double to CompactLattice with float.
95 template<class Int>
96 void ConvertLattice(const ExpandedFst<ArcTpl<LatticeWeightTpl<double> > > &ifst,
97  MutableFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > > *ofst) {
98  VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > > fst;
99  ConvertLattice(ifst, &fst);
100  ConvertLattice(fst, ofst);
101 }
102 
104 template<class Int>
105 void ConvertLattice(const ExpandedFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > > &ifst,
106  MutableFst<ArcTpl<LatticeWeightTpl<float> > > *ofst) {
107  VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > > fst;
108  ConvertLattice(ifst, &fst);
109  ConvertLattice(fst, ofst);
110 }
111 
113 template<class Int>
114 void ConvertLattice(const ExpandedFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > > &ifst,
115  MutableFst<ArcTpl<LatticeWeightTpl<double> > > *ofst) {
116  VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > > fst;
117  ConvertLattice(ifst, &fst);
118  ConvertLattice(fst, ofst);
119 }
120 
123 template <class Real>
125  const ExpandedFst<ArcTpl<TropicalWeight> > &ifst,
126  MutableFst<ArcTpl<LatticeWeightTpl<Real> > > *ofst);
127 
128 
130 inline std::vector<std::vector<double> > DefaultLatticeScale() {
131  std::vector<std::vector<double> > ans(2);
132  ans[0].resize(2, 0.0);
133  ans[1].resize(2, 0.0);
134  ans[0][0] = ans[1][1] = 1.0;
135  return ans;
136 }
137 
138 inline std::vector<std::vector<double> > AcousticLatticeScale(double acwt) {
139  std::vector<std::vector<double> > ans(2);
140  ans[0].resize(2, 0.0);
141  ans[1].resize(2, 0.0);
142  ans[0][0] = 1.0;
143  ans[1][1] = acwt;
144  return ans;
145 }
146 
147 inline std::vector<std::vector<double> > GraphLatticeScale(double lmwt) {
148  std::vector<std::vector<double> > ans(2);
149  ans[0].resize(2, 0.0);
150  ans[1].resize(2, 0.0);
151  ans[0][0] = lmwt;
152  ans[1][1] = 1.0;
153  return ans;
154 }
155 
156 inline std::vector<std::vector<double> > LatticeScale(double lmwt, double acwt) {
157  std::vector<std::vector<double> > ans(2);
158  ans[0].resize(2, 0.0);
159  ans[1].resize(2, 0.0);
160  ans[0][0] = lmwt;
161  ans[1][1] = acwt;
162  return ans;
163 }
164 
165 
173 template<class Weight, class ScaleFloat>
174 void ScaleLattice(
175  const std::vector<std::vector<ScaleFloat> > &scale,
176  MutableFst<ArcTpl<Weight> > *fst);
177 
180 template<class Weight, class Int>
182  MutableFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > > *fst);
183 
186 template<class Weight, class Int>
188  const ExpandedFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > > &fst);
189 
190 
195 template<class Real>
198  typedef ArcTpl<LatticeWeight> LatticeArc;
199  public:
200  LatticeArc operator()(const StdArc &arc) {
201  // Note: we have to check whether the arc's weight is zero below,
202  // and if so return (infinity, infinity) and not (infinity, zero),
203  // because (infinity, zero) is not a valid LatticeWeight, which should
204  // either be both finite, or both infinite (i.e. Zero()).
205  return LatticeArc(arc.ilabel, arc.olabel,
206  LatticeWeight(arc.weight.Value(),
207  arc.weight == StdArc::Weight::Zero() ?
208  arc.weight.Value() : 0.0),
209  arc.nextstate);
210  }
211  MapFinalAction FinalAction() { return MAP_NO_SUPERFINAL; }
212 
213  MapSymbolsAction InputSymbolsAction() { return MAP_COPY_SYMBOLS; }
214 
215  MapSymbolsAction OutputSymbolsAction() { return MAP_COPY_SYMBOLS; }
216 
217  // I believe all properties are preserved.
218  uint64 Properties(uint64 props) { return props; }
219 };
220 
221 
224 
225 template<class Real>
228  typedef ArcTpl<LatticeWeight> LatticeArc;
229  public:
230  StdArc operator()(const LatticeArc &arc) {
231  return StdArc(arc.ilabel, arc.olabel,
232  StdArc::Weight(arc.weight.Value1() + arc.weight.Value2()),
233  arc.nextstate);
234  }
235  MapFinalAction FinalAction() { return MAP_NO_SUPERFINAL; }
236 
237  MapSymbolsAction InputSymbolsAction() { return MAP_COPY_SYMBOLS; }
238 
239  MapSymbolsAction OutputSymbolsAction() { return MAP_COPY_SYMBOLS; }
240 
241  // I believe all properties are preserved.
242  uint64 Properties(uint64 props) { return props; }
243 };
244 
245 
246 template<class Weight, class Int>
248  Weight beam,
249  MutableFst<ArcTpl<CompactLatticeWeightTpl<Weight, Int> > > *fst);
250 
251 
252 } // end namespace fst
253 
255 
256 #endif // KALDI_FSTEXT_LATTICE_UTILS_H_
void RemoveAlignmentsFromCompactLattice(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *fst)
Removes state-level alignments (the strings that are part of the weights).
LatticeWeightTpl< Real > LatticeWeight
MapSymbolsAction InputSymbolsAction()
uint64 Properties(uint64 props)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc StdArc
ArcTpl< LatticeWeight > LatticeArc
LatticeArc operator()(const StdArc &arc)
StdArc operator()(const LatticeArc &arc)
Class LatticeToStdMapper maps a LatticeArc to a normal arc (StdArc) by adding the elements of the Lat...
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
ArcTpl< LatticeWeight > LatticeArc
MapSymbolsAction OutputSymbolsAction()
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
bool CompactLatticeHasAlignment(const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > &fst)
Returns true if lattice has alignments, i.e.
MapSymbolsAction InputSymbolsAction()
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
LatticeWeightTpl< Real > LatticeWeight
void PruneCompactLattice(Weight beam, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *fst)
uint64 Properties(uint64 props)
fst::StdArc::Weight Weight
std::vector< std::vector< double > > GraphLatticeScale(double lmwt)
Class StdToLatticeMapper maps a normal arc (StdArc) to a LatticeArc by putting the StdArc weight as t...
void ConvertFstToLattice(const ExpandedFst< ArcTpl< TropicalWeight > > &ifst, MutableFst< ArcTpl< LatticeWeightTpl< Real > > > *ofst)
Converts TropicalWeight to LatticeWeight (puts all the weight on the first float in the lattice&#39;s pai...
MapSymbolsAction OutputSymbolsAction()
MapFinalAction FinalAction()
MapFinalAction FinalAction()
std::vector< std::vector< double > > DefaultLatticeScale()
Returns a default 2x2 matrix scaling factor for LatticeWeight.