Cambridge SMT System
Score.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 "Score.h"
13 
15  fst::VectorFst<fst::ArcTpl<FeatureWeight> >* fst) {
16  fst::VectorFst<fst::ArcTpl<FeatureWeight> > fsttmp1;
17  fst::ShortestPath (*fst, &fsttmp1);
18  fst::Project (&fsttmp1, fst::PROJECT_INPUT);
19  fst::RmEpsilon (&fsttmp1);
20  fst::TopSort (&fsttmp1);
21  Sentence best;
22  for (fst::StateIterator<fst::VectorFst<fst::ArcTpl<FeatureWeight> > > si (
23  fsttmp1);
24  !si.Done(); si.Next() ) {
25  for (fst::ArcIterator<fst::VectorFst<fst::ArcTpl<FeatureWeight> > > ai (fsttmp1,
26  si.Value() ); !ai.Done(); ai.Next() ) {
27  best.push_back (ai.Value().ilabel);
28  }
29  }
30  return best;
31 }
32 
33 Sentence GetBestHypothesis (const TupleArcFst* fst, const PARAMS& lambda) {
34  fst::VectorFst<fst::ArcTpl<FeatureWeight> > * fsttmp1 = new fst::VectorFst <
35  fst::ArcTpl<FeatureWeight> > ();
36  fst::DotProductMap<double> m (lambda);
37  Map (
38  *fst,
39  fsttmp1,
40  fst::GeneralMapper<TupleArc, fst::ArcTpl<FeatureWeight>, fst::DotProductMap<double> >
41  (
42  m) );
43  fst::VectorFst<fst::ArcTpl<FeatureWeight> > *fsttmp2 = new fst::VectorFst <
44  fst::ArcTpl<FeatureWeight> >;
45  ShortestPath (*fsttmp1, fsttmp2);
46  delete fsttmp1;
47  Sentence h = GetBestHypothesisTokens (fsttmp2);
48  delete fsttmp2;
49  int offset;
50  h.size() < 2 ? offset = 0 : offset = 1;
51  Sentence s = NGram (h.begin() + offset,
52  h.end() - offset); // Trim sentence start and end markers from hypothesis
53  return s;
54 }
55 
Sentence GetBestHypothesis(const TupleArcFst *fst, const PARAMS &lambda)
Definition: Score.cpp:33
Definition: fstio.hpp:27
fst::TropicalWeightTpl< F > Map(double)
std::vector< Wid > Sentence
Definition: MertCommon.h:48
std::vector< Wid > NGram
Definition: MertCommon.h:47
fst::VectorFst< TupleArc > TupleArcFst
Map functor used with generic weight mapper.
Sentence GetBestHypothesisTokens(const fst::VectorFst< fst::ArcTpl< FeatureWeight > > *fst)
Definition: Score.cpp:14