Cambridge SMT System
TGMert.cpp
Go to the documentation of this file.
1 //Copyright (c) 2011, 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 "TGMert.h"
13 #include "function-weight.h"
14 #include "fast-shortest-distance.h"
15 #include "mapping-shortest-path.h"
16 
17 void const ComputeShortestPath (const fst::VectorFst<FunctionArc>& fst,
18  double x, MertLine& line) {
19  fst::VectorFst < fst::ArcTpl<fst::TropicalWeightTpl<double> > > pathFst;
20  mertfst::SingleShortestPath (fst, &pathFst, x);
21  TopSort (&pathFst);
22  for (fst::StateIterator
23  < fst::VectorFst<fst::ArcTpl<fst::TropicalWeightTpl<double> > >
24  > siter (pathFst); !siter.Done(); siter.Next() ) {
25  for (fst::ArcIterator
26  < fst::VectorFst<fst::ArcTpl<fst::TropicalWeightTpl<double> > >
27  > aiter (pathFst, siter.Value() ); !aiter.Done(); aiter.Next() ) {
28  line.t.push_back (aiter.Value().olabel);
29  line.score += aiter.Value().weight.Value();
30  }
31  }
32 }
33 
34 MertList ComputeFromFunctionArc (const fst::VectorFst<FunctionArc>& fst) {
36  // Need to handle first case.
37  const MertList& mlconst = sd.Value();
38  // Hacky but am really getting fed up with all constness
39  // and don't want to keep on copying this list
40  MertList& ml = const_cast<MertList&> (mlconst);
41  if (ml.size() == 1) {
42  ComputeShortestPath (fst, 0, ml.front() );
43  } else {
44  double x = (++ml.begin() )->x;
45  ComputeShortestPath (fst, x - 1, ml.front() );
46  MertIter prev = ++ml.begin();
47  for (MertIter next =
48  ++ (++ml.begin() ); next != sd.Value().end(); ++next) {
49  // Sometimes can end up with a very large gamma interval in the region of 1e16. When this happens the
50  // midpoint is also huge. Any shortest path computations get corrupted for that interval. Therefore use this
51  // little hack to protect ourselves from it. The value of 1000 is a magic number and has no significance
52  // other than it empirically seems OK.
53  double midpoint;
54  if ( (next->x - prev->x) / 2 > 1000) {
55  if (abs (prev->x) < abs (next->x) ) {
56  midpoint = prev->x + 1000;
57  } else {
58  midpoint = next->x - 1000;
59  }
60  } else {
61  //Do normal midpoint calc
62  midpoint = (prev->x + next->x) / 2.0;
63  }
64  ComputeShortestPath (fst, midpoint, *prev);
65  ++prev;
66  }
67  ComputeShortestPath (fst, prev->x + 1, ml.back() );
68  }
69  return ml;
70 }
void SingleShortestPath(const fst::Fst< FArc > &ifst, fst::MutableFst< TArc > *ofst, F gamma)
Definition: fstio.hpp:27
MertList::iterator MertIter
MertList ComputeFromFunctionArc(const fst::VectorFst< FunctionArc > &fst)
Definition: TGMert.cpp:34
const MertList & Value() const
double score
void const ComputeShortestPath(const fst::VectorFst< FunctionArc > &fst, double x, MertLine &line)
Definition: TGMert.cpp:17
std::list< MertLine > MertList
SentenceIdx t
void ShortestDistance(const fst::Fst< Arc > &fst, std::vector< typename Arc::Weight > *distance, const ShortestDistanceOptions< Arc, Queue, ArcFilter > &opts)