Cambridge SMT System
Score.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 SCORE_H_
13 #define SCORE_H_
14 
15 #include <BleuStats.h>
16 #include "TuneSet.h"
17 #include "function-weight.h"
18 
20 
21 template<typename RefData>
22 typename RefData::ErrorStats::Error ComputeError (RefData& refData,
23  const TuneSet& lats, const PARAMS& lambda) {
24  typename RefData::ErrorStats aggregate;
25  for (std::vector<Sid>::const_iterator sit = lats.ids.begin();
26  sit != lats.ids.end(); ++sit) {
27  if (opts.verbose) {
28  tracer << "sentence s=" << *sit << '\n';
29  }
31  if (!fst) {
32  cerr << "ERROR: invalid vector lattice for sentence s=" << *sit
33  << '\n';
34  exit (1);
35  }
36  Sentence h = GetBestHypothesis (fst, lambda);
37  typename RefData::ErrorStats es = refData.ComputeErrorStats (*sit, h);
38  if (opts.verbose) {
39  tracer << "best hypothesis=" << h << " " << es << '\n';
40  }
41  aggregate = aggregate + es;
42  if (!opts.useCache) {
43  delete fst;
44  }
45  }
46  return aggregate.ComputeError();
47 }
48 
49 template<typename RefData>
50 std::string Score (RefData& refData, const TuneSet& lats,
51  const PARAMS& lambda) {
52  typename RefData::ErrorStats::Error error = ComputeError<RefData> (refData,
53  lats, lambda);
54  std::stringstream ss;
55  ss << error;
56  return ss.str();
57 }
58 
59 class Scorer {
60  public:
61  virtual std::string operator() (const std::vector<std::string>& refFilenames,
62  const TuneSet& lats,
63  const PARAMS& lambda) = 0;
64  virtual ~Scorer() {
65  }
66  ;
67 };
68 
69 template<typename RefData>
70 class ScorerImpl: public Scorer {
71 
72  virtual std::string operator() (const std::vector<std::string>& refFilenames,
73  const TuneSet& lats,
74  const PARAMS& lambda) {
75  RefData refData;
76  refData.LoadRefData (refFilenames);
77  return Score (refData, lats, lambda);
78  }
79 };
80 
81 #endif /* SCORE_H_ */
MertOpt opts
Definition: MertCommon.cpp:14
RefData::ErrorStats::Error ComputeError(RefData &refData, const TuneSet &lats, const PARAMS &lambda)
Definition: Score.h:22
bool useCache
Definition: MertCommon.h:37
Definition: fstio.hpp:27
#define tracer
Definition: data.lmbr.hpp:18
Definition: Score.h:59
TupleArcFst * GetVectorLattice(const Sid s, const bool use_cache) const
Definition: TuneSet.cpp:87
bool verbose
Definition: MertCommon.h:33
virtual ~Scorer()
Definition: Score.h:64
std::vector< Wid > Sentence
Definition: MertCommon.h:48
SentenceList ids
Definition: TuneSet.h:34
Sentence GetBestHypothesis(const TupleArcFst *, const PARAMS &)
Definition: Score.cpp:33
std::string Score(RefData &refData, const TuneSet &lats, const PARAMS &lambda)
Definition: Score.h:50
fst::VectorFst< TupleArc > TupleArcFst
virtual std::string operator()(const std::vector< std::string > &refFilenames, const TuneSet &lats, const PARAMS &lambda)=0