Cambridge SMT System
task.hifst.replacefstbyarc.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_REPLACEFSTBYARC_HPP
16 #define TASK_HIFST_REPLACEFSTBYARC_HPP
17 
23 namespace ucam {
24 namespace hifst {
25 
26 template<class Arc>
28  bool aligner_;
29  explicit GenerateTrivialFst(bool alignmode)
30  : aligner_(alignmode)
31  {};
32 
33  fst::VectorFst<Arc> *operator()(typename Arc::Label const& hieroindex) const {
34  fst::VectorFst<Arc>* outfst = new fst::VectorFst<Arc>;
35  outfst->AddState();
36  outfst->SetStart ( 0 );
37  outfst->AddState();
38  outfst->SetFinal ( 1, Arc::Weight::One() );
39  if ( aligner_ )
40  outfst->AddArc ( 0, Arc ( NORULE, hieroindex, Arc::Weight::One() , 1 ) );
41  else
42  outfst->AddArc ( 0, Arc ( hieroindex, hieroindex, Arc::Weight::One() , 1 ) );
43  return outfst;
44  };
45 };
46 
47 
53 template<class Arc = fst::LexStdArc>
55  typedef typename Arc::Label Label;
56  typedef typename Arc::Weight Weight;
57 
58  private:
60  // bool aligner_;
62  const std::size_t minns_;
64  public:
66  ReplaceFstByArc ( bool alignmode,
67  std::size_t min_numstates = 2 )
68  : minns_ ( min_numstates )
69  , gtf_(alignmode)
70  {};
71 
78  inline fst::VectorFst<Arc> *operator() ( fst::VectorFst<Arc> const& fst,
79  Label const& hieroindex ) const {
80  if ( fst.NumStates() <= minns_ ) return NULL;
81  return gtf_(hieroindex);
82  };
83 
84  private:
85  DISALLOW_COPY_AND_ASSIGN ( ReplaceFstByArc );
86 };
87 
94 template<class Arc = fst::LexStdArc>
96  typedef typename Arc::Label Label;
97  typedef typename Arc::Weight Weight;
98 
99  private:
101  // bool aligner_;
103  unordered_set<std::string> replacefstbyarc_;
104  unordered_set<std::string> replacefstbyarcexceptions_;
107  std::size_t const minns_;
109  public:
118  unordered_set<std::string> const& replacefstbyarc,
119  bool alignmode,
120  std::size_t min_numstates = std::numeric_limits<std::size_t>::max()
121  )
122  : vcat_ ( vcat )
123  , replacefstbyarc_ ( replacefstbyarc )
124  , minns_ ( min_numstates )
125  , gtf_(alignmode)
126  {};
127 
129  unordered_set<std::string> const& replacefstbyarc,
130  unordered_set<std::string> const& replacefstbyarcexceptions,
131  bool alignmode,
132  std::size_t min_numstates = std::numeric_limits<std::size_t>::max()
133  )
134  : vcat_ ( vcat )
135  , replacefstbyarc_ ( replacefstbyarc )
136  , replacefstbyarcexceptions_ ( replacefstbyarcexceptions )
137  , minns_ ( min_numstates )
138  , gtf_(alignmode)
139  {};
140 
149  inline fst::VectorFst<Arc> *operator() ( fst::VectorFst<Arc> const& fst,
150  Label const& hieroindex ) const {
151  grammar_inversecategories_t::const_iterator itx = vcat_.find ( (
152  hieroindex - APBASETAG ) / APCCTAG );
153  if ( !USER_CHECK ( itx != vcat_.end(), "Category not identified!" ) ) {
154  LERROR ( "Category id is " << ( hieroindex - APBASETAG ) / APCCTAG );
155  return NULL;
156  }
157  if (replacefstbyarcexceptions_.find (itx->second) !=
158  replacefstbyarcexceptions_.end() )
159  return NULL;
160  if ( replacefstbyarc_.find ( itx->second ) != replacefstbyarc_.end()
161  || fst.NumStates() >= minns_
162  ) {
163  return gtf_(hieroindex);
164  }
165  return NULL;
166  };
167 
168  private:
170 };
171 
172 }
173 } // end namespaces
174 
175 #endif
#define ZDISALLOW_COPY_AND_ASSIGN(TypeName)
ReplaceFstByArc(bool alignmode, std::size_t min_numstates=2)
Creates FST replacement or not depending on conditions.
#define APBASETAG
Definition: fstio.hpp:27
unordered_map< uint, std::string > grammar_inversecategories_t
fst::VectorFst< Arc > * operator()(typename Arc::Label const &hieroindex) const
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
#define NORULE
#define LERROR(msg)
ManualReplaceFstByArc(grammar_inversecategories_t const &vcat, unordered_set< std::string > const &replacefstbyarc, unordered_set< std::string > const &replacefstbyarcexceptions, bool alignmode, std::size_t min_numstates=std::numeric_limits< std::size_t >::max())
ManualReplaceFstByArc(grammar_inversecategories_t const &vcat, unordered_set< std::string > const &replacefstbyarc, bool alignmode, std::size_t min_numstates=std::numeric_limits< std::size_t >::max())
#define APCCTAG
Definition: bleu.hpp:14
Creates FST replacement or not depending on conditions including program options. ...