Cambridge SMT System
LMert.cpp
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 #include <LMert.h>
13 
14 std::vector<MertLine> const LMertAlgorithm::ComputeLatticeEnvelope (
15  TupleArcFst* vec, const PARAMS& lambda,
16  const PARAMS& direction) {
17  MertLattice lattice (vec, lambda, direction);
18  return lattice.ComputeLatticeEnvelope().lines;
19 }
20 
21 bool GradientSortPredicate (const MertLine& line1, const MertLine& line2) {
22  return line1.m < line2.m;
23 }
24 
26  sort (lines.begin(), lines.end(), GradientSortPredicate);
27 }
28 
30  SortLines();
31  int j = 0;
32  for (std::vector<MertLine>::size_type i = 0; i < lines.size(); i++) {
33  MertLine l = lines[i];
34  l.x = -std::numeric_limits<double>::infinity();
35  if (0 < j) {
36  if (lines[j - 1].m == l.m) {
37  if (l.y <= lines[j - 1].y)
38  continue;
39  --j;
40  }
41  while (0 < j) {
42  l.x = (l.y - lines[j - 1].y) / (lines[j - 1].m - l.m);
43  if (lines[j - 1].x < l.x)
44  break;
45  --j;
46  }
47  if (0 == j)
48  l.x = -std::numeric_limits<double>::infinity();
49  lines[j++] = l;
50  } else {
51  lines[j++] = l;
52  }
53  }
54  lines.resize (j);
55 }
56 
57 std::string MertEnvelope::ToString (bool show_hypothesis = false) {
58  std::ostringstream oss;
59  for (std::vector<MertLine>::size_type i = 0; i < lines.size(); ++i) {
60  oss << "line i=[" << std::right << std::setw (4) << i << "]" << std::fixed
61  << std::setprecision (6) << " x=[" << std::right << std::setw (12) << lines[i].x
62  << "]" << " y=[" << std::right << std::setw (12) << lines[i].y << "]"
63  << " m=[" << std::right << std::setw (12) << lines[i].m << "]";
64  if (show_hypothesis) {
65  oss << " t=[" << lines[i].t << "]";
66  }
67  oss << '\n';
68  }
69  return oss.str();
70 }
71 
73  const PARAMS& direction) :
74  fst (fst), lambda (lambda), direction (direction) {
75 }
76 
78  envelopes.resize (fst->NumStates() + 1);
79 }
80 
82  envelopes[fst->Start()].lines.push_back (MertLine() );
83 }
84 
85 void MertLattice::PropagateEnvelope (const TupleArcFst::StateId& src,
86  const TupleArcFst::StateId& trg, const TupleW& features, const Wid& w = 0) {
87  for (unsigned int i = 0; i < envelopes[src].lines.size(); ++i) {
88  MertLine line (envelopes[src].lines[i]);
89  line.y += DotProduct (features, lambda) * -1;
90  line.m += DotProduct (features, direction) * -1;
91  if (w != 0) {
92  line.t.push_back (w);
93  }
94  envelopes[trg].lines.push_back (line);
95  }
96 }
97 
99  for (fst::StateIterator < TupleArcFst > si (*fst); !si.Done(); si.Next() ) {
100  const TupleArc::StateId& s = si.Value();
101  envelopes[s].SweepLine();
102  for (fst::ArcIterator < TupleArcFst > ai (*fst, si.Value() ); !ai.Done();
103  ai.Next() ) {
104  const TupleArc& a = ai.Value();
105  PropagateEnvelope (s, a.nextstate, a.weight, a.ilabel);
106  }
107  if (fst->Final (s) != TupleW::Zero() ) {
108  PropagateEnvelope (s, fst->NumStates(), fst->Final (s) );
109  }
110  envelopes[s].lines.clear();
111  }
112 }
113 
115  envelopes[fst->NumStates()].SweepLine();
116 }
117 
123  return envelopes[fst->NumStates()];
124 }
long long Wid
Definition: fstio.hpp:27
void PropagateEnvelope(const TupleArcFst::StateId &, const TupleArcFst::StateId &, const TupleW &, const Wid &w)
Definition: LMert.cpp:85
void InitializeEnvelopes()
Definition: LMert.cpp:77
void InitializeStartState()
Definition: LMert.cpp:81
Implements Tropical Sparse tuple weight semiring, extending from openfst SparsePowerWeight class...
MertLattice(TupleArcFst *, const PARAMS &, const PARAMS &)
Definition: LMert.cpp:72
void ComputeFinalEnvelope()
Definition: LMert.cpp:114
void ComputeStateEnvelopes()
Definition: LMert.cpp:98
std::vector< MertLine > lines
Definition: LMert.h:30
void SweepLine()
Definition: LMert.cpp:29
SentenceIdx t
MertEnvelope ComputeLatticeEnvelope()
Definition: LMert.cpp:118
static Lines const ComputeLatticeEnvelope(TupleArcFst *, const PARAMS &, const PARAMS &)
Definition: LMert.cpp:14
fst::VectorFst< TupleArc > TupleArcFst
std::string ToString(bool)
Definition: LMert.cpp:57
static const TropicalSparseTupleWeight< T > & Zero()
void SortLines()
Definition: LMert.cpp:25
T DotProduct(const TropicalSparseTupleWeight< T > &w, const std::vector< T > &vw)
Implements Dot product of two vector weights.
bool GradientSortPredicate(const MertLine &line1, const MertLine &line2)
Definition: LMert.cpp:21