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