FST  openfst-1.7.2
OpenFst Library
project.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Functions and classes to project an FST on to its domain or range.
5 
6 #ifndef FST_PROJECT_H_
7 #define FST_PROJECT_H_
8 
9 #include <fst/arc-map.h>
10 #include <fst/mutable-fst.h>
11 
12 
13 namespace fst {
14 
15 // This specifies whether to project on input or output.
17 
18 // Mapper to implement projection per arc.
19 template <class A>
21  public:
22  using FromArc = A;
23  using ToArc = A;
24 
25  constexpr explicit ProjectMapper(ProjectType project_type)
26  : project_type_(project_type) {}
27 
28  ToArc operator()(const FromArc &arc) const {
29  const auto label = project_type_ == PROJECT_INPUT ? arc.ilabel : arc.olabel;
30  return ToArc(label, label, arc.weight, arc.nextstate);
31  }
32 
33  constexpr MapFinalAction FinalAction() const {
34  return MAP_NO_SUPERFINAL;
35  }
36 
38  return project_type_ == PROJECT_INPUT ? MAP_COPY_SYMBOLS
40  }
41 
43  return project_type_ == PROJECT_OUTPUT ? MAP_COPY_SYMBOLS
45  }
46 
47  constexpr uint64 Properties(uint64 props) const {
48  return ProjectProperties(props, project_type_ == PROJECT_INPUT);
49  }
50 
51  private:
52  const ProjectType project_type_;
53 };
54 
55 // Projects an FST onto its domain or range by either copying each arcs' input
56 // label to the output label or vice versa.
57 //
58 // Complexity:
59 //
60 // Time: O(V + E)
61 // Space: O(1)
62 //
63 // where V is the number of states and E is the number of arcs.
64 template <class Arc>
65 inline void Project(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
66  ProjectType project_type) {
67  ArcMap(ifst, ofst, ProjectMapper<Arc>(project_type));
68  switch (project_type) {
69  case PROJECT_INPUT:
70  ofst->SetOutputSymbols(ifst.InputSymbols());
71  return;
72  case PROJECT_OUTPUT:
73  ofst->SetInputSymbols(ifst.OutputSymbols());
74  return;
75  }
76 }
77 
78 // Destructive variant of the above.
79 template <class Arc>
80 inline void Project(MutableFst<Arc> *fst, ProjectType project_type) {
81  ArcMap(fst, ProjectMapper<Arc>(project_type));
82  switch (project_type) {
83  case PROJECT_INPUT:
84  fst->SetOutputSymbols(fst->InputSymbols());
85  return;
86  case PROJECT_OUTPUT:
87  fst->SetInputSymbols(fst->OutputSymbols());
88  return;
89  }
90 }
91 
92 // Projects an FST onto its domain or range by either copying each arc's input
93 // label to the output label or vice versa. This version is a delayed FST.
94 //
95 // Complexity:
96 //
97 // Time: O(v + e)
98 // Space: O(1)
99 //
100 // where v is the number of states visited and e is the number of arcs visited.
101 // Constant time and to visit an input state or arc is assumed and exclusive of
102 // caching.
103 template <class A>
104 class ProjectFst : public ArcMapFst<A, A, ProjectMapper<A>> {
105  public:
106  using FromArc = A;
107  using ToArc = A;
108 
110 
111  ProjectFst(const Fst<A> &fst, ProjectType project_type)
112  : ArcMapFst<A, A, ProjectMapper<A>>(fst, ProjectMapper<A>(project_type)) {
113  if (project_type == PROJECT_INPUT) {
114  GetMutableImpl()->SetOutputSymbols(fst.InputSymbols());
115  }
116  if (project_type == PROJECT_OUTPUT) {
117  GetMutableImpl()->SetInputSymbols(fst.OutputSymbols());
118  }
119  }
120 
121  // See Fst<>::Copy() for doc.
122  ProjectFst(const ProjectFst<A> &fst, bool safe = false)
123  : ArcMapFst<A, A, ProjectMapper<A>>(fst, safe) {}
124 
125  // Gets a copy of this ProjectFst. See Fst<>::Copy() for further doc.
126  ProjectFst<A> *Copy(bool safe = false) const override {
127  return new ProjectFst(*this, safe);
128  }
129 
130  private:
132 };
133 
134 // Specialization for ProjectFst.
135 template <class A>
137  : public StateIterator<ArcMapFst<A, A, ProjectMapper<A>>> {
138  public:
139  explicit StateIterator(const ProjectFst<A> &fst)
140  : StateIterator<ArcMapFst<A, A, ProjectMapper<A>>>(fst) {}
141 };
142 
143 // Specialization for ProjectFst.
144 template <class A>
146  : public ArcIterator<ArcMapFst<A, A, ProjectMapper<A>>> {
147  public:
148  using StateId = typename A::StateId;
149 
151  : ArcIterator<ArcMapFst<A, A, ProjectMapper<A>>>(fst, s) {}
152 };
153 
154 // Useful alias when using StdArc.
156 
157 } // namespace fst
158 
159 #endif // FST_PROJECT_H_
MapSymbolsAction
Definition: arc-map.h:39
typename A::StateId StateId
Definition: project.h:148
void ArcMap(MutableFst< A > *fst, C *mapper)
Definition: arc-map.h:94
uint64_t uint64
Definition: types.h:32
ToArc operator()(const FromArc &arc) const
Definition: project.h:28
const SymbolTable * InputSymbols() const override=0
virtual void SetInputSymbols(const SymbolTable *isyms)=0
ProjectType
Definition: project.h:16
constexpr MapFinalAction FinalAction() const
Definition: project.h:33
const SymbolTable * OutputSymbols() const override=0
uint64 ProjectProperties(uint64 inprops, bool project_input)
Definition: properties.cc:177
MapFinalAction
Definition: arc-map.h:24
constexpr ProjectMapper(ProjectType project_type)
Definition: project.h:25
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: project.h:42
constexpr uint64 Properties(uint64 props) const
Definition: project.h:47
void Project(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, ProjectType project_type)
Definition: project.h:65
virtual const SymbolTable * InputSymbols() const =0
StateIterator(const ProjectFst< A > &fst)
Definition: project.h:139
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: project.h:37
ProjectFst< A > * Copy(bool safe=false) const override
Definition: project.h:126
virtual void SetOutputSymbols(const SymbolTable *osyms)=0
ProjectFst(const ProjectFst< A > &fst, bool safe=false)
Definition: project.h:122
ProjectFst(const Fst< A > &fst, ProjectType project_type)
Definition: project.h:111
ArcIterator(const ProjectFst< A > &fst, StateId s)
Definition: project.h:150
virtual const SymbolTable * OutputSymbols() const =0