FST  openfst-1.8.3
OpenFst Library
epsnormalize.h
Go to the documentation of this file.
1 // Copyright 2005-2024 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 #include <memory>
24 
25 #include <fst/arc-map.h>
26 #include <fst/arc.h>
27 #include <fst/factor-weight.h>
28 #include <fst/fst.h>
29 #include <fst/invert.h>
30 #include <fst/mutable-fst.h>
31 #include <fst/rmepsilon.h>
32 #include <fst/string-weight.h>
33 #include <fst/symbol-table.h>
34 #include <fst/vector-fst.h>
35 
36 namespace fst {
37 
39 
40 // Returns an equivalent FST that is epsilon-normalized. An acceptor is
41 // epsilon-normalized if it is epsilon-removed. A transducer is input
42 // epsilon-normalized if additionally if on each path any epsilon input
43 // label follows all non-epsilon input labels. Output epsilon-normalized
44 // is defined similarly.
45 //
46 // For more information, see:
47 //
48 // Mohri, M. 2002. Generic epsilon-removal and input epsilon-normalization
49 // algorithms for weighted transducers. International Journal of Computer
50 // Science, 13(1): 129-143, 2002.
51 template <class Arc>
52 void EpsNormalize(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
54  EpsNormalize<Arc, GALLIC>(ifst, ofst, type);
55 }
56 
57 // Same as above, except allows specifying explicitly the gallic weight type.
58 template <class Arc, GallicType G>
59 void EpsNormalize(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
60  EpsNormalizeType type) {
62  std::unique_ptr<SymbolTable> symbols;
63  if (type == EPS_NORM_INPUT) {
64  ArcMap(ifst, &gfst, ToGallicMapper<Arc, G>());
65  if (ifst.OutputSymbols()) symbols.reset(ifst.OutputSymbols()->Copy());
66  } else { // type == EPS_NORM_OUTPUT
68  if (ifst.InputSymbols()) symbols.reset(ifst.InputSymbols()->Copy());
69  }
70  RmEpsilon(&gfst);
73  fwfst(gfst);
74  ArcMap(fwfst, ofst, FromGallicMapper<Arc, G>());
75  ofst->SetOutputSymbols(symbols.get());
76  if (type == EPS_NORM_OUTPUT) Invert(ofst);
77 }
78 
79 } // namespace fst
80 
81 #endif // FST_EPSNORMALIZE_H_
void ArcMap(MutableFst< A > *fst, C *mapper)
Definition: arc-map.h:120
void RmEpsilon(MutableFst< Arc > *fst, std::vector< typename Arc::Weight > *distance, const RmEpsilonOptions< Arc, Queue > &opts)
Definition: rmepsilon.h:217
void Invert(const Fst< Arc > &ifst, MutableFst< Arc > *ofst)
Definition: invert.h:73
virtual SymbolTable * Copy() const
Definition: symbol-table.h:411
EpsNormalizeType
Definition: epsnormalize.h:38
void EpsNormalize(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, EpsNormalizeType type=EPS_NORM_INPUT)
Definition: epsnormalize.h:52
virtual const SymbolTable * InputSymbols() const =0
virtual void SetOutputSymbols(const SymbolTable *osyms)=0
virtual const SymbolTable * OutputSymbols() const =0