-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullBackoffStrategy.cpp
110 lines (83 loc) · 2.63 KB
/
FullBackoffStrategy.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
/*
* FullBackoffStrategy.cpp
*
* Created on: Dec 13, 2016
* Author: onrust
*/
#include "FullBackoffStrategy.h"
#include "Logging.h"
#include "InterpolationStrategy.h"
namespace SLM {
FullBackoffStrategy::FullBackoffStrategy(SLM::LanguageModel& languageModel, const std::string& baseFileName, SLM::InterpolationStrategy* interpolationStrategy)
: BackoffStrategy(languageModel, baseFileName), interpolationStrategy(interpolationStrategy)
{
// init(languageModel, baseFileName);
// std::string cacheFileName = baseFileName + "_" + name() + "." + cacheExtension;
// L_V << "FullBackoffStrategy: (" << name() << ")" << std::setw(30) << "Cache output file:" << cacheFileName << "\n";
// cacheOutputFile.open(cacheFileName);
}
void FullBackoffStrategy::init(SLM::LanguageModel& languageModel, const std::string& baseFileName)
{
openFiles(languageModel, baseFileName);
std::string cacheFileName = baseFileName + "_" + name() + "." + cacheExtension;
L_V << "FullBackoffStrategy: (" << name() << ")" << std::setw(30) << "Cache output file:" << cacheFileName << "\n";
cacheOutputFile.open(cacheFileName);
}
FullBackoffStrategy::~FullBackoffStrategy() {
cacheOutputFile.flush();
cacheOutputFile.close();
}
std::string FullBackoffStrategy::name() const
{
return "full" + interpolationStrategy->name();
}
bool FullBackoffStrategy::setIgnoreCache(bool setting)
{
ignoreCache = setting;
return ignoreCache;
}
bool FullBackoffStrategy::addToCache(const Pattern& pattern, double val)
{
if(!ignoreCache)
{
cache[pattern] = val;
return true;
}
return false;
}
std::experimental::optional<double> FullBackoffStrategy::getFromCache(const Pattern& pattern)
{
auto i = cache.find(pattern);
if(i == cache.end())
{
return std::experimental::nullopt;
} else
{
return i->second;
}
}
void FullBackoffStrategy::writeCache()
{
}
double FullBackoffStrategy::prob(const Pattern& context, const Pattern& focus, bool isOOV)
{
L_S << "FullBackoffStrategy(" << interpolationStrategy->name() << "): Estimating prob for " << languageModel.toString(context)
<< " " << languageModel.toString(focus) << "\n";
double logProb = 0.0;
if(!isOOV)
{
// implement skipgrams from layer 3 on
double prob = languageModel.getProbS4(focus, context, this, interpolationStrategy);
// double prob = languageModel.getProb4(focus, context);
logProb = log2(prob);
++sentCount;
sentLLH -= logProb;
} else
{
++sentOovs;
}
L_S << "FullBackoffStrategy(" << interpolationStrategy->name() << "): \t\t" << logProb << "\n";
writeProbToFile(focus, context, logProb, isOOV);
return logProb;
}
} /* namespace SLM */