2012-03-18 00:58:40 +04:00
|
|
|
#ifndef MERT_REFERENCE_H_
|
|
|
|
#define MERT_REFERENCE_H_
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <climits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Ngram.h"
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
|
2012-03-19 17:45:15 +04:00
|
|
|
/**
|
|
|
|
* Reference class represents reference translations for an output
|
|
|
|
* translation used in calculating BLEU score.
|
|
|
|
*/
|
2013-05-29 21:16:15 +04:00
|
|
|
class Reference
|
|
|
|
{
|
|
|
|
public:
|
2012-03-18 00:58:40 +04:00
|
|
|
// for m_length
|
2012-05-10 02:51:05 +04:00
|
|
|
typedef std::vector<std::size_t>::iterator iterator;
|
|
|
|
typedef std::vector<std::size_t>::const_iterator const_iterator;
|
2012-03-18 00:58:40 +04:00
|
|
|
|
|
|
|
Reference() : m_counts(new NgramCounts) { }
|
2013-05-29 21:16:15 +04:00
|
|
|
~Reference() {
|
|
|
|
delete m_counts;
|
|
|
|
}
|
2012-03-18 00:58:40 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
NgramCounts* get_counts() {
|
|
|
|
return m_counts;
|
|
|
|
}
|
|
|
|
const NgramCounts* get_counts() const {
|
|
|
|
return m_counts;
|
|
|
|
}
|
2012-03-18 00:58:40 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
iterator begin() {
|
|
|
|
return m_length.begin();
|
|
|
|
}
|
|
|
|
const_iterator begin() const {
|
|
|
|
return m_length.begin();
|
|
|
|
}
|
|
|
|
iterator end() {
|
|
|
|
return m_length.end();
|
|
|
|
}
|
|
|
|
const_iterator end() const {
|
|
|
|
return m_length.end();
|
|
|
|
}
|
2012-03-18 00:58:40 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void push_back(std::size_t len) {
|
|
|
|
m_length.push_back(len);
|
|
|
|
}
|
2012-03-18 00:58:40 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
std::size_t num_references() const {
|
|
|
|
return m_length.size();
|
|
|
|
}
|
2012-03-18 00:58:40 +04:00
|
|
|
|
|
|
|
int CalcAverage() const;
|
2012-05-10 02:51:05 +04:00
|
|
|
int CalcClosest(std::size_t length) const;
|
2012-03-18 00:58:40 +04:00
|
|
|
int CalcShortest() const;
|
|
|
|
|
2015-05-01 00:26:30 +03:00
|
|
|
void clear() {
|
|
|
|
m_length.clear();
|
|
|
|
m_counts->clear();
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
private:
|
2012-03-18 00:58:40 +04:00
|
|
|
NgramCounts* m_counts;
|
|
|
|
|
|
|
|
// multiple reference lengths
|
2012-05-10 02:51:05 +04:00
|
|
|
std::vector<std::size_t> m_length;
|
2012-03-18 00:58:40 +04:00
|
|
|
};
|
|
|
|
|
2012-04-26 00:44:05 +04:00
|
|
|
// TODO(tetsuok): fix this function and related stuff.
|
|
|
|
// "average" reference length should not be calculated at sentence-level unlike "closest".
|
2013-05-29 21:16:15 +04:00
|
|
|
inline int Reference::CalcAverage() const
|
|
|
|
{
|
2012-03-18 00:58:40 +04:00
|
|
|
int total = 0;
|
2012-05-10 02:51:05 +04:00
|
|
|
for (std::size_t i = 0; i < m_length.size(); ++i) {
|
2012-03-18 00:58:40 +04:00
|
|
|
total += m_length[i];
|
|
|
|
}
|
|
|
|
return static_cast<int>(
|
2013-05-29 21:16:15 +04:00
|
|
|
static_cast<float>(total) / m_length.size());
|
2012-03-18 00:58:40 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
inline int Reference::CalcClosest(std::size_t length) const
|
|
|
|
{
|
2012-03-18 00:58:40 +04:00
|
|
|
int min_diff = INT_MAX;
|
|
|
|
int closest_ref_id = 0; // an index of the closest reference translation
|
2012-05-10 02:51:05 +04:00
|
|
|
for (std::size_t i = 0; i < m_length.size(); ++i) {
|
2012-03-18 00:58:40 +04:00
|
|
|
const int ref_length = m_length[i];
|
|
|
|
const int length_diff = abs(ref_length - static_cast<int>(length));
|
|
|
|
const int abs_min_diff = abs(min_diff);
|
|
|
|
// Look for the closest reference
|
|
|
|
if (length_diff < abs_min_diff) {
|
|
|
|
min_diff = ref_length - length;
|
|
|
|
closest_ref_id = i;
|
|
|
|
// if two references has the same closest length, take the shortest
|
|
|
|
} else if (length_diff == abs_min_diff) {
|
|
|
|
if (ref_length < static_cast<int>(m_length[closest_ref_id])) {
|
|
|
|
closest_ref_id = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return static_cast<int>(m_length[closest_ref_id]);
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
inline int Reference::CalcShortest() const
|
|
|
|
{
|
2012-03-18 00:58:40 +04:00
|
|
|
return *std::min_element(m_length.begin(), m_length.end());
|
|
|
|
}
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-18 00:58:40 +04:00
|
|
|
#endif // MERT_REFERENCE_H_
|