Cambridge SMT System
fstutils.mapper.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
15 #ifndef FSTUTILS_MAPPER_HPP
16 #define FSTUTILS_MAPPER_HPP
17 
24 namespace fst {
25 
31 template<class Arc, class WeightMakerFunctorT>
33  public:
35  explicit GenericWeightAutoMapper ( const WeightMakerFunctorT& mw )
36  : mw_( mw )
37  {};
38 
40  inline Arc operator() ( const Arc& arc ) const {
41  return Arc ( arc.ilabel, arc.olabel, mw_ ( arc.weight ), arc.nextstate );
42  }
43 
44  inline MapSymbolsAction InputSymbolsAction() const {
45  return MAP_COPY_SYMBOLS;
46  }
47  inline MapSymbolsAction OutputSymbolsAction() const {
48  return MAP_COPY_SYMBOLS;
49  }
50  inline MapFinalAction FinalAction() const {
51  return MAP_NO_SUPERFINAL;
52  }
53  inline uint Properties ( uint props ) const {
54  return props;
55  }
56 
58  WeightMakerFunctorT const &mw_;
59 
60 };
61 
68 template<class FromArc, class ToArc, class WeightMakerFunctorT >
70  public:
71  explicit GenericWeightMapper ( const WeightMakerFunctorT& mw ) : mw_ ( mw ) {};
72 
73  ToArc operator() ( const FromArc& arc ) const {
74  if ( arc.weight == FromArc::Weight::Zero() )
75  return ToArc ( arc.ilabel, arc.olabel, ToArc::Weight::Zero(), arc.nextstate );
76  if ( arc.weight == FromArc::Weight::One() )
77  return ToArc ( arc.ilabel, arc.olabel, ToArc::Weight::One(), arc.nextstate );
78  return ToArc ( arc.ilabel, arc.olabel, mw_ ( arc.weight ), arc.nextstate );
79  }
80 
81  MapSymbolsAction InputSymbolsAction() const {
82  return MAP_COPY_SYMBOLS;
83  }
84  MapSymbolsAction OutputSymbolsAction() const {
85  return MAP_COPY_SYMBOLS;
86  }
87  MapFinalAction FinalAction() const {
88  return MAP_NO_SUPERFINAL;
89  }
90  uint Properties ( uint props ) const {
91  return ( props & kWeightInvariantProperties ) | kUnweighted;
92  }
93 
94  private:
96  WeightMakerFunctorT const &mw_;
97 };
98 
99 
100 template<class FromArc, class ToArc, class ArcMakerFunctorT >
102  public:
103  explicit GenericArcMapper ( const ArcMakerFunctorT& ma ) : ma_ ( ma ) {};
104 
105  ToArc operator() ( const FromArc& arc ) const {
106  return ma_(arc);
107  }
108 
109  MapSymbolsAction InputSymbolsAction() const {
110  return MAP_COPY_SYMBOLS;
111  }
112  MapSymbolsAction OutputSymbolsAction() const {
113  return MAP_COPY_SYMBOLS;
114  }
115  MapFinalAction FinalAction() const {
116  return MAP_NO_SUPERFINAL;
117  }
118  uint Properties ( uint props ) const {
119  return ( props & kWeightInvariantProperties ) | kUnweighted;
120  }
121 
122  private:
123  ArcMakerFunctorT const &ma_;
124 };
125 
126 
127 template<class Arc, class ArcMakerFunctorT >
129  public:
130  explicit GenericArcAutoMapper ( const ArcMakerFunctorT& ma ) : ma_ ( ma ) {};
131 
132  Arc operator() ( const Arc& arc ) const {
133  return ma_(arc);
134  }
135 
136  MapSymbolsAction InputSymbolsAction() const {
137  return MAP_COPY_SYMBOLS;
138  }
139  MapSymbolsAction OutputSymbolsAction() const {
140  return MAP_COPY_SYMBOLS;
141  }
142  MapFinalAction FinalAction() const {
143  return MAP_NO_SUPERFINAL;
144  }
145  uint Properties ( uint props ) const {
146  return ( props & kWeightInvariantProperties ) | kUnweighted;
147  }
148 
149  private:
150  ArcMakerFunctorT const &ma_;
151 };
152 
153 
154 
159 template<class Arc>
161  public:
163  explicit WordPenaltyMapper ( typename Arc::Weight wp,
164  unordered_set<typename Arc::Label> epsilons) :
165  epsilons_ (epsilons),
166  wp_ ( wp ) {
167  };
168 
170  Arc operator() ( const Arc& arc ) const {
171  if (epsilons_.find (arc.ilabel) != epsilons_.end() )
172  return Arc ( arc.ilabel, arc.olabel, arc.weight, arc.nextstate );
173  return Arc ( arc.ilabel, arc.olabel, Times (arc.weight, wp_), arc.nextstate );
174  }
175 
176  inline MapSymbolsAction InputSymbolsAction() const {
177  return MAP_COPY_SYMBOLS;
178  }
179  inline MapSymbolsAction OutputSymbolsAction() const {
180  return MAP_COPY_SYMBOLS;
181  }
182  inline MapFinalAction FinalAction() const {
183  return MAP_NO_SUPERFINAL;
184  }
185  inline uint Properties ( uint props ) const {
186  return props;
187  }
188 
190  typename Arc::Weight wp_;
191  unordered_set<typename Arc::Label> epsilons_;
192 
193 };
194 
197  typedef StdArc FromArc;
198  typedef LexStdArc ToArc;
199  typedef ToArc::Weight W;
200 
201  //If i==0, copies to both weights. If i=1 or 2, only copies stdweight to first or second weight respectively, leaving a Weight::One() on the other.
202  explicit StdToLexStdMapper (int i = 0) : i_ (i) {
203  CHECK (!i || i == 1 || i == 2);
204  }
205 
206  LexStdArc operator() (const StdArc& arc) const {
207  FromArc::Weight w1 = (i_ == 1
208  || !i_) ? arc.weight.Value() : FromArc::Weight::One();
209  FromArc::Weight w2 = (i_ == 2
210  || !i_) ? arc.weight.Value() : FromArc::Weight::One();
211  W w (w1, w2);
212  return LexStdArc (arc.ilabel, arc.olabel, w, arc.nextstate);
213  }
214 
215  MapSymbolsAction InputSymbolsAction() const {
216  return MAP_COPY_SYMBOLS;
217  }
218  MapSymbolsAction OutputSymbolsAction() const {
219  return MAP_COPY_SYMBOLS;
220  }
221  MapFinalAction FinalAction() const {
222  return MAP_NO_SUPERFINAL;
223  }
224  uint Properties (uint props) const {
225  return props;
226  }
227  int i_;
228 };
229 
233  typedef StdArc ToArc;
234  typedef ToArc::Weight W;
235 
236  explicit LexStdToStdMapper() : i_ (0) {}
237 
238  explicit LexStdToStdMapper (int i) : i_ (i) {
239  CHECK (i == 1 || i == 2 || i == 0);
240  }
241 
242  StdArc operator() (const LexStdArc& arc) const {
243  W w;
244  if (i_ == 0)
245  w = Times ( arc.weight.Value1(), arc.weight.Value2() );
246  if (i_ == 1)
247  w = arc.weight.Value1();
248  if (i_ == 2)
249  w = arc.weight.Value2();
250  return StdArc (arc.ilabel, arc.olabel, w, arc.nextstate);
251  }
252 
253  MapSymbolsAction InputSymbolsAction() const {
254  return MAP_COPY_SYMBOLS;
255  }
256 
257  MapSymbolsAction OutputSymbolsAction() const {
258  return MAP_COPY_SYMBOLS;
259  }
260 
261  MapFinalAction FinalAction() const {
262  return MAP_NO_SUPERFINAL;
263  }
264 
265  uint Properties (uint props) const {
266  return props;
267  }
268 
269  int i_;
270 };
271 
272 }; //namespace
273 
274 #endif
WordPenaltyMapper(typename Arc::Weight wp, unordered_set< typename Arc::Label > epsilons)
Constructor.
GenericArcAutoMapper(const ArcMakerFunctorT &ma)
MapFinalAction FinalAction() const
templated Mapper that inserts a word penalty over an FST, skipping user defined epsilon arcs...
MapSymbolsAction InputSymbolsAction() const
Definition: fstio.hpp:27
MapSymbolsAction OutputSymbolsAction() const
MapSymbolsAction InputSymbolsAction() const
MapSymbolsAction InputSymbolsAction() const
templated Mapper that modifies weights over an FST, passing through the other values of the arc...
uint Properties(uint props) const
StdArc to LexStdArc mapper.
MapSymbolsAction OutputSymbolsAction() const
MapSymbolsAction OutputSymbolsAction() const
MapFinalAction FinalAction() const
Arc operator()(const Arc &arc) const
Takes arc as input parameter and returns modified arc.
LexStdArc to StdArc Mapper.
MapSymbolsAction OutputSymbolsAction() const
MapFinalAction FinalAction() const
unordered_set< typename Arc::Label > epsilons_
MapFinalAction FinalAction() const
MapSymbolsAction InputSymbolsAction() const
templated Mapper that modifies weights when copying from one FST to another, passing through the othe...
uint Properties(uint props) const
Arc::Weight wp_
Specialized functor that modifies arc weights.
uint Properties(uint props) const
MapFinalAction FinalAction() const
MapFinalAction FinalAction() const
LexicographicArc< StdArc::Weight, StdArc::Weight > LexStdArc
MapSymbolsAction InputSymbolsAction() const
TropicalSparseTupleWeight< T > Times(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2)
WeightMakerFunctorT const & mw_
Specialized functor that modifies arc weights.
MapSymbolsAction InputSymbolsAction() const
uint Properties(uint props) const
uint Properties(uint props) const
MapSymbolsAction OutputSymbolsAction() const
MapFinalAction FinalAction() const
MapSymbolsAction InputSymbolsAction() const
uint Properties(uint props) const
GenericArcMapper(const ArcMakerFunctorT &ma)
uint Properties(uint props) const
MapSymbolsAction OutputSymbolsAction() const
MapSymbolsAction OutputSymbolsAction() const
GenericWeightMapper(const WeightMakerFunctorT &mw)
GenericWeightAutoMapper(const WeightMakerFunctorT &mw)
Constructor.