-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntropyInterpolationStrategy.cpp
70 lines (54 loc) · 1.38 KB
/
EntropyInterpolationStrategy.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
/*
* EntropyInterpolationStrategy.cpp
*
* Created on: Feb 7, 2017
* Author: louis
*/
#include "EntropyInterpolationStrategy.h"
#include "LanguageModel.h"
#include <functional>
#include <algorithm>
#include <numeric>
#include <cmath>
namespace SLM {
EntropyInterpolationStrategy::EntropyInterpolationStrategy(SLM::LanguageModel& lm) : lm(&lm) {
// TODO Auto-generated constructor stub
}
EntropyInterpolationStrategy::~EntropyInterpolationStrategy() {
// TODO Auto-generated destructor stub
}
double EntropyInterpolationStrategy::get(const Pattern& context)
{
int contextSize = context.size();
auto it = weights.find(context);
if(it != weights.end())
{
return it->second;
}
else
{
double entropySum = 0.0;
std::vector<unsigned int> occurrenceCounts = lm->getCounts(context);
unsigned int sum = std::accumulate ( occurrenceCounts.begin( ) , occurrenceCounts.end( ) , 0 ) ;
if(sum > 0)
{
for (auto count: occurrenceCounts)
{
if(count > 0)
{
double mle = (1.0*count)/(1.0*sum);
entropySum -= mle * log2(mle);
}
}
}
double entropy = 1.0 / (1.0 + entropySum);
L_S << "Entropyi: get(" << contextSize << ") sum:" << sum << " entropysum:" << entropySum << " entropy:" << entropy << "\n";
weights[context] = entropy;
return entropy;
}
}
std::string EntropyInterpolationStrategy::name() const
{
return "entropy";
}
} /* namespace SLM */