Cambridge SMT System
BleuStats.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 "BleuStats.h"
13 #include <cmath>
14 #include <cstddef>
15 using std::min;
16 using std::log;
17 
18 const unsigned int BleuStats::MAX_BLEU_ORDER = 4;
19 
20 BleuStats::BleuStats() : refLength (0) {
21  tots.resize (MAX_BLEU_ORDER, 0);
22  hits.resize (MAX_BLEU_ORDER, 0);
23 }
24 
25 BleuStats::Error BleuStats::ComputeError (const unsigned int order) const {
26  double logBleu = 0.0;
27  double logBrev = 0.0;
28  for (unsigned int n = 0; n < order; ++n) {
29  logBleu += log ( (double) this->hits[n] / (double) this->tots[n]);
30  }
31  logBleu *= 1 / (double) BleuStats::MAX_BLEU_ORDER;
32  logBrev = min (0.0, 1 - this->refLength / (double) (this->tots[0]) );
33  return Error (exp (logBleu + logBrev), exp (logBrev) );
34 }
35 
36 ostream& operator<< (ostream& o, const BleuStats& b) {
37  for (unsigned int n = 0; n < BleuStats::MAX_BLEU_ORDER; ++n) {
38  if (n > 0) {
39  o << "\t";
40  }
41  o << b.hits[n] << "\t" << b.tots[n];
42  }
43  o << "\t" << b.refLength;
44  return o;
45 }
46 
47 BleuStats operator+ (const BleuStats& bs1, const BleuStats& bs2) {
48  BleuStats bs;
49  for (unsigned int n = 0; n < BleuStats::MAX_BLEU_ORDER; ++n) {
50  bs.hits[n] = bs1.hits[n] + bs2.hits[n];
51  bs.tots[n] = bs1.tots[n] + bs2.tots[n];
52  }
53  bs.refLength = bs1.refLength + bs2.refLength;
54  return bs;
55 }
56 
57 BleuStats operator- (const BleuStats& bs1, const BleuStats& bs2) {
58  BleuStats bs;
59  bs.refLength = bs1.refLength - bs2.refLength;
60  for (unsigned int n = 0; n < BleuStats::MAX_BLEU_ORDER; ++n) {
61  bs.tots[n] = bs1.tots[n] - bs2.tots[n];
62  bs.hits[n] = bs1.hits[n] - bs2.hits[n];
63  }
64  return bs;
65 }
66 
67 ostream& operator<< (ostream& o, const Bleu& b) {
68  o << b.m_bleu << " " << b.m_brev;
69  return o;
70 }
71 
72 bool operator> (Bleu& b1, Bleu& b2) {
73  return b1.m_bleu > b2.m_bleu;
74 }
75 
vector< int > hits
Definition: BleuStats.h:46
vector< int > tots
Definition: BleuStats.h:45
long refLength
Definition: BleuStats.h:47
BleuStats operator+(const BleuStats &bs1, const BleuStats &bs2)
Definition: BleuStats.cpp:47
Bleu Error
Definition: BleuStats.h:43
static const unsigned int MAX_BLEU_ORDER
Definition: BleuStats.h:50
Definition: BleuStats.h:22
BleuStats operator-(const BleuStats &bs1, const BleuStats &bs2)
Definition: BleuStats.cpp:57
Error ComputeError(const unsigned int=4) const
Definition: BleuStats.cpp:25
bool operator>(Bleu &b1, Bleu &b2)
Definition: BleuStats.cpp:72
ostream & operator<<(ostream &o, const BleuStats &b)
Definition: BleuStats.cpp:36