factor.h
Go to the documentation of this file.
1 // fstext/factor.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 #ifndef KALDI_FSTEXT_FACTOR_H_
21 #define KALDI_FSTEXT_FACTOR_H_
22 
23 /*
24  This header declares the Factor function, which takes an FST and
25  compresses it by detecting linear chains of states, and creating
26  special input symbols that represent these chains. It outputs enough
27  information to be able to reconstruct the original sequences [i.e.
28  the mapping between the new symbols, and sequences of the original
29  symbols]. It ensures that the original symbols all have the same
30  number as a corresponding "new" symbol representing a sequence of length
31  one; this enables certain optimizations later on.
32 */
33 
34 
35 #include <fst/fstlib.h>
36 #include <fst/fst-decl.h>
37 #include "util/const-integer-set.h"
38 
39 namespace fst {
40 
41 
64 template<class Arc, class I>
65 void Factor(const Fst<Arc> &fst, MutableFst<Arc> *ofst,
66  std::vector<std::vector<I> > *symbols);
67 
68 
71 template<class Arc>
72 void Factor(const Fst<Arc> &fst, MutableFst<Arc> *ofst1,
73  MutableFst<Arc> *ofst2);
74 
75 
76 
82 template<class Arc, class I>
83 void ExpandInputSequences(const std::vector<std::vector<I> > &sequences,
84  MutableFst<Arc> *fst);
85 
86 
97 template<class Arc, class I>
98 void CreateFactorFst(const std::vector<std::vector<I> > &sequences,
99  MutableFst<Arc> *fst);
100 
101 
107 template<class Arc, class I>
108 void CreateMapFst(const std::vector<I> &symbol_map,
109  MutableFst<Arc> *fst);
110 
111 
113 { kStateFinal = 0x1,
121 
122 typedef unsigned char StatePropertiesType;
123 
127 template<class Arc>
128 void GetStateProperties(const Fst<Arc> &fst,
129  typename Arc::StateId max_state,
130  std::vector<StatePropertiesType> *props);
131 
132 
133 
134 template<class Arc>
136  // visitor class that gives the user the dfs order,
137  // c.f. dfs-visit.h. Used in factor-fst-impl.h
138  typedef typename Arc::StateId StateId;
139  public:
140  DfsOrderVisitor(std::vector<StateId> *order): order_(order) { order->clear(); }
141  void InitVisit(const Fst<Arc> &fst) {}
142  bool InitState(StateId s, StateId) { order_->push_back(s); return true; }
143  bool TreeArc(StateId, const Arc&) { return true; }
144  bool BackArc(StateId, const Arc&) { return true; }
145  bool ForwardOrCrossArc(StateId, const Arc&) { return true; }
146  void FinishState(StateId, StateId, const Arc *) { }
147  void FinishVisit() { }
148  private:
149  std::vector<StateId> *order_;
150 };
151 
152 
153 
154 } // namespace fst
155 
156 #include "factor-inl.h"
157 
158 #endif
fst::StdArc::StateId StateId
StatePropertiesEnum
Definition: factor.h:112
void FinishVisit()
Definition: factor.h:147
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
Arc::StateId StateId
Definition: factor.h:138
DfsOrderVisitor(std::vector< StateId > *order)
Definition: factor.h:140
void GetStateProperties(const Fst< Arc > &fst, typename Arc::StateId max_state, std::vector< StatePropertiesType > *props)
This function works out various properties of the states in the FST, using the bit properties defined...
Definition: factor-inl.h:37
void InitVisit(const Fst< Arc > &fst)
Definition: factor.h:141
unsigned char StatePropertiesType
Definition: factor.h:122
bool ForwardOrCrossArc(StateId, const Arc &)
Definition: factor.h:145
void CreateFactorFst(const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
The function CreateFactorFst will create an FST that expands out the "factors" that are the indices o...
Definition: factor-inl.h:250
void Factor(const Fst< Arc > &fst, MutableFst< Arc > *ofst, std::vector< std::vector< I > > *symbols_out)
Factor identifies linear chains of states with an olabel (if any) only on the first arc of the chain...
Definition: factor-inl.h:69
void FinishState(StateId, StateId, const Arc *)
Definition: factor.h:146
bool BackArc(StateId, const Arc &)
Definition: factor.h:144
bool TreeArc(StateId, const Arc &)
Definition: factor.h:143
bool InitState(StateId s, StateId)
Definition: factor.h:142
std::vector< StateId > * order_
Definition: factor.h:149
void CreateMapFst(const std::vector< I > &symbol_map, MutableFst< Arc > *fst)
CreateMapFst will create an FST representing this symbol_map.
Definition: factor-inl.h:285
void ExpandInputSequences(const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
ExpandInputSequences expands out the input symbols into sequences of input symbols.
Definition: factor-inl.h:163