-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackoffStrategy.cpp
161 lines (117 loc) · 4.61 KB
/
BackoffStrategy.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* BackoffStrategy.cpp
*
* Created on: Dec 12, 2016
* Author: onrust
*/
#include "BackoffStrategy.h"
#include "Logging.h"
namespace SLM {
BackoffStrategy::BackoffStrategy(SLM::LanguageModel& languageModel, const std::string& baseFileName)
: languageModel(languageModel)
{
}
void BackoffStrategy::init() const
{
}
void BackoffStrategy::openFiles(SLM::LanguageModel& languageModel, const std::string& baseFileName)
{
std::string outputFileName = baseFileName + "_" + name() + "." + outputExtension;
L_V << "BackoffStrategy: (" << name() << ")" << std::setw(30) << "Output file:" << outputFileName << "\n";
outputFile.open(outputFileName);
std::string probsFileName = baseFileName + "_" + name() + "." + probsExtension;
L_V << "BackoffStrategy: (" << name() << ")" << std::setw(30) << "Probs output file:" << probsFileName << "\n";
probsFile.open(probsFileName);
std::string sentenceFileName = baseFileName + "_" + name() + "." + sentencesExtension;
L_V << "BackoffStrategy: (" << name() << ")" << std::setw(30) << "Sentences output file:" << sentenceFileName << "\n";
sentsProbFile.open(sentenceFileName);
}
BackoffStrategy::~BackoffStrategy() {
outputFile.flush();
outputFile.close();
probsFile.flush();
probsFile.close();
sentsProbFile.flush();
sentsProbFile.close();
}
double BackoffStrategy::perplexityAndNextFile()
{
totalCount += fileCount;
totalOovs += fileOovs;
totalLLH += fileLLH;
totalSentences += sentences;
double filePerplexity = 0.0;
if(files)
{
// done();
filePerplexity = pow(2, fileLLH/(fileCount/*-totalOovs*/));
L_I << "BackoffStrategy: FILE (" << name() << ")\tF" << sentences << "\tP" << filePerplexity << "\tC" << (fileCount/*-totalOovs*/) << "\tO" << fileOovs << "\tL" << fileLLH << std::endl;
outputFile << "BackoffStrategy: FILE (" << name() << ")\tF" << sentences << "\tP" << filePerplexity << "\tC" << (fileCount/*-totalOovs*/) << "\tO" << fileOovs << "\tL" << fileLLH << std::endl;
double totalPerplexity = pow(2, totalLLH/(totalCount/*-totalOovs*/));
L_I << "BackoffStrategy: TOTAL (" << name() << ")\tS" << sentences << "\tP" << totalPerplexity << "\tC" << (totalCount/*-totalOovs*/) << "\tO" << totalOovs << "\tL" << totalLLH << std::endl;
}
++files;
L_V << "BackoffStrategy: (" << name() << ") next file\n";
fileCount = 0;
fileOovs = 0;
fileLLH = 0.0;
sentences = 0;
sentCount = 0;
sentOovs = 0;
sentLLH = 0.0;
return filePerplexity;
}
int BackoffStrategy::nextFile()
{
perplexityAndNextFile();
return files;
}
int BackoffStrategy::nextLine()
{
if(sentences)
{
double sentPerplexity = pow(2, sentLLH/(1.0*sentCount/*-sentOovs*/));
L_V << "BackoffStrategy: SENT (" << name() << ") sentence:" << sentences << "\tppl:" << sentPerplexity << "\tcount:" << (sentCount/*-sentOovs*/) << "\toovs:" << sentOovs << "\tllh:" << sentLLH << std::endl;
sentsProbFile << sentences << "\t" << sentPerplexity << "\t" << (sentCount/*-sentOovs*/) << "\t" << sentOovs << "\t" << sentLLH << std::endl;
}
++sentences;
fileCount += sentCount;
fileOovs += sentOovs;
fileLLH += sentLLH;
sentCount = 0;
sentOovs = 0;
sentLLH = 0.0;
return sentences;
}
void BackoffStrategy::done()
{
nextLine();
// double filePerplexity = pow(2, fileLLH/(fileCount/*-totalOovs*/));
// L_I << "BackoffStrategy: " << name() << "\tF" << sentences << "\tP" << filePerplexity << "\tC" << (fileCount/*-totalOovs*/) << "\tO" << fileOovs << "\tL" << fileLLH << std::endl;
--sentences;
// L_V << "BackoffStrategy: done\n";
nextFile();
double totalPerplexity = pow(2, totalLLH/(totalCount/*-totalOovs*/));
outputFile << "BackoffStrategy: TOTAL (" << name() << ")\tS" << totalSentences << "\tP" << totalPerplexity << "\tC" << (totalCount/*-totalOovs*/) << "\tO" << totalOovs << "\tL" << totalLLH << std::endl;
// if(files)
// {
// L_I << "BackoffStrategy: " << name() << " #C" << fileCount << "/O" << fileOovs << "[S" << sentences << "]:L" << fileLLH << "\n";
// }
// totalCount += fileCount;
// totalOovs += fileOovs;
// totalLLH += fileLLH;
// double totalPerplexity = pow(2, totalLLH/(totalCount/*-totalOovs*/));
// L_I << "BackoffStrategy: " << name() << "\tS" << sentences << "\tP" << totalPerplexity << "\tC" << (totalCount/*-totalOovs*/) << "\tO" << totalOovs << "\tL" << totalLLH << std::endl;
}
void BackoffStrategy::writeProbToFile(const Pattern& focus, const Pattern& context, double logProb, bool isOOV)
{
if(isOOV)
{
/*std::cout*/ probsFile << "***";
}
/*std::cout*/ probsFile << "p(" << languageModel.toString(focus) << " |"
<< languageModel.toString(context) << ") = "
<< std::fixed << std::setprecision(20) << logProb
<< std::endl;
}
} /* namespace SLM */