FST  openfst-1.8.2.post1
OpenFst Library
epsnormalize.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Function that implements epsilon-normalization.
19 
20 #ifndef FST_EPSNORMALIZE_H_
21 #define FST_EPSNORMALIZE_H_
22 
23 
24 #include <fst/arc-map.h>
25 #include <fst/factor-weight.h>
26 #include <fst/invert.h>
27 #include <fst/rmepsilon.h>
28 
29 
30 namespace fst {
31 
33 
34 // Returns an equivalent FST that is epsilon-normalized. An acceptor is
35 // epsilon-normalized if it is epsilon-removed. A transducer is input
36 // epsilon-normalized if additionally if on each path any epsilon input
37 // label follows all non-epsilon input labels. Output epsilon-normalized
38 // is defined similarly.
39 //
40 // For more information, see:
41 //
42 // Mohri, M. 2002. Generic epsilon-removal and input epsilon-normalization
43 // algorithms for weighted transducers. International Journal of Computer
44 // Science, 13(1): 129-143, 2002.
45 template <class Arc>
46 void EpsNormalize(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
48  EpsNormalize<Arc, GALLIC>(ifst, ofst, type);
49 }
50 
51 // Same as above, except allows specifying explicitly the gallic weight type.
52 template <class Arc, GallicType G>
53 void EpsNormalize(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
54  EpsNormalizeType type) {
56  std::unique_ptr<SymbolTable> symbols;
57  if (type == EPS_NORM_INPUT) {
58  ArcMap(ifst, &gfst, ToGallicMapper<Arc, G>());
59  if (ifst.OutputSymbols()) symbols.reset(ifst.OutputSymbols()->Copy());
60  } else { // type == EPS_NORM_OUTPUT
62  if (ifst.InputSymbols()) symbols.reset(ifst.InputSymbols()->Copy());
63  }
64  RmEpsilon(&gfst);
67  fwfst(gfst);
68  ArcMap(fwfst, ofst, FromGallicMapper<Arc, G>());
69  ofst->SetOutputSymbols(symbols.get());
70  if (type == EPS_NORM_OUTPUT) Invert(ofst);
71 }
72 
73 } // namespace fst
74 
75 #endif // FST_EPSNORMALIZE_H_
void ArcMap(MutableFst< A > *fst, C *mapper)
Definition: arc-map.h:110
void RmEpsilon(MutableFst< Arc > *fst, std::vector< typename Arc::Weight > *distance, const RmEpsilonOptions< Arc, Queue > &opts)
Definition: rmepsilon.h:207
void Invert(const Fst< Arc > &ifst, MutableFst< Arc > *ofst)
Definition: invert.h:67
virtual SymbolTable * Copy() const
Definition: symbol-table.h:407
EpsNormalizeType
Definition: epsnormalize.h:32
void EpsNormalize(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, EpsNormalizeType type=EPS_NORM_INPUT)
Definition: epsnormalize.h:46
virtual const SymbolTable * InputSymbols() const =0
virtual void SetOutputSymbols(const SymbolTable *osyms)=0
virtual const SymbolTable * OutputSymbols() const =0