2008-05-27 20:50:52 +04:00
|
|
|
#ifndef __BLEUSCORER_H__
|
|
|
|
#define __BLEUSCORER_H__
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2008-10-17 01:14:38 +04:00
|
|
|
#include <limits.h>
|
2008-05-27 20:50:52 +04:00
|
|
|
#include "Types.h"
|
|
|
|
#include "ScoreData.h"
|
|
|
|
#include "Scorer.h"
|
2011-11-11 14:11:10 +04:00
|
|
|
#include "ScopedVector.h"
|
2008-05-27 20:50:52 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2008-06-24 23:27:18 +04:00
|
|
|
enum BleuReferenceLengthStrategy { BLEU_AVERAGE, BLEU_SHORTEST, BLEU_CLOSEST };
|
2008-05-27 20:50:52 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-11-12 03:58:23 +04:00
|
|
|
* Bleu scoring
|
|
|
|
*/
|
2011-02-24 15:42:19 +03:00
|
|
|
class BleuScorer: public StatisticsBasedScorer
|
|
|
|
{
|
|
|
|
public:
|
2011-11-12 04:51:27 +04:00
|
|
|
explicit BleuScorer(const string& config = "") : StatisticsBasedScorer("BLEU",config),_refLengthStrategy(BLEU_CLOSEST) {
|
2009-08-05 19:38:35 +04:00
|
|
|
//configure regularisation
|
|
|
|
static string KEY_REFLEN = "reflen";
|
|
|
|
static string REFLEN_AVERAGE = "average";
|
|
|
|
static string REFLEN_SHORTEST = "shortest";
|
|
|
|
static string REFLEN_CLOSEST = "closest";
|
2011-02-24 15:42:19 +03:00
|
|
|
|
|
|
|
|
2009-08-05 19:38:35 +04:00
|
|
|
string reflen = getConfig(KEY_REFLEN,REFLEN_CLOSEST);
|
|
|
|
if (reflen == REFLEN_AVERAGE) {
|
2011-02-24 15:42:19 +03:00
|
|
|
_refLengthStrategy = BLEU_AVERAGE;
|
2009-08-05 19:38:35 +04:00
|
|
|
} else if (reflen == REFLEN_SHORTEST) {
|
2011-02-24 15:42:19 +03:00
|
|
|
_refLengthStrategy = BLEU_SHORTEST;
|
2009-08-05 19:38:35 +04:00
|
|
|
} else if (reflen == REFLEN_CLOSEST) {
|
2011-02-24 15:42:19 +03:00
|
|
|
_refLengthStrategy = BLEU_CLOSEST;
|
2009-08-05 19:38:35 +04:00
|
|
|
} else {
|
2011-02-24 15:42:19 +03:00
|
|
|
throw runtime_error("Unknown reference length strategy: " + reflen);
|
2009-08-05 19:38:35 +04:00
|
|
|
}
|
2011-10-05 17:36:17 +04:00
|
|
|
// cerr << "Using reference length strategy: " << reflen << endl;
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
|
|
|
virtual void setReferenceFiles(const vector<string>& referenceFiles);
|
|
|
|
virtual void prepareStats(size_t sid, const string& text, ScoreStats& entry);
|
|
|
|
static const int LENGTH;
|
|
|
|
|
|
|
|
size_t NumberOfScores() {
|
2011-08-20 19:25:19 +04:00
|
|
|
// cerr << "BleuScorer: " << (2 * LENGTH + 1) << endl;
|
2011-02-24 15:42:19 +03:00
|
|
|
return (2 * LENGTH + 1);
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
|
|
|
|
2011-10-05 17:36:17 +04:00
|
|
|
//protected:
|
2011-02-24 15:42:19 +03:00
|
|
|
float calculateScore(const vector<int>& comps);
|
|
|
|
|
|
|
|
private:
|
2011-11-12 04:51:27 +04:00
|
|
|
// no copying allowed
|
2011-02-24 15:42:19 +03:00
|
|
|
BleuScorer(const BleuScorer&);
|
2011-11-12 04:40:01 +04:00
|
|
|
~BleuScorer() {}
|
2011-02-24 15:42:19 +03:00
|
|
|
BleuScorer& operator=(const BleuScorer&);
|
2011-11-12 04:51:27 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
//Used to construct the ngram map
|
|
|
|
struct CompareNgrams {
|
|
|
|
int operator() (const vector<int>& a, const vector<int>& b) {
|
|
|
|
size_t i;
|
|
|
|
size_t as = a.size();
|
|
|
|
size_t bs = b.size();
|
|
|
|
for (i = 0; i < as && i < bs; ++i) {
|
|
|
|
if (a[i] < b[i]) {
|
|
|
|
//cerr << "true" << endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (a[i] > b[i]) {
|
|
|
|
//cerr << "false" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//entries are equal, shortest wins
|
|
|
|
return as < bs;;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef map<vector<int>,int,CompareNgrams> counts_t;
|
|
|
|
typedef map<vector<int>,int,CompareNgrams>::iterator counts_it;
|
|
|
|
|
2011-11-11 14:11:10 +04:00
|
|
|
typedef ScopedVector<counts_t> refcounts_t;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Count the ngrams of each type, up to the given length in the input line.
|
|
|
|
*/
|
2011-02-24 15:42:19 +03:00
|
|
|
size_t countNgrams(const string& line, counts_t& counts, unsigned int n);
|
|
|
|
|
|
|
|
void dump_counts(counts_t& counts) {
|
|
|
|
for (counts_it i = counts.begin(); i != counts.end(); ++i) {
|
|
|
|
cerr << "(";
|
|
|
|
copy(i->first.begin(), i->first.end(), ostream_iterator<int>(cerr," "));
|
|
|
|
cerr << ") " << i->second << ", ";
|
|
|
|
}
|
|
|
|
cerr << endl;
|
|
|
|
}
|
|
|
|
BleuReferenceLengthStrategy _refLengthStrategy;
|
|
|
|
|
|
|
|
// data extracted from reference files
|
|
|
|
refcounts_t _refcounts;
|
|
|
|
vector<vector<size_t> > _reflengths;
|
2008-05-27 20:50:52 +04:00
|
|
|
};
|
|
|
|
|
2011-11-12 02:59:50 +04:00
|
|
|
#endif // __BLEUSCORER_H__
|