00001 #include "SemposOverlapping.h"
00002 #include "SemposScorer.h"
00003
00004 #include <algorithm>
00005 #include <stdexcept>
00006
00007 using namespace std;
00008
00009 namespace
00010 {
00011
00012 MosesTuning::SemposOverlapping* g_overlapping = NULL;
00013
00014 }
00015
00016 namespace MosesTuning
00017 {
00018
00019
00020 SemposOverlapping* SemposOverlappingFactory::GetOverlapping(const string& str, const SemposScorer* sempos)
00021 {
00022 if (str == "cap-micro") {
00023 return new CapMicroOverlapping(sempos);
00024 } else if (str == "cap-macro") {
00025 return new CapMacroOverlapping(sempos);
00026 } else {
00027 throw runtime_error("Unknown overlapping: " + str);
00028 }
00029 }
00030
00031 void SemposOverlappingFactory::SetOverlapping(SemposOverlapping* ovr)
00032 {
00033 g_overlapping = ovr;
00034 }
00035
00036 vector<ScoreStatsType> CapMicroOverlapping::prepareStats(const sentence_t& cand, const sentence_t& ref)
00037 {
00038 vector<ScoreStatsType> stats(2);
00039 sentence_t intersection;
00040
00041 set_intersection(cand.begin(), cand.end(), ref.begin(), ref.end(),
00042 inserter(intersection, intersection.begin()));
00043
00044 int multCoeff = 1000;
00045
00046 float interSum = 0;
00047 for (sentence_t::iterator it = intersection.begin(); it != intersection.end(); it++) {
00048 interSum += semposScorer->weight(it->first);
00049 }
00050
00051 float refSum = 0;
00052 for (sentence_t::iterator it = ref.begin(); it != ref.end(); it++) {
00053 refSum += semposScorer->weight(it->first);
00054 }
00055
00056 stats[0] = (ScoreStatsType)(multCoeff * interSum);
00057 stats[1] = (ScoreStatsType)(multCoeff * refSum);
00058 return stats;
00059 }
00060
00061 float CapMicroOverlapping::calculateScore(const vector<ScoreStatsType>& stats) const
00062 {
00063 if (stats.size() != 2) {
00064 throw std::runtime_error("Size of stats vector has to be 2");
00065 }
00066 if (stats[1] == 0) return 1.0f;
00067 return stats[0] / static_cast<float>(stats[1]);
00068 }
00069
00070 vector<ScoreStatsType> CapMacroOverlapping::prepareStats(const sentence_t& cand, const sentence_t& ref)
00071 {
00072 vector<ScoreStatsType> stats(2 * kMaxNOC);
00073 sentence_t intersection;
00074
00075 set_intersection(cand.begin(), cand.end(), ref.begin(), ref.end(),
00076 inserter(intersection, intersection.begin()));
00077
00078 int multCoeff = 1000;
00079
00080 for (int i = 0; i < 2 * kMaxNOC; ++i) stats[i] = 0;
00081 for (sentence_t::const_iterator it = intersection.begin(); it != intersection.end(); ++it) {
00082 const int sempos = it->second;
00083 float weight = semposScorer->weight(it->first);
00084 stats[2 * sempos] += weight * multCoeff ;
00085 }
00086 for (sentence_t::const_iterator it = ref.begin(); it != ref.end(); ++it) {
00087 const int sempos = it->second;
00088 float weight = semposScorer->weight(it->first);
00089 stats[2 * sempos + 1] += weight * multCoeff;
00090 }
00091
00092 return stats;
00093 }
00094
00095 float CapMacroOverlapping::calculateScore(const vector<ScoreStatsType>& stats) const
00096 {
00097 if (stats.size() != 2 * kMaxNOC) {
00098
00099 throw std::runtime_error("Size of stats vector has to be 38");
00100 }
00101
00102 int n = 0;
00103 float sum = 0;
00104 for (int i = 0; i < kMaxNOC; ++i) {
00105 int clipped = stats[2 * i];
00106 int refsize = stats[2 * i + 1];
00107 if (refsize > 0) {
00108 sum += clipped / static_cast<float>(refsize);
00109 ++n;
00110 }
00111 }
00112 if (n == 0) return 1;
00113 return sum / n;
00114 }
00115
00116 }