FST  openfst-1.8.3
OpenFst Library
project.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 // Functions and classes to project an FST on to its domain or range.
19 
20 #ifndef FST_PROJECT_H_
21 #define FST_PROJECT_H_
22 
23 #include <cstdint>
24 
25 #include <fst/arc-map.h>
26 #include <fst/arc.h>
27 #include <fst/cache.h>
28 #include <fst/float-weight.h>
29 #include <fst/fst.h>
30 #include <fst/impl-to-fst.h>
31 #include <fst/mutable-fst.h>
32 #include <fst/properties.h>
33 
34 namespace fst {
35 
36 // This specifies whether to project on input or output.
37 enum class ProjectType { INPUT = 1, OUTPUT = 2 };
38 OPENFST_DEPRECATED("Use `ProjectType::INPUT` instead.")
40 OPENFST_DEPRECATED("Use `ProjectType::OUTPUT` instead.")
42 
43 // Mapper to implement projection per arc.
44 template <class A>
46  public:
47  using FromArc = A;
48  using ToArc = A;
49 
50  constexpr explicit ProjectMapper(ProjectType project_type)
51  : project_type_(project_type) {}
52 
53  ToArc operator()(const FromArc &arc) const {
54  const auto label =
55  project_type_ == ProjectType::INPUT ? arc.ilabel : arc.olabel;
56  return ToArc(label, label, arc.weight, arc.nextstate);
57  }
58 
59  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
60 
62  return project_type_ == ProjectType::INPUT ? MAP_COPY_SYMBOLS
64  }
65 
67  return project_type_ == ProjectType::OUTPUT ? MAP_COPY_SYMBOLS
69  }
70 
71  constexpr uint64_t Properties(uint64_t props) const {
72  return ProjectProperties(props, project_type_ == ProjectType::INPUT);
73  }
74 
75  private:
76  const ProjectType project_type_;
77 };
78 
79 // Projects an FST onto its domain or range by either copying each arcs' input
80 // label to the output label or vice versa.
81 //
82 // Complexity:
83 //
84 // Time: O(V + E)
85 // Space: O(1)
86 //
87 // where V is the number of states and E is the number of arcs.
88 template <class Arc>
89 inline void Project(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
90  ProjectType project_type) {
91  ArcMap(ifst, ofst, ProjectMapper<Arc>(project_type));
92  switch (project_type) {
93  case ProjectType::INPUT:
94  ofst->SetOutputSymbols(ifst.InputSymbols());
95  return;
97  ofst->SetInputSymbols(ifst.OutputSymbols());
98  return;
99  }
100 }
101 
102 // Destructive variant of the above.
103 template <class Arc>
104 inline void Project(MutableFst<Arc> *fst, ProjectType project_type) {
105  ArcMap(fst, ProjectMapper<Arc>(project_type));
106  switch (project_type) {
107  case ProjectType::INPUT:
108  fst->SetOutputSymbols(fst->InputSymbols());
109  return;
110  case ProjectType::OUTPUT:
111  fst->SetInputSymbols(fst->OutputSymbols());
112  return;
113  }
114 }
115 
116 // Projects an FST onto its domain or range by either copying each arc's input
117 // label to the output label or vice versa. This version is a delayed FST.
118 //
119 // Complexity:
120 //
121 // Time: O(v + e)
122 // Space: O(1)
123 //
124 // where v is the number of states visited and e is the number of arcs visited.
125 // Constant time and to visit an input state or arc is assumed and exclusive of
126 // caching.
127 template <class A>
128 class ProjectFst : public ArcMapFst<A, A, ProjectMapper<A>> {
129  public:
130  using FromArc = A;
131  using ToArc = A;
132 
134 
135  ProjectFst(const Fst<A> &fst, ProjectType project_type)
136  : ArcMapFst<A, A, ProjectMapper<A>>(fst, ProjectMapper<A>(project_type)) {
137  if (project_type == ProjectType::INPUT) {
138  GetMutableImpl()->SetOutputSymbols(fst.InputSymbols());
139  }
140  if (project_type == ProjectType::OUTPUT) {
141  GetMutableImpl()->SetInputSymbols(fst.OutputSymbols());
142  }
143  }
144 
145  // See Fst<>::Copy() for doc.
146  ProjectFst(const ProjectFst &fst, bool safe = false)
147  : ArcMapFst<A, A, ProjectMapper<A>>(fst, safe) {}
148 
149  // Gets a copy of this ProjectFst. See Fst<>::Copy() for further doc.
150  ProjectFst *Copy(bool safe = false) const override {
151  return new ProjectFst(*this, safe);
152  }
153 
154  private:
156 };
157 
158 // Specialization for ProjectFst.
159 template <class A>
161  : public StateIterator<ArcMapFst<A, A, ProjectMapper<A>>> {
162  public:
163  explicit StateIterator(const ProjectFst<A> &fst)
164  : StateIterator<ArcMapFst<A, A, ProjectMapper<A>>>(fst) {}
165 };
166 
167 // Specialization for ProjectFst.
168 template <class A>
170  : public ArcIterator<ArcMapFst<A, A, ProjectMapper<A>>> {
171  public:
172  using StateId = typename A::StateId;
173 
175  : ArcIterator<ArcMapFst<A, A, ProjectMapper<A>>>(fst, s) {}
176 };
177 
178 // Useful alias when using StdArc.
180 
181 } // namespace fst
182 
183 #endif // FST_PROJECT_H_
MapSymbolsAction
Definition: arc-map.h:65
typename A::StateId StateId
Definition: project.h:172
void ArcMap(MutableFst< A > *fst, C *mapper)
Definition: arc-map.h:120
class OPENFST_DEPRECATED("allow_negative is no-op") StringCompiler
Definition: string.h:170
constexpr ProjectType PROJECT_INPUT
Definition: project.h:39
ToArc operator()(const FromArc &arc) const
Definition: project.h:53
const SymbolTable * InputSymbols() const override=0
virtual void SetInputSymbols(const SymbolTable *isyms)=0
ProjectFst * Copy(bool safe=false) const override
Definition: project.h:150
ProjectType
Definition: project.h:37
constexpr MapFinalAction FinalAction() const
Definition: project.h:59
const SymbolTable * OutputSymbols() const override=0
MapFinalAction
Definition: arc-map.h:50
constexpr ProjectType PROJECT_OUTPUT
Definition: project.h:41
ProjectFst(const ProjectFst &fst, bool safe=false)
Definition: project.h:146
constexpr ProjectMapper(ProjectType project_type)
Definition: project.h:50
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: project.h:66
void Project(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, ProjectType project_type)
Definition: project.h:89
virtual const SymbolTable * InputSymbols() const =0
StateIterator(const ProjectFst< A > &fst)
Definition: project.h:163
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: project.h:61
uint64_t ProjectProperties(uint64_t inprops, bool project_input)
Definition: properties.cc:195
virtual void SetOutputSymbols(const SymbolTable *osyms)=0
ProjectFst(const Fst< A > &fst, ProjectType project_type)
Definition: project.h:135
ArcIterator(const ProjectFst< A > &fst, StateId s)
Definition: project.h:174
constexpr uint64_t Properties(uint64_t props) const
Definition: project.h:71
virtual const SymbolTable * OutputSymbols() const =0