Cambridge SMT System
function-weight.h
Go to the documentation of this file.
1 //Copyright (c) 2012, University of Cambridge
2 //All rights reserved.
3 //
4 //Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met://
5 //
6 // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8 // * Neither the name of the University of Cambridge nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9 //
10 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 
12 #ifndef FUNCTION_WEIGHT_H_
13 #define FUNCTION_WEIGHT_H_
14 
15 #include <fst/fstlib.h>
16 #include <list>
17 #include <vector>
18 #include <limits>
19 #include <iterator>
20 #include <ostream>
21 
22 typedef long long Wid;
23 
24 typedef std::vector<Wid> SentenceIdx;
25 
26 typedef double F;
27 
28 class MertLine {
29  public:
31  x (-std::numeric_limits<double>::infinity() ), y (0.0), m (0.0) {
32  }
33 
34  MertLine (double y, double m, Wid word) :
35  x (-std::numeric_limits<double>::infinity() ), y (y), m (m) {
36  //t.push_back(word);
37  }
38  double x; // x-intercept of left-adjacent line
39  double y; // y-intercept of line
40  double m; // slope of line
41  SentenceIdx t; // partial translation hypothesis associated with line
42  double score; //translation score
43 };
44 
45 typedef std::list<MertLine> MertList;
46 typedef MertList::iterator MertIter;
47 
48 const MertLine zeroLine (std::numeric_limits<double>::infinity(), 0, 0);
49 const MertLine oneLine (0, 0, 0);
50 
51 static MertList InitFW (MertLine l) {
52  MertList ml;
53  ml.push_back (l);
54  return ml;
55 }
56 
57 const MertList zeroList = InitFW (zeroLine);
58 const MertList oneList = InitFW (oneLine);
59 
60 std::ostream& operator<< (std::ostream&, const MertLine&);
61 
62 std::ostream& operator<< (std::ostream&, const MertList&);
63 
64 std::istream& operator>> (std::istream&, MertList&);
65 
66 bool operator== (const MertLine&, const MertLine&);
67 
68 /*
69  * Holds two sorted lists and returns the correct value
70  */
72 
73  public:
74  CompositeList (const MertList& l1, const MertList& l2);
75 
76  class cl_iterator: std::iterator<std::input_iterator_tag, MertLine> {
77  public:
79  MertLine& operator*();
80  cl_iterator& operator++();
81  cl_iterator operator++ (int);
82  bool operator== (const cl_iterator&);
83  bool operator!= (const cl_iterator&);
84 
85  private:
86  MertIter i1;
87  MertIter i2;
88  MertIter i1end;
89  MertIter i2end;
90  int currList;
91  MertIter currIter;
92  bool isEnd() const;
93  };
94 
95  cl_iterator begin();
96  cl_iterator end();
97 
98  private:
99  MertList l1;
100  MertList l2;
101 };
102 
104 
106  public:
107 
109 
110  static const FunctionWeight& Zero() {
111  static FunctionWeight zero (zeroList);
112  return zero;
113  }
114 
115  static const FunctionWeight& One() {
116  static FunctionWeight one (oneList);
117  return one;
118  }
119 
121  values (FunctionWeight::Zero().values) {
122  }
123 
125  values (values) {
126  }
127 
128  FunctionWeight (const MertList& values) {
129  this->values = values;
130  }
131 
132  const MertList& Value() const {
133  return values;
134  }
135 
136  bool operator== (const FunctionWeight&) const;
137 
138  bool operator!= (const FunctionWeight&) const;
139 
141  fst::DivideType t = fst::DIVIDE_ANY);
142 
143  std::size_t Hash() const;
144 
145  bool Member() const;
146 
147  ReverseWeight Reverse() const;
148 
149  std::istream& Read (std::istream& strm);
150 
151  std::ostream& Write (std::ostream& strm) const;
152 
153  fst::TropicalWeightTpl<F> Map (F) const;
154 
155  static uint64 Properties() {
156  return fst::kLeftSemiring | fst::kRightSemiring | fst::kCommutative |
157  fst::kIdempotent;
158  }
159  ;
160 
161  friend std::istream& operator>> (std::istream& strm, FunctionWeight& w);
162 
163  friend FunctionWeight Plus (const FunctionWeight&, const FunctionWeight&);
164 
165  friend FunctionWeight Times (const FunctionWeight&, const FunctionWeight&);
166 
167  static const std::string& Type() {
168  static const std::string type = "Function";
169  return type;
170  }
171 
172  private:
173  MertList values;
174 
175 };
176 
177 bool ApproxEqual (const FunctionWeight&, const FunctionWeight&, float);
178 
180 
182 
183 std::ostream& operator<< (std::ostream& strm, const FunctionWeight& w);
184 
185 fst::TropicalWeightTpl<F> Map (double);
186 
187 struct FunctionArc {
188  typedef int Label;
190  typedef int StateId;
191 
192  // Vector arc initialised from a function weight
193  FunctionArc (Label i, Label o, Weight w, StateId s) :
194  ilabel (i), olabel (o), weight (w), nextstate (s) {
195  }
196 
198  }
199 
200  static const std::string& Type() {
201  static const std::string type = "Function";
202  return type;
203  }
204 
205  Label ilabel; // Transition input label
206  Label olabel; // Transition output label
207  Weight weight; // Transition weight
208  StateId nextstate; // Transition destination state
209 };
210 
211 template<class W, unsigned int n>
212 F DotProduct (const W&, const fst::TupleWeight<fst::TropicalWeightTpl<F>, n>&);
213 
214 template<unsigned int n>
215 F DotProduct (const fst::TupleWeight<fst::TropicalWeightTpl<F>, n>& features
216  , const std::vector<F>& params) {
217  F result = 0.0;
218  for (unsigned int i = 0; i < features.Length(); ++i) {
219  result += features.Value (i).Value() * params[i];
220  }
221  return result;
222 }
223 
224 template<class FromArc, class T>
226 
227  public:
229  typedef typename FromArc::Weight FW; // From weight (vector)
230  typedef ToArc::Weight TW; // To weight (Function)
231  explicit VectorToFunctionMapper (const std::vector<T>& direction,
232  const std::vector<T>& initial) :
233 
234  direction (direction), initial (initial) {
235  }
236  ToArc operator() (const FromArc& arc) const {
237  //FW features = arc.weight.Value();
238  FW features = arc.weight;
239  if (features == FW::Zero() ) {
240  return ToArc (arc.ilabel, arc.olabel, TW::Zero(), arc.nextstate);
241  }
242  if (features == FW::One() ) {
243  return ToArc (arc.ilabel, arc.olabel, TW::One(), arc.nextstate);
244  }
245  F m = DotProduct (features, direction);
246  F y = DotProduct (features, initial);
247  MertList mlist;
248  mlist.push_back (MertLine (y, m, arc.ilabel) );
249  // cout << mlist << endl;
250  TW mapped (mlist);
251  return ToArc (arc.ilabel, arc.olabel, mapped, arc.nextstate);
252  }
253  fst::MapSymbolsAction InputSymbolsAction() const {
254  return fst::MAP_COPY_SYMBOLS;
255  }
256  fst::MapSymbolsAction OutputSymbolsAction() const {
257  return fst::MAP_COPY_SYMBOLS;
258  }
259  fst::MapFinalAction FinalAction() const {
260  return fst::MAP_NO_SUPERFINAL;
261  }
262  uint Properties (uint props) const {
263  return props;
264  }
265 
266  private:
267 
268  const std::vector<T>& direction;
269 
270  const std::vector<T>& initial;
271 
272 };
273 
275 
276  public:
278  typedef fst::ArcTpl<fst::TropicalWeightTpl<double> > ToArc;
279  typedef FromArc::Weight FW; // From weight (Function)
280  typedef ToArc::Weight TW; // To weight (Std)
281  explicit FunctionToStdMapper (double gamma) :
282  gamma (gamma) {
283  }
284  ToArc operator() (const FromArc& arc) const {
285  const MertList& function = arc.weight.Value();
286  if (arc.weight.Value() == FW::Zero().Value() ) {
287  return ToArc (arc.ilabel, arc.olabel, TW::Zero(), arc.nextstate);
288  }
289  if (arc.weight.Value() == FW::One().Value() ) {
290  return ToArc (arc.ilabel, arc.olabel, TW::One(), arc.nextstate);
291  }
292  if (function.size() != 1) {
293  cout << "Function arc has more than one function";
294  exit (1);
295  }
296  TW mapped (function.front().m * gamma + function.front().y);
297  return ToArc (arc.ilabel, arc.olabel, mapped, arc.nextstate);
298  }
299  fst::MapSymbolsAction InputSymbolsAction() const {
300  return fst::MAP_COPY_SYMBOLS;
301  }
302  fst::MapSymbolsAction OutputSymbolsAction() const {
303  return fst::MAP_COPY_SYMBOLS;
304  }
305  fst::MapFinalAction FinalAction() const {
306  return fst::MAP_NO_SUPERFINAL;
307  }
308  uint Properties (uint props) const {
309  return props;
310  }
311 
312  private:
313 
314  double gamma;
315 
316 };
317 
318 #endif /* FUNCTION_WEIGHT_H_ */
long long Wid
FunctionWeight Plus(const FunctionWeight &, const FunctionWeight &)
static uint64 Properties()
fst::ArcTpl< fst::TropicalWeightTpl< double > > ToArc
bool operator==(const MertLine &, const MertLine &)
const MertList oneList
MertList::iterator MertIter
uint Properties(uint props) const
const MertLine oneLine(0, 0, 0)
bool ApproxEqual(const FunctionWeight &, const FunctionWeight &, float)
StateId nextstate
fst::TropicalWeightTpl< F > Map(double)
fst::MapFinalAction FinalAction() const
double F
static const FunctionWeight & Zero()
fst::MapSymbolsAction OutputSymbolsAction() const
fst::MapFinalAction FinalAction() const
VectorToFunctionMapper(const std::vector< T > &direction, const std::vector< T > &initial)
FunctionArc(Label i, Label o, Weight w, StateId s)
const MertLine zeroLine(std::numeric_limits< double >::infinity(), 0, 0)
fst::MapSymbolsAction OutputSymbolsAction() const
const MertList & Value() const
FunctionWeight ReverseWeight
TropicalSparseTupleWeight< T > Divide(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2, DivideType type=DIVIDE_ANY)
double score
static const FunctionWeight & One()
static const std::string & Type()
fst::MapSymbolsAction InputSymbolsAction() const
FunctionWeight(MertList &values)
std::ostream & operator<<(std::ostream &, const MertLine &)
FunctionWeight Weight
fst::MapSymbolsAction InputSymbolsAction() const
FromArc::Weight FW
static const std::string & Type()
std::list< MertLine > MertList
SentenceIdx t
uint Properties(uint props) const
std::vector< Wid > SentenceIdx
FunctionWeight Times(const FunctionWeight &, const FunctionWeight &)
CompositeList::cl_iterator CompIter
FunctionToStdMapper(double gamma)
const MertList zeroList
std::istream & operator>>(std::istream &, MertList &)
MertLine(double y, double m, Wid word)
F DotProduct(const W &, const fst::TupleWeight< fst::TropicalWeightTpl< F >, n > &)
FunctionWeight(const MertList &values)