Cambridge SMT System
task.optimizefst.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_OPTIMIZEFST_HPP
16 #define TASK_OPTIMIZEFST_HPP
17 
25 namespace ucam {
26 namespace fsttools {
27 
32 template <class Data, class Arc = fst::StdArc >
34  typedef typename Arc::Label Label;
35  typedef typename Arc::Weight Weight;
36 
37  private:
39  std::string fstkey_;
40 
41  bool stripHifstEpsilons_;
43  public:
46  , const std::string& fstkey
47  , const std::string stripEps
48  )
49  : fstkey_ (fstkey)
50  , stripHifstEpsilons_(rg.getBool (stripEps) )
51  {
52  if (!stripHifstEpsilons_) return;
53  ru_.addIPL (DR, EPSILON)
54  .addIPL (OOV, EPSILON)
55  .addIPL (SEP, EPSILON)
56  .addOPL (DR, EPSILON)
57  .addOPL (OOV, EPSILON)
58  .addOPL (SEP, EPSILON)
59  ;
60  };
61 
62  inline static OptimizeFstTask * init ( const ucam::util::RegistryPO& rg
63  , const std::string& optimizefstkey
64  , const std::string& fstkey
65  , const std::string& stripepskey
66  ) {
67  if ( rg.getBool ( optimizefstkey ) )
68  return new OptimizeFstTask ( rg, fstkey, stripepskey );
69  return NULL;
70  };
71 
79  inline bool run ( Data& d ) {
80  using namespace fst;
81  VectorFst<Arc> *auxfst = d.getFst(fstkey_);
82  if (auxfst == NULL) {
83  LERROR("Lattice " << d.sidx << "not available under key " << fstkey_);
84  exit(EXIT_FAILURE);
85  }
86  std::string parenskey = fstkey_ + ".parens";
87  if ( d.fsts.find ( parenskey ) != d.fsts.end() ) {
88  LWARN("Skipping optimization! PDTs are, in general, non-determinizable.");
89  return false;
90  }
91  if (stripHifstEpsilons_) {
92  FORCELINFO ("Remove hifst epsilons");
93  ru_(auxfst);
94  }
95  FORCELINFO("Rm/Det/Min lattice " << d.sidx );
96  Determinize(RmEpsilonFst<Arc>(*auxfst), auxfst);
97  Minimize(auxfst);
98  return false;
99  };
100 
101  private:
103 };
104 
105 }} // end namespaces
106 
107 #endif
#define ZDISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: fstio.hpp:27
#define SEP
#define FORCELINFO(msg)
RelabelUtil & addIPL(typename Arc::Label labelfind, typename Arc::Label labelreplace)
Definition: fstutils.hpp:511
#define DR
bool run(Data &d)
Optimizes fst. this involves rmepsilon, determinizing and minimizing. The fst is accessed via data ob...
Templated (hybrid) Interface for Task classes.
Convenience class that inherits Taskinterface behaviour and optimizes an fst.
static OptimizeFstTask * init(const ucam::util::RegistryPO &rg, const std::string &optimizefstkey, const std::string &fstkey, const std::string &stripepskey)
#define LWARN(msg)
Utility functor for relabeling one or more lattices. Note that you can chain commands. See Unit test in fstutils.gtest.cpp for an example.
Definition: fstutils.hpp:503
#define EPSILON
bool getBool(const std::string &key) const
To handle yes|no program option values.
Definition: registrypo.hpp:225
#define OOV
OptimizeFstTask(const ucam::util::RegistryPO &rg, const std::string &fstkey, const std::string stripEps)
Constructor with RegistryPO object.
#define LERROR(msg)
Definition: bleu.hpp:14