-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageModel.h
203 lines (146 loc) · 6.82 KB
/
LanguageModel.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* LanguageModel.h
*
* Created on: Dec 10, 2016
* Author: louis
*/
#ifndef LANGUAGEMODEL_H_
#define LANGUAGEMODEL_H_
#include <classencoder.h>
#include <classdecoder.h>
#include <patternmodel.h>
//#include "BackoffStrategy.h"
#include "ProgramOptions.h"
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include "cpyp/m.h"
#include "cpyp/random.h"
#include "cpyp/crp.h"
#include "cpyp/tied_parameter_resampler.h"
#include <boost/dynamic_bitset.hpp>
#include "uvector.h"
#include "uniform_vocab.h"
//#include <classdecoder.h>
#include <pattern.h>
#include "Logging.h"
#include "InterpolationStrategy.h"
namespace SLM {
class BackoffStrategy;
class InterpolationStrategy;
class LanguageModel;
} /* namespace SLM */
//#include "LanguageModel.h"
// A not very memory-efficient implementation of an N-gram LM based on PYPs
// as described in Y.-W. Teh. (2006) A Hierarchical Bayesian Language Model
// based on Pitman-Yor Processes. In Proc. ACL.
namespace cpyp {
template<unsigned N> struct PYPLM;
template<> struct PYPLM<0> : public UniformVocabulary {
PYPLM(unsigned vs, double a, double b, double c, double d) :
UniformVocabulary(vs, a, b, c, d) {
}
};
// represents an N-gram LM
template<unsigned N> struct PYPLM {
PYPLM() :
backoff(0, 1, 1, 1, 1), tr(1, 1, 1, 1, 0.8, 0.0) {
}
explicit PYPLM(unsigned vs, double da = 1.0, double db = 1.0, double ss = 1.0, double sr = 1.0) :
backoff(vs, da, db, ss, sr), tr(da, db, ss, sr, 0.8, 0.0) {
}
template<typename Engine>
void increment(const Pattern& w, const Pattern& context, Engine& eng) {
const double bo = backoff.prob(w, context);
Pattern lookup = (N==1) ? Pattern() : Pattern(context.reverse(), 0, N-1);
auto it = p.find(lookup);
if (it == p.end()) {
it = p.insert(make_pair(lookup, crp<Pattern>(0.8, 0))).first;
tr.insert(&it->second); // add to resampler
}
if (it->second.increment(w, bo, eng)) {
backoff.increment(w, context, eng);
}
}
template<typename Engine>
void decrement(const Pattern& w, const Pattern& context, Engine& eng) {
Pattern lookup = (N==1) ? Pattern() : Pattern(context.reverse(), 0, N-1);
auto it = p.find(lookup);
assert(it != p.end());
if (it->second.decrement(w, eng)) {
backoff.decrement(w, context, eng);
}
}
std::pair <double,double> prob_recursive(const Pattern& w, const Pattern& originalContext, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is, const boost::dynamic_bitset<>& skipPattern) const;
double getProb(const Pattern& w, const Pattern& context, double backoffProb, int level) const;
std::pair <double,double> prob_() const;
std::pair <double,double> prob_d(const Pattern& w, SLM::InterpolationStrategy* is) const;
std::pair <double,double> prob_cd(const Pattern& w, const Pattern& originalContext, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is) const;
std::pair <double,double> prob_b_d(const Pattern& w, const Pattern& originalContext, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is) const;
std::pair <double,double> prob_a__d(const Pattern& w, const Pattern& originalContext, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is) const;
std::pair <double,double> prob_bcd(const Pattern& w, const Pattern& originalContext, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is) const;
std::pair <double,double> prob_a_cd(const Pattern& w, const Pattern& originalContext, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is) const;
std::pair <double,double> prob_ab_d(const Pattern& w, const Pattern& originalContext, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is) const;
std::pair <double,double> prob_abcd(const Pattern& w, const Pattern& originalContext, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is) const;
unsigned int getCount(const Pattern& w, const Pattern& context) const;
std::vector<unsigned int> getCounts(const Pattern& context) const;
double prob(const Pattern& w, const Pattern& context) const;
double prob(const Pattern& w, const Pattern& context, double boProb) const;
double prob4(const Pattern& w, const Pattern& context) const;
double probS4(const Pattern& w, const Pattern& context, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is);
double probLS4(const Pattern& w, const Pattern& context, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is, std::unordered_map<Pattern, double>& normalisationCache, unsigned vocabSize);
double getNormalisationFactor(const Pattern& context, const Pattern& originalContext, crp<Pattern> restaurant, SLM::BackoffStrategy* bs, SLM::InterpolationStrategy* is, std::unordered_map<Pattern, double>& normalisationCache, unsigned vocabSize);
double log_likelihood() const {
return backoff.log_likelihood() + tr.log_likelihood();
}
template<typename Engine>
void resample_hyperparameters(Engine& eng) {
tr.resample_hyperparameters(eng);
backoff.resample_hyperparameters(eng);
}
template<class Archive> void serialize(Archive& ar, const unsigned int version) {
backoff.serialize(ar, version);
//ar & tr;
ar & p;
}
PYPLM<N - 1> backoff;
tied_parameter_resampler<crp<Pattern>> tr;
std::unordered_map<Pattern, crp<Pattern>> p; // .first = context .second = CRP
};
} // namespace cpyp
namespace SLM
{
class LanguageModel {
public:
LanguageModel(const SLM::ProgramOptions& programOptions);
virtual ~LanguageModel();
Pattern toPattern(const std::string& patternString, bool allowUnknown = false);
std::string toString(const Pattern& pattern);
const PatternSet<uint64_t>& getVocabulary() const;
unsigned getVocabularySize();
bool isOOV(const Pattern& word);
unsigned int count(const Pattern& pattern);
unsigned int getCount(const Pattern& focus, const Pattern& context) const;
std::vector<unsigned int> getCounts(const Pattern& context) const;
double getProb(const Pattern& focus, const Pattern& context);
double getProb4(const Pattern& focus, const Pattern& context);
double getProbS4(const Pattern& focus, const Pattern& context, SLM::BackoffStrategy* backoffStrategy, SLM::InterpolationStrategy* interpolationStrategy);
double getProbLS4(const Pattern& focus, const Pattern& context, SLM::BackoffStrategy* backoffStrategy, SLM::InterpolationStrategy* interpolationStrategy, std::unordered_map<Pattern, double>& normalisationCache);
private:
void initialise(const ProgramOptions& programOptions);
void defaultPatternModelOptions();
void extendClassEncoder(const std::vector<std::string>& inputFiles, const std::string& outputFile);
void loadLanguageModel(const std::string& inputFile);
ClassEncoder classEncoder;
ClassDecoder classDecoder;
IndexedCorpus* indexedCorpus;
PatternModel<uint32_t> patternModel;
PatternModelOptions patternModelOptions;
PatternSet<uint64_t> vocabulary;
unsigned vocabularySize = 0;
::cpyp::PYPLM<4> lm;
};
} // namespace SLM
#endif /* LANGUAGEMODEL_H_ */