2012-02-20 04:46:08 +04:00
|
|
|
#ifndef MERT_BLEU_SCORER_H_
|
|
|
|
#define MERT_BLEU_SCORER_H_
|
2008-05-27 20:50:52 +04:00
|
|
|
|
2012-02-25 21:01:03 +04:00
|
|
|
#include <ostream>
|
2008-05-27 20:50:52 +04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2011-11-14 10:15:30 +04:00
|
|
|
|
2008-05-27 20:50:52 +04:00
|
|
|
#include "Types.h"
|
|
|
|
#include "ScoreData.h"
|
2012-06-24 06:51:48 +04:00
|
|
|
#include "StatisticsBasedScorer.h"
|
2011-11-11 14:11:10 +04:00
|
|
|
#include "ScopedVector.h"
|
2008-05-27 20:50:52 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
|
|
|
|
2012-03-09 21:49:31 +04:00
|
|
|
const int kBleuNgramOrder = 4;
|
2008-05-27 20:50:52 +04:00
|
|
|
|
2012-03-14 17:14:11 +04:00
|
|
|
class NgramCounts;
|
2012-11-04 19:43:36 +04:00
|
|
|
class Reference;
|
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:
|
2012-03-19 17:45:15 +04:00
|
|
|
enum ReferenceLengthType {
|
|
|
|
AVERAGE,
|
|
|
|
CLOSEST,
|
|
|
|
SHORTEST
|
|
|
|
};
|
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
explicit BleuScorer(const std::string& config = "");
|
2011-11-12 05:16:31 +04:00
|
|
|
~BleuScorer();
|
2012-11-04 19:43:36 +04:00
|
|
|
|
|
|
|
static std::vector<float> ScoreNbestList(const std::string& scoreFile, const std::string& featureFile);
|
2011-11-12 05:16:31 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
virtual void setReferenceFiles(const std::vector<std::string>& referenceFiles);
|
|
|
|
virtual void prepareStats(std::size_t sid, const std::string& text, ScoreStats& entry);
|
2012-06-24 06:51:48 +04:00
|
|
|
virtual statscore_t calculateScore(const std::vector<int>& comps) const;
|
2013-05-29 21:16:15 +04:00
|
|
|
virtual std::size_t NumberOfScores() const {
|
|
|
|
return 2 * kBleuNgramOrder + 1;
|
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
int CalcReferenceLength(std::size_t sentence_id, std::size_t length);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
ReferenceLengthType GetReferenceLengthType() const {
|
|
|
|
return m_ref_length_type;
|
|
|
|
}
|
|
|
|
void SetReferenceLengthType(ReferenceLengthType type) {
|
|
|
|
m_ref_length_type = type;
|
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
const std::vector<Reference*>& GetReferences() const {
|
|
|
|
return m_references.get();
|
|
|
|
}
|
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.
|
|
|
|
*/
|
2012-12-07 00:12:24 +04:00
|
|
|
std::size_t CountNgrams(const std::string& line, NgramCounts& counts, unsigned int n, bool is_testing=false);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-03-19 17:45:15 +04:00
|
|
|
void DumpCounts(std::ostream* os, const NgramCounts& counts) const;
|
2011-11-12 05:16:31 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
bool OpenReference(const char* filename, std::size_t file_id);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-04-04 17:33:30 +04:00
|
|
|
// NOTE: this function is used for unit testing.
|
2013-07-03 16:03:58 +04:00
|
|
|
virtual bool OpenReferenceStream(std::istream* is, std::size_t file_id);
|
2011-11-12 05:16:31 +04:00
|
|
|
|
2013-07-04 23:19:51 +04:00
|
|
|
//private:
|
2013-07-03 16:03:58 +04:00
|
|
|
protected:
|
2012-02-01 15:24:48 +04:00
|
|
|
ReferenceLengthType m_ref_length_type;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-03-18 00:58:40 +04:00
|
|
|
// reference translations.
|
|
|
|
ScopedVector<Reference> m_references;
|
2011-11-12 05:21:08 +04:00
|
|
|
|
2013-07-03 16:03:58 +04:00
|
|
|
// constructor used by subclasses
|
|
|
|
BleuScorer(const std::string& name, const std::string& config): StatisticsBasedScorer(name,config) {}
|
2013-07-04 23:19:51 +04:00
|
|
|
|
2011-11-12 05:21:08 +04:00
|
|
|
// no copying allowed
|
|
|
|
BleuScorer(const BleuScorer&);
|
|
|
|
BleuScorer& operator=(const BleuScorer&);
|
2008-05-27 20:50:52 +04:00
|
|
|
};
|
|
|
|
|
2012-04-06 20:02:32 +04:00
|
|
|
/** Computes sentence-level BLEU+1 score.
|
|
|
|
* This function is used in PRO.
|
|
|
|
*/
|
2013-02-18 15:11:20 +04:00
|
|
|
float smoothedSentenceBleu
|
2013-05-29 21:16:15 +04:00
|
|
|
(const std::vector<float>& stats, float smoothing=1.0, bool smoothBP=false);
|
2012-04-06 20:02:32 +04:00
|
|
|
|
2012-05-29 21:38:57 +04:00
|
|
|
/** Computes sentence-level BLEU score given a background corpus.
|
|
|
|
* This function is used in batch MIRA.
|
|
|
|
*/
|
|
|
|
float sentenceLevelBackgroundBleu(const std::vector<float>& sent, const std::vector<float>& bg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes plain old BLEU from a vector of stats
|
|
|
|
*/
|
|
|
|
float unsmoothedBleu(const std::vector<float>& stats);
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
}
|
|
|
|
|
2012-02-20 04:46:08 +04:00
|
|
|
#endif // MERT_BLEU_SCORER_H_
|