Cambridge SMT System
task.sparseweightvectorlattices.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_SPWLATS_HPP
16 #define TASK_SPWLATS_HPP
17 
26 namespace ucam {
27 namespace hifst {
28 
33 template<class Data, class Arc>
35  private:
36  //key for a pointer in the data object to read the alignment lattice
37  const std::string alilatskey_;
39  const std::string sparseweightvectorlatticekey_;
41  const std::string lmkey_;
43  const std::string ruleflowerlatticekey_;
44 
46  fst::VectorFst<TupleArc32> myfst_;
47 
49  Data *d_;
50 
51  //Strip dr, oov, ...
52  bool stripHifstEpsilons_;
53  bool determinize_;
55  public:
58  const std::string& alilatskey =
60  const std::string& ruleflowerlatticekey =
62  const std::string& sparseweightvectorlatticekey =
64  ) :
65  ruleflowerlatticekey_ ( ruleflowerlatticekey )
66  , alilatskey_ ( alilatskey )
67  , sparseweightvectorlatticekey_ ( sparseweightvectorlatticekey )
68  , stripHifstEpsilons_ (rg.getBool (
70  , determinize_ (rg.getBool (
72  ru_.addIPL (DR, EPSILON)
73  .addIPL (OOV, EPSILON)
74  .addIPL (SEP, EPSILON)
75  ;
76  };
77 
85  bool run ( Data& d ) {
86  myfst_.DeleteStates();
87  d_ = &d;
88  fst::VectorFst<Arc> *lattice = static_cast<fst::VectorFst<Arc> *>
89  ( d.fsts[alilatskey_] );
90  USER_CHECK ( lattice->NumStates(), "Empty alignment lattice?" );
91  Invert ( lattice );
92  fst::VectorFst<TupleArc32> *lxr
93  = applyFlowerLattice (*lattice
94  , * ( static_cast<fst::VectorFst<TupleArc32> *>
95  ( d.fsts[ruleflowerlatticekey_] ) ) );
96  LDBG_EXECUTE ( lattice->Write ( "fsts/aplats/aplats+flower.fst" ) );
97  if (stripHifstEpsilons_) {
98  LINFO ("Remove hifst epsilons");
99  ru_ (lxr);
100  }
101  LINFO ( "Project, RmEpsilon, Determinize and Minimize" );
102  fst::Project ( lxr, fst::PROJECT_INPUT );
103  LDBG_EXECUTE ( lxr->Write ( "fsts/aplats/aplats+flower+p.fst.gz" ) );
104  fst::RmEpsilon<TupleArc32> ( lxr );
105  if ( determinize_ ) {
106  fst::Determinize<TupleArc32> ( *lxr, &myfst_ );
107  LDBG_EXECUTE ( myfst_.Write ( "fsts/aplats/aplats+flower+p+re+d.fst" ) );
108  delete lxr;
109  fst::Minimize<TupleArc32> ( &myfst_ );
110  LDBG_EXECUTE ( myfst_.Write ( "fsts/aplats/aplats+flower+p+re+d+m.fst" ) );
111  } else {
112  myfst_ = *lxr;
113  }
114  d.fsts[sparseweightvectorlatticekey_] = &myfst_;
115  LINFO ( "Ready! Stored with key=" << sparseweightvectorlatticekey_ << "; NS=" <<
116  myfst_.NumStates() );
117  return false;
118  };
119 
121  myfst_.DeleteStates();
122  LINFO ("Shutdown!");
123  }
124 
125  private:
126 
128  fst::VectorFst<TupleArc32> * applyFlowerLattice ( const fst::VectorFst<Arc>
129  & hypfst,
130  const fst::VectorFst<TupleArc32>& grammarflowerlattice ) {
131  LINFO ( "Removing Weights of Target Lattice" );
132  fst::VectorFst<Arc> prvfst ( hypfst );
133  fst::Map ( &prvfst, fst::RmWeightMapper<Arc>() );
134  fst::VectorFst<TupleArc32> *vwfst = new fst::VectorFst<TupleArc32>;
135  LINFO ( "Mapping Arc Target Lattice to TupleArc32" );
137  fst::Map<Arc> ( prvfst, vwfst,
139  fst::VectorFst<TupleArc32> *output = new fst::VectorFst<TupleArc32>;
140  LDBG_EXECUTE ( vwfst->Write ( "fsts/aplats/vw-beforecomposing.fst" ) );
141  LINFO ( "Compose (TupleArc32)" );
142  fst::Compose ( *vwfst, grammarflowerlattice, output );
143  delete vwfst;
144  return output;
145  };
146 
147  ZDISALLOW_COPY_AND_ASSIGN ( SparseWeightVectorLatticesTask );
148 
149 };
150 
151 }
152 } // end namespaces
153 
154 #endif // TASK_ALATS2SPWLATS_HPP
bool run(Data &d)
Implements virtual method from ucam::util::TaskInterface.
#define LINFO(msg)
#define SEP
#define LDBG_EXECUTE(order)
const std::string kSparseweightvectorlatticeDeterminize
const std::string kSparseweightvectorlatticeLoadalilats
RelabelUtil & addIPL(typename Arc::Label labelfind, typename Arc::Label labelreplace)
Definition: fstutils.hpp:511
fst::TropicalWeightTpl< F > Map(double)
#define DR
Templated Functor to generate a sparse vector weight from any other representation.
const std::string kSparseweightvectorlatticeStripSpecialEpsilonLabels
Templated (hybrid) Interface for Task classes.
templated Mapper that modifies weights when copying from one FST to another, passing through the othe...
const std::string kRuleflowerlatticeStore
const std::string kSparseweightvectorlatticeStore
#define EPSILON
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
SparseWeightVectorLatticesTask(const ucam::util::RegistryPO &rg, const std::string &alilatskey=HifstConstants::kSparseweightvectorlatticeLoadalilats, const std::string &ruleflowerlatticekey=HifstConstants::kRuleflowerlatticeStore, const std::string &sparseweightvectorlatticekey=HifstConstants::kSparseweightvectorlatticeStore)
Constructor with registry object and keys to access/write lattices in data object.
#define OOV
Creates lattices using tropical tuple weight semiring – each arc containing separate feature weight ...
Definition: bleu.hpp:14