Cambridge SMT System
task.hifst.optimize.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 TASK_HIFST_OPTIMIZE_HPP
16 #define TASK_HIFST_OPTIMIZE_HPP
17 
23 namespace ucam {
24 namespace hifst {
25 
26 template<class Arc = fst::LexStdArc>
28  typedef typename Arc::Label Label;
29  typedef typename Arc::Weight Weight;
30 
31  private:
32  bool alignmode_;
33 
34  public:
36  OptimizeMachine ( bool align = false ) : alignmode_ (align) {};
37  inline void setAlignMode (bool align) {
38  alignmode_ = align;
39  };
40 
41  // If numstates is = to threshold, then the machine fully optimized
42  // Impose any external condition in check => if it doesn'nt meet condition, then simply rmepsilons
43  inline void operator() ( fst::VectorFst<Arc> *fst ,
44  uint nstatesthreshold = std::numeric_limits<uint>::max() ,
45  bool check = true ) const {
46  if (fst->NumStates() > nstatesthreshold || ! check ) {
47  LINFO ("Only rm epsilons...");
48  fst::RmEpsilon<Arc> (fst);
49  return;
50  }
51  LINFO ("Full optimization");
52  this->optimize (fst);
53  }
54 
55  private:
56 
57  inline void optimize ( fst::VectorFst<Arc> *fst ) const {
58  if ( !alignmode_ ) {
59  LINFO ("FSA");
60  fst::Determinize (fst::RmEpsilonFst<Arc> (*fst), fst);
61  fst::Minimize (fst);
62  } else {
63  LINFO ("FST");
64  EncodeDeterminizeMinimizeDecode (fst::RmEpsilonFst<Arc> (*fst) , fst ) ;
65  }
66  };
67 
68  ZDISALLOW_COPY_AND_ASSIGN ( OptimizeMachine );
69 };
70 
71 template<class Arc = fst::LexStdArc>
73  typedef typename Arc::Label Label;
74  typedef typename Arc::Weight Weight;
75 
76  private:
77 
78  public:
81 
82  inline void operator() ( fst::VectorFst<Arc> *fst ) const {
83  fst::RmEpsilon (fst);
84  };
85 
86  inline void operator() ( fst::VectorFst<Arc> *fst ,
87  uint numstatesthreshold ) const {
88  fst::RmEpsilon (fst);
89  };
90 
91  private:
93 };
94 
95 }
96 } // end namespaces
97 
98 #endif
#define ZDISALLOW_COPY_AND_ASSIGN(TypeName)
#define LINFO(msg)
Definition: fstio.hpp:27
fst::VectorFst< Arc > * EncodeDeterminizeMinimizeDecode(fst::VectorFst< Arc > *myfst)
Encodes, determinizes, minimizes and decodes an fst.
Definition: fstutils.hpp:149
bool check(const char *expression, const char *message, const char *filename, const char *function, uint line, bool abort=true)
Function that reports as many details as possible if the assertion USER_CHECK has failed...
void operator()(fst::VectorFst< Arc > *fst, uint nstatesthreshold=std::numeric_limits< uint >::max(), bool check=true) const
Definition: bleu.hpp:14