-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLEInterpolationStrategy.cpp
86 lines (66 loc) · 1.94 KB
/
MLEInterpolationStrategy.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
/*
* MLEInterpolationStrategy.cpp
*
* Created on: Dec 19, 2016
* Author: onrust
*/
#include "MLEInterpolationStrategy.h"
#include "LanguageModel.h"
#include <functional>
#include <algorithm>
#include <numeric>
#include <cmath>
namespace SLM {
MLEInterpolationStrategy::MLEInterpolationStrategy(SLM::LanguageModel& lm) : lm(&lm) {
// TODO Auto-generated constructor stub
}
MLEInterpolationStrategy::~MLEInterpolationStrategy() {
// TODO Auto-generated destructor stub
}
double MLEInterpolationStrategy::get(const Pattern& context)
{
Pattern contextContext;
Pattern contextFocus;
int contextSize = context.size();
if(contextSize == 1)
{
contextFocus = context;
} else if(contextSize == 2)
{
contextContext = Pattern(context, 0, 1);
contextFocus = Pattern(context, 1, 1);
} else if(contextSize == 3)
{
contextContext = Pattern(context, 0, 2);
contextFocus = Pattern(context, 2, 1);
} else if(contextSize == 4)
{
contextContext = Pattern(context, 0, 3);
contextFocus = Pattern(context, 3, 1);
}
std::map<Pattern, double>::const_iterator i = weights.find(context);
if(i == weights.end())
{
std::vector<unsigned int> occurrenceCounts = lm->getCounts(contextContext);
unsigned int sum = std::accumulate ( occurrenceCounts.begin( ) , occurrenceCounts.end( ) , 0 ) ;
double mle = (1.0*lm->getCount(contextFocus, contextContext))/(1.0*sum);
if(lm->toString(contextFocus) == "{*}")
mle = (1.0*(sum-1))/(2.0*sum);
if(!std::isnormal(mle))
{
mle = 0.000000000000001;
}
mle = std::max(0.000000000000001, mle);
L_S << "MLEi: get(" << lm->toString(contextContext) << "," << lm->toString(contextFocus) << ") sum:" << sum << " count:" << lm->getCount(contextFocus, contextContext) << " MLE:" << mle << "\n";
// weights[context] = mle;
return mle;
} else
{
return i->second;
}
}
std::string MLEInterpolationStrategy::name() const
{
return "mle";
}
} /* namespace SLM */