00001 #ifndef MERT_REFERENCE_H_
00002 #define MERT_REFERENCE_H_
00003
00004 #include <algorithm>
00005 #include <climits>
00006 #include <vector>
00007
00008 #include "Ngram.h"
00009
00010 namespace MosesTuning
00011 {
00012
00013
00018 class Reference
00019 {
00020 public:
00021
00022 typedef std::vector<std::size_t>::iterator iterator;
00023 typedef std::vector<std::size_t>::const_iterator const_iterator;
00024
00025 Reference() : m_counts(new NgramCounts) { }
00026 ~Reference() {
00027 delete m_counts;
00028 }
00029
00030 NgramCounts* get_counts() {
00031 return m_counts;
00032 }
00033 const NgramCounts* get_counts() const {
00034 return m_counts;
00035 }
00036
00037 iterator begin() {
00038 return m_length.begin();
00039 }
00040 const_iterator begin() const {
00041 return m_length.begin();
00042 }
00043 iterator end() {
00044 return m_length.end();
00045 }
00046 const_iterator end() const {
00047 return m_length.end();
00048 }
00049
00050 void push_back(std::size_t len) {
00051 m_length.push_back(len);
00052 }
00053
00054 std::size_t num_references() const {
00055 return m_length.size();
00056 }
00057
00058 int CalcAverage() const;
00059 int CalcClosest(std::size_t length) const;
00060 int CalcShortest() const;
00061
00062 void clear() {
00063 m_length.clear();
00064 m_counts->clear();
00065 }
00066
00067 private:
00068 NgramCounts* m_counts;
00069
00070
00071 std::vector<std::size_t> m_length;
00072 };
00073
00074
00075
00076 inline int Reference::CalcAverage() const
00077 {
00078 int total = 0;
00079 for (std::size_t i = 0; i < m_length.size(); ++i) {
00080 total += m_length[i];
00081 }
00082 return static_cast<int>(
00083 static_cast<float>(total) / m_length.size());
00084 }
00085
00086 inline int Reference::CalcClosest(std::size_t length) const
00087 {
00088 int min_diff = INT_MAX;
00089 int closest_ref_id = 0;
00090 for (std::size_t i = 0; i < m_length.size(); ++i) {
00091 const int ref_length = m_length[i];
00092 const int length_diff = abs(ref_length - static_cast<int>(length));
00093 const int abs_min_diff = abs(min_diff);
00094
00095 if (length_diff < abs_min_diff) {
00096 min_diff = ref_length - length;
00097 closest_ref_id = i;
00098
00099 } else if (length_diff == abs_min_diff) {
00100 if (ref_length < static_cast<int>(m_length[closest_ref_id])) {
00101 closest_ref_id = i;
00102 }
00103 }
00104 }
00105 return static_cast<int>(m_length[closest_ref_id]);
00106 }
00107
00108 inline int Reference::CalcShortest() const
00109 {
00110 return *std::min_element(m_length.begin(), m_length.end());
00111 }
00112
00113 }
00114
00115
00116 #endif // MERT_REFERENCE_H_