-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmotionRating.cpp
More file actions
73 lines (61 loc) · 1.7 KB
/
EmotionRating.cpp
File metadata and controls
73 lines (61 loc) · 1.7 KB
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
#include "EmotionRating.h"
#include <cmath>
#include <algorithm>
EmotionRating::EmotionRating(int numClasses) : R(numClasses) {
c.resize(R);
u.resize(R);
S.resize(R);
}
void EmotionRating::compute(double P, double N) {
(void)N; // N is not directly used in formula, but kept for symmetry
computeDelta(P);
computeNormalizedIntensity();
computeClassCenters();
computeScores();
computeSoftmax();
selectClassAndPolarity();
}
void EmotionRating::computeDelta(double P) {
delta = 2 * P - 100;
}
void EmotionRating::computeNormalizedIntensity() {
t = std::fabs(delta) / 100.0;
if (t > 1.0) t = 1.0; // clamp just in case
}
void EmotionRating::computeClassCenters() {
for (int k = 0; k < R; k++) {
c[k] = static_cast<double>(k) / (R - 1);
}
}
void EmotionRating::computeScores() {
double sigma = 0.5 / (R - 1);
for (int k = 0; k < R; k++) {
u[k] = - ( (t - c[k]) * (t - c[k]) ) / (2 * sigma * sigma);
}
}
void EmotionRating::computeSoftmax() {
double sumExp = 0.0;
for (int k = 0; k < R; k++) {
S[k] = std::exp(u[k]);
sumExp += S[k];
}
for (int k = 0; k < R; k++) {
S[k] /= sumExp;
}
}
void EmotionRating::selectClassAndPolarity() {
auto maxIt = std::max_element(S.begin(), S.end());
k_star = std::distance(S.begin(), maxIt) + 1; // +1 as in formula
if (delta > 0) s = 1;
else if (delta < 0) s = -1;
else s = 0;
}
int EmotionRating::getSelectedClass() const {
return k_star;
}
int EmotionRating::getPolarity() const {
return s;
}
std::vector<double> EmotionRating::getSoftmax() const {
return S;
}