-
Notifications
You must be signed in to change notification settings - Fork 779
/
Copy pathSemposOverlapping.cpp
116 lines (92 loc) · 3.04 KB
/
SemposOverlapping.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "SemposOverlapping.h"
#include "SemposScorer.h"
#include <algorithm>
#include <stdexcept>
using namespace std;
namespace
{
MosesTuning::SemposOverlapping* g_overlapping = NULL;
} // namespace
namespace MosesTuning
{
SemposOverlapping* SemposOverlappingFactory::GetOverlapping(const string& str, const SemposScorer* sempos)
{
if (str == "cap-micro") {
return new CapMicroOverlapping(sempos);
} else if (str == "cap-macro") {
return new CapMacroOverlapping(sempos);
} else {
throw runtime_error("Unknown overlapping: " + str);
}
}
void SemposOverlappingFactory::SetOverlapping(SemposOverlapping* ovr)
{
g_overlapping = ovr;
}
vector<ScoreStatsType> CapMicroOverlapping::prepareStats(const sentence_t& cand, const sentence_t& ref)
{
vector<ScoreStatsType> stats(2);
sentence_t intersection;
set_intersection(cand.begin(), cand.end(), ref.begin(), ref.end(),
inserter(intersection, intersection.begin()));
int multCoeff = 1000;
float interSum = 0;
for (sentence_t::iterator it = intersection.begin(); it != intersection.end(); it++) {
interSum += semposScorer->weight(it->first);
}
float refSum = 0;
for (sentence_t::iterator it = ref.begin(); it != ref.end(); it++) {
refSum += semposScorer->weight(it->first);
}
stats[0] = (ScoreStatsType)(multCoeff * interSum);
stats[1] = (ScoreStatsType)(multCoeff * refSum);
return stats;
}
float CapMicroOverlapping::calculateScore(const vector<ScoreStatsType>& stats) const
{
if (stats.size() != 2) {
throw std::runtime_error("Size of stats vector has to be 2");
}
if (stats[1] == 0) return 1.0f;
return stats[0] / static_cast<float>(stats[1]);
}
vector<ScoreStatsType> CapMacroOverlapping::prepareStats(const sentence_t& cand, const sentence_t& ref)
{
vector<ScoreStatsType> stats(2 * kMaxNOC);
sentence_t intersection;
set_intersection(cand.begin(), cand.end(), ref.begin(), ref.end(),
inserter(intersection, intersection.begin()));
int multCoeff = 1000;
for (int i = 0; i < 2 * kMaxNOC; ++i) stats[i] = 0;
for (sentence_t::const_iterator it = intersection.begin(); it != intersection.end(); ++it) {
const int sempos = it->second;
float weight = semposScorer->weight(it->first);
stats[2 * sempos] += weight * multCoeff ;
}
for (sentence_t::const_iterator it = ref.begin(); it != ref.end(); ++it) {
const int sempos = it->second;
float weight = semposScorer->weight(it->first);
stats[2 * sempos + 1] += weight * multCoeff;
}
return stats;
}
float CapMacroOverlapping::calculateScore(const vector<ScoreStatsType>& stats) const
{
if (stats.size() != 2 * kMaxNOC) {
// TODO: Add some comments. The number "38" looks like a magic number.
throw std::runtime_error("Size of stats vector has to be 38");
}
int n = 0;
float sum = 0;
for (int i = 0; i < kMaxNOC; ++i) {
int clipped = stats[2 * i];
int refsize = stats[2 * i + 1];
if (refsize > 0) {
sum += clipped / static_cast<float>(refsize);
++n;
}
}
if (n == 0) return 1;
return sum / n;
}
}