-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcfg_grammar.h
299 lines (266 loc) · 10.9 KB
/
pcfg_grammar.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* pcfg_grammar.h
*
* Created on: Jul 18, 2016
* Author: asaparov
*/
#ifndef PCFG_GRAMMAR_H_
#define PCFG_GRAMMAR_H_
#include "grammar.h"
#include "morphology.h"
#include <math/multiset.h>
template<typename BaseDistribution, typename Likelihood>
struct conjugate_pair {
bool preterminal;
BaseDistribution pi;
array_multiset<unsigned int> rules;
unsigned int nonterminal_count;
hash_map<rule<null_semantics>, array<weighted_feature_set<double>>*> posterior;
hash_map<sequence, unsigned int> token_ids;
inline bool has_nonterminal_rules() const {
return !preterminal;
}
inline bool has_terminal_rules() const {
return preterminal;
}
constexpr bool is_string_preterminal() const {
return false;
}
constexpr part_of_speech get_part_of_speech() const {
return POS_OTHER;
}
inline void clear() {
for (auto entry : posterior) {
core::free(entry.key);
for (unsigned int i = 0; i < entry.value->length; i++)
core::free(entry.value->data[i]);
core::free(*entry.value);
core::free(entry.value);
}
posterior.clear();
}
static inline void free(conjugate_pair<BaseDistribution, Likelihood>& distribution) {
distribution.clear();
core::free(distribution.pi);
core::free(distribution.rules);
for (auto entry : distribution.token_ids)
core::free(entry.key);
core::free(distribution.token_ids);
core::free(distribution.posterior);
}
};
template<typename BaseDistribution, typename Likelihood, typename BaseParameters>
inline bool init(conjugate_pair<BaseDistribution, Likelihood>& distribution,
bool is_preterminal, unsigned int nonterminal_count, const BaseParameters& params)
{
distribution.preterminal = is_preterminal;
distribution.nonterminal_count = nonterminal_count;
if (!init(distribution.pi, params)) {
return false;
} else if (!init(distribution.rules, 16)) {
free(distribution.pi);
return false;
} else if (!hash_map_init(distribution.token_ids, 32)) {
free(distribution.pi);
free(distribution.rules);
return false;
} else if (!hash_map_init(distribution.posterior, 32)) {
free(distribution.pi);
free(distribution.rules);
free(distribution.token_ids);
return false;
}
return true;
}
template<typename BaseDistribution, typename Likelihood, typename Semantics>
using pcfg_grammar = grammar<Semantics, conjugate_pair<BaseDistribution, Likelihood>>;
template<typename BaseDistribution, typename Likelihood, typename Semantics>
inline unsigned int get_rule_id(
conjugate_pair<BaseDistribution, Likelihood>& distribution,
const rule<Semantics>& rule)
{
if (rule.is_terminal()) {
if (!distribution.token_ids.check_size())
exit(EXIT_FAILURE);
bool contains; unsigned int index;
const sequence& terminal = rule.get_terminal();
unsigned int& id = distribution.token_ids.get(terminal, contains, index);
if (!contains) {
sequence::copy(terminal, distribution.token_ids.table.keys[index]);
distribution.token_ids.table.size++;
id = distribution.token_ids.table.size;
}
return id;
} else {
return rule.nt.nonterminals[0] * distribution.nonterminal_count + rule.nt.nonterminals[1];
}
}
template<typename BaseDistribution, typename Likelihood, typename Semantics>
bool add(conjugate_pair<BaseDistribution, Likelihood>& distribution, const rule<Semantics>& rule, const null_semantics& logical_form, unsigned int count = 1)
{
return distribution.rules.add(get_rule_id(distribution, rule), count);
}
template<typename BaseDistribution, typename Likelihood, typename Semantics>
bool remove(conjugate_pair<BaseDistribution, Likelihood>& distribution, const rule<Semantics>& rule, const null_semantics& logical_form)
{
return distribution.rules.subtract(get_rule_id(distribution, rule)) < distribution.rules.counts.size;
}
template<typename BaseDistribution, typename Likelihood, typename Semantics, typename StringMapType>
inline double log_probability(
conjugate_pair<BaseDistribution, Likelihood>& distribution,
const rule<Semantics>& observation,
const Semantics& logical_form,
const StringMapType& token_map)
{
unsigned int length;
weighted<Semantics>* posterior = log_conditional<false, false>(distribution, observation, logical_form, token_map, length);
double weight;
if (length == 0)
weight = -std::numeric_limits<double>::infinity();
else weight = posterior[0].log_probability;
if (posterior != NULL) {
for (unsigned int i = 0; i < length; i++)
free(posterior[i]);
free(posterior);
}
return weight;
}
template<bool DiscardImpossible, bool PruneAmbiguousLogicalForms,
typename BaseDistribution, typename Likelihood, typename Semantics>
array<weighted_feature_set<double>>* log_conditional(
conjugate_pair<BaseDistribution, Likelihood>& distribution,
const rule<Semantics>& observation, const null_semantics& logical_form)
{
if (!distribution.posterior.check_size())
return NULL;
bool contains; unsigned int index;
array<weighted_feature_set<double>>*& list = distribution.posterior.get(observation, contains, index);
if (!contains) {
list = (array<weighted_feature_set<double>>*) malloc(sizeof(array<weighted_feature_set<double>>));
if (list == NULL) return NULL;
if (!array_init(*list, 1)) {
free(list);
return NULL;
}
distribution.posterior.table.keys[index] = observation;
distribution.posterior.table.size++;
}
if (list->length > 0)
return list;
if (!init(list->data[0], 1))
return NULL;
list->length = 1;
if (observation.is_terminal()) {
/* every preterminal is assigned to a single terminal */
if (observation.t.length > 1
|| get_rule_id(distribution, observation) != distribution.rules.counts.keys[0]) {
free(list->data[0]);
list->length = 0;
} else {
list->data[0].log_probability = 0.0;
}
} else {
/* nonterminal rules follow a Dirichlet-multinomial distribution */
list->data[0].log_probability =
Likelihood::log_conditional(distribution.pi, get_rule_id(distribution, observation), distribution.rules);
}
return list;
}
template<bool DiscardImpossible, bool PruneAmbiguousLogicalForms,
typename BaseDistribution, typename Likelihood, typename StringMapType>
inline weighted<null_semantics>* log_conditional(
conjugate_pair<BaseDistribution, Likelihood>& distribution,
const rule<null_semantics>& observation, const null_semantics& logical_form,
const StringMapType& token_map, unsigned int& length)
{
length = 0;
const array<weighted_feature_set<double>>* posterior =
log_conditional<DiscardImpossible, PruneAmbiguousLogicalForms>(distribution, observation, logical_form);
if (posterior == NULL) return NULL;
weighted<null_semantics>* weighted_logical_forms = (weighted<null_semantics>*)
malloc(sizeof(weighted<null_semantics>) * max((size_t) 1, posterior->length));
if (weighted_logical_forms == NULL) {
fprintf(stderr, "log_conditional ERROR: Unable to initialize weighted logical form array.\n");
return NULL;
}
for (unsigned int i = 0; i < posterior->length; i++)
{
weighted<null_semantics>& item = weighted_logical_forms[length];
item.log_probability = posterior->data[i].log_probability;
length++;
}
return weighted_logical_forms;
}
template<typename BaseDistribution, typename Likelihood, typename Semantics>
bool get_rules(const conjugate_pair<BaseDistribution, Likelihood>& distribution,
const null_semantics& logical_form, array<rule<Semantics>>& rules, double min_probability)
{
typedef typename Semantics::function function_type;
/* naively consider every possible rule */
/* TODO: this can obviously be made more efficient */
double log_min_probability = log(min_probability);
for (unsigned int i = 0; i < distribution.nonterminal_count; i++) {
for (unsigned int j = 0; j < distribution.nonterminal_count; j++) {
double log_conditional = Likelihood::log_conditional(
distribution.pi, (i + 1) * distribution.nonterminal_count + (j + 1), distribution.rules);
if (log_conditional + 1.0e-9 > log_min_probability) {
if (!rules.ensure_capacity(rules.length + 1)) return false;
rule<Semantics>& new_rule = rules[(unsigned int) rules.length];
new_rule.type = rule_type::NONTERMINAL;
new_rule.nt.length = 2;
new_rule.nt.nonterminals = (unsigned int*) malloc(sizeof(unsigned int) * new_rule.nt.length);
new_rule.nt.transformations = (transformation<Semantics>*) malloc(sizeof(transformation<Semantics>) * new_rule.nt.length);
new_rule.nt.transformations[0].function_count = 1;
new_rule.nt.transformations[0].functions = (function_type*) malloc(sizeof(function_type) * new_rule.nt.transformations[0].function_count);
new_rule.nt.transformations[0].functions[0] = null_semantics::FUNCTION_IDENTITY;
new_rule.nt.transformations[1].function_count = 1;
new_rule.nt.transformations[1].functions = (function_type*) malloc(sizeof(function_type) * new_rule.nt.transformations[1].function_count);
new_rule.nt.transformations[1].functions[0] = null_semantics::FUNCTION_IDENTITY;
new_rule.nt.nonterminals[0] = i + 1;
new_rule.nt.nonterminals[1] = j + 1;
rules.length++;
}
}
}
return true;
}
template<typename BaseDistribution, typename Likelihood, typename Semantics>
bool get_rules(const conjugate_pair<BaseDistribution, Likelihood>& distribution, array<rule<Semantics>>& rules)
{
typedef typename Semantics::function function_type;
/* for now, just retrieve all rules */
for (unsigned int i = 0; i < distribution.nonterminal_count; i++) {
for (unsigned int j = 0; j < distribution.nonterminal_count; j++) {
if (!rules.ensure_capacity(rules.length + 1)) return false;
rule<Semantics>& new_rule = rules[(unsigned int) rules.length];
new_rule.type = rule_type::NONTERMINAL;
new_rule.nt.length = 2;
new_rule.nt.nonterminals = (unsigned int*) malloc(sizeof(unsigned int) * new_rule.nt.length);
new_rule.nt.transformations = (transformation<Semantics>*) malloc(sizeof(transformation<Semantics>) * new_rule.nt.length);
new_rule.nt.transformations[0].function_count = 1;
new_rule.nt.transformations[0].functions = (function_type*) malloc(sizeof(function_type) * new_rule.nt.transformations[0].function_count);
new_rule.nt.transformations[0].functions[0] = null_semantics::FUNCTION_IDENTITY;
new_rule.nt.transformations[1].function_count = 1;
new_rule.nt.transformations[1].functions = (function_type*) malloc(sizeof(function_type) * new_rule.nt.transformations[1].function_count);
new_rule.nt.transformations[1].functions[0] = null_semantics::FUNCTION_IDENTITY;
new_rule.nt.nonterminals[0] = i + 1;
new_rule.nt.nonterminals[1] = j + 1;
rules.length++;
}
}
return true;
}
template<typename BaseDistribution, typename Likelihood>
double max_log_conditional(
conjugate_pair<BaseDistribution, Likelihood>& distribution,
const rule<null_semantics>& observation, const null_semantics& logical_form,
const string** token_map)
{
array<weighted_feature_set<double>>* posterior = log_conditional<true, false>(distribution, observation, logical_form);
if (posterior == NULL)
exit(EXIT_FAILURE);
else if (posterior->length == 0)
return -std::numeric_limits<double>::infinity();
return posterior->data[0].log_probability;
}
#endif /* PCFG_GRAMMAR_H_ */