Cambridge SMT System
task.hifst.makeweights.hpp
Go to the documentation of this file.
1 #ifndef TASK_HIFST_MAKEWEIGHTS_HPP
2 #define TASK_HIFST_MAKEWEIGHTS_HPP
3 
4 namespace ucam {
5 namespace hifst {
6 
7 // @todo: this codes needs to be merged with other makeweight functors.
8 // General template class. This returns error.
9 template<class Arc>
11  typedef typename Arc::Weight Weight;
13  LERROR("MakeWeightHifst not implemented for this semiring");
14  exit(EXIT_FAILURE);
15  }
16  inline Weight operator () (float const weight ) const {
17  return Weight ( weight );
18  };
19  inline Weight operator () (Weight const & weight) const {
20  return weight ;
21  };
22  inline Weight operator () (float const weight, unsigned) const {
23  return Weight(weight);
24  };
25 
26  inline void update() {};
27 
28 };
29 
30 // Template specialization for Lexicographic StdArc,StdArc.
31 // This is essentially the logic in lexicographic-tropical-tropical-funcs.h
32 template<>
35  typedef Arc::Weight Weight;
37  }
38 
39  inline Weight operator () (float const weight ) const {
40  return Weight ( weight, weight);
41  };
42  inline Weight operator () (Weight const & weight) const {
43  return Weight( weight.Value2(), weight.Value2() );
44  };
45  inline Weight operator () (float const weight, unsigned) const {
46  return Weight(weight, weight);
47  };
48  inline void update() {};
49 };
50 
51 // Template specialization for TupleArc
52 template<>
54  typedef TupleArc32 Arc;
55  typedef Arc::Weight Weight;
56 
58  // sparse feature index.
59  // +1 because sparse tuple semiring starts at 1, not 0.
60  unsigned k_;
61  // Assumptions: the tropical sparse tuple semiring
62  // will carry first the lm weights, then a single grammar weight (dot product)
63  // in addition, rule indices are passed as features (negative indices, get
64  // ignored by the dot product).
66  : addRuleFeature_(!rg.getBool(HifstConstants::kHifstDisableRuleFeatures))
67  , k_(rg.getVectorString(HifstConstants::kLmFeatureweights).size() + 1)
68  {}
69 
70  inline Weight operator () (float const weight ) const {
71  Weight result;
72  result.Push ( k_, weight);
73  return result;
74  };
75 
76  inline Weight operator () (float const weight, unsigned spi ) const {
77  Weight result;
78  result.Push ( k_ , weight);
79  if (addRuleFeature_)
80  result.Push( -spi - 2, 1);
81  return result;
82  };
83 
84  inline Weight operator () (Weight const &weight) const {
85  return weight;
86  };
87 
88  inline void update() {};
89 };
90 
91 // General template class. This returns error.
92 template<class Arc>
94  typedef typename Arc::Weight Weight;
95  explicit MakeWeightHifstLocalLm() {}
97  LERROR("MakeWeightHifstLocalLm not implemented for this semiring");
98  exit(EXIT_FAILURE);
99  }
100  inline Weight operator () (float const weight ) const {
101  return Weight ( weight );
102  };
103  inline Weight operator () (Weight const & weight) const {
104  return weight ;
105  };
106  inline Weight operator () (float const weight, unsigned) const {
107  return Weight(weight);
108  };
109  inline void update() {};
110 };
111 
112 // Template specialization for Lexicographic StdArc,StdArc.
113 // This is essentially the logic in lexicographic-tropical-tropical-funcs.h
114 template<>
117  typedef Arc::Weight Weight;
118  typedef fst::StdArc::Weight StdWeight;
121  }
122 
123  // Language Model uses this one:
124  inline Weight operator () (float const weight ) const {
125  return Weight ( weight, StdWeight::One());
126  };
127  inline Weight operator () (float const weight, unsigned) const {
128  return Weight(weight, StdWeight::One() );
129  };
130 
131  // copies back the second cost into the first one.
132  inline Weight operator () (Weight const & weight) const {
133  return Weight( weight.Value2(), weight.Value2() );
134  };
135  inline void update() {};
136 };
137 
138 // singleton-like function to hack in the Mapper state the actual
139 // position of the local lm in tuple arc 32.
140 // Assumptions: the tropical sparse tuple semiring
141 // will carry first the lm weights, then a single grammar weight (dot product)
142 // the next feature weight is the local language model.
143 // offset +1
144 inline int getLocalLmIndex(ucam::util::RegistryPO const *rg = NULL) {
145  static int k = -1;
146  if (rg != NULL)
147  k = rg->getVectorString(HifstConstants::kLmFeatureweights).size() + 1 + 1;
148  return k;
149 };
150 
151 // Map does not copy the mapper functor, so an internal variable will not get
152 // initialized properly. Hence, using getLocalLmIndex to initialize.
153 // Template specialization for TupleArc
154 template<>
156  typedef TupleArc32 Arc;
157  typedef Arc::Weight Weight;
158 
159  unsigned k_;
161  : k_(getLocalLmIndex())
162  {
163  if (!k_) {
164  LERROR("MakeWeightHifstLocalLm -- Index has to be bigger than 0 (constr)");
165  exit(EXIT_FAILURE);
166  }
167  }
169  : k_(getLocalLmIndex(&rg))
170  {}
171 
172  // More than one language model will add scores to the same k_
173  inline Weight operator () (float const weight ) const {
174  if (!k_) {
175  LERROR("MakeWeightHifstLocalLm -- Index has to be bigger than 0 (op)");
176  exit(EXIT_FAILURE);
177  }
178  Weight result;
179  result.Push ( k_, weight);
180  return result;
181  };
182  // deletes lm scores under tropical sparse tuple weights
183  // not as effective as lexicographic semiring, but should do the job.
184  inline Weight operator () (Weight const &weight) const {
185  if (!k_) {
186  LERROR("MakeWeightHifstLocalLm -- Index has to be bigger than 0! (op 2)");
187  exit(EXIT_FAILURE);
188  }
189  using namespace fst;
190  Weight result;
191  result.SetDefaultValue(weight.DefaultValue());
192  for (SparseTupleWeightIterator<StdArc::Weight, int> it(weight); !it.Done(); it.Next()) {
193  if (it.Value().first != k_)
194  result.Push(it.Value());
195  }
196  return result;
197  };
198 
199  // local language model always working on the default value(s)
200  inline void update() {};
201 };
202 
203 }} // end namespaces
204 
205 #endif
MakeWeightHifst(const ucam::util::RegistryPO &rg)
Definition: fstio.hpp:27
const std::string kHifstDisableRuleFeatures
int getLocalLmIndex(ucam::util::RegistryPO const *rg=NULL)
Weight operator()(float const weight) const
std::string const kLmFeatureweights
MakeWeightHifstLocalLm(const ucam::util::RegistryPO &rg)
fst::ArcTpl< TupleW32 > TupleArc32
LexicographicArc< StdArc::Weight, StdArc::Weight > LexStdArc
#define LERROR(msg)
Definition: bleu.hpp:14
MakeWeightHifst(const ucam::util::RegistryPO &rg)