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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
#include "rule_extractor_helper.h"
#include "data_array.h"
#include "alignment.h"
namespace extractor {
RuleExtractorHelper::RuleExtractorHelper(
shared_ptr<DataArray> source_data_array,
shared_ptr<DataArray> target_data_array,
shared_ptr<Alignment> alignment,
int max_rule_span,
int max_rule_symbols,
bool require_aligned_terminal,
bool require_aligned_chunks,
bool require_tight_phrases) :
source_data_array(source_data_array),
target_data_array(target_data_array),
alignment(alignment),
max_rule_span(max_rule_span),
max_rule_symbols(max_rule_symbols),
require_aligned_terminal(require_aligned_terminal),
require_aligned_chunks(require_aligned_chunks),
require_tight_phrases(require_tight_phrases) {}
RuleExtractorHelper::RuleExtractorHelper() {}
RuleExtractorHelper::~RuleExtractorHelper() {}
void RuleExtractorHelper::GetLinksSpans(
vector<int>& source_low, vector<int>& source_high,
vector<int>& target_low, vector<int>& target_high, int sentence_id) const {
int source_sent_len = source_data_array->GetSentenceLength(sentence_id);
int target_sent_len = target_data_array->GetSentenceLength(sentence_id);
source_low = vector<int>(source_sent_len, -1);
source_high = vector<int>(source_sent_len, -1);
target_low = vector<int>(target_sent_len, -1);
target_high = vector<int>(target_sent_len, -1);
vector<pair<int, int> > links = alignment->GetLinks(sentence_id);
for (auto link: links) {
if (source_low[link.first] == -1 || source_low[link.first] > link.second) {
source_low[link.first] = link.second;
}
source_high[link.first] = max(source_high[link.first], link.second + 1);
if (target_low[link.second] == -1 || target_low[link.second] > link.first) {
target_low[link.second] = link.first;
}
target_high[link.second] = max(target_high[link.second], link.first + 1);
}
}
bool RuleExtractorHelper::CheckAlignedTerminals(
const vector<int>& matching,
const vector<int>& chunklen,
const vector<int>& source_low) const {
if (!require_aligned_terminal) {
return true;
}
int sentence_id = source_data_array->GetSentenceId(matching[0]);
int source_sent_start = source_data_array->GetSentenceStart(sentence_id);
int num_aligned_chunks = 0;
for (size_t i = 0; i < chunklen.size(); ++i) {
for (size_t j = 0; j < chunklen[i]; ++j) {
int sent_index = matching[i] - source_sent_start + j;
if (source_low[sent_index] != -1) {
++num_aligned_chunks;
break;
}
}
}
if (num_aligned_chunks == 0) {
return false;
}
return !require_aligned_chunks || num_aligned_chunks == chunklen.size();
}
bool RuleExtractorHelper::CheckTightPhrases(
const vector<int>& matching,
const vector<int>& chunklen,
const vector<int>& source_low) const {
if (!require_tight_phrases) {
return true;
}
int sentence_id = source_data_array->GetSentenceId(matching[0]);
int source_sent_start = source_data_array->GetSentenceStart(sentence_id);
for (size_t i = 0; i + 1 < chunklen.size(); ++i) {
int gap_start = matching[i] + chunklen[i] - source_sent_start;
int gap_end = matching[i + 1] - 1 - source_sent_start;
if (source_low[gap_start] == -1 || source_low[gap_end] == -1) {
return false;
}
}
return true;
}
bool RuleExtractorHelper::FindFixPoint(
int source_phrase_low, int source_phrase_high,
const vector<int>& source_low, const vector<int>& source_high,
int& target_phrase_low, int& target_phrase_high,
const vector<int>& target_low, const vector<int>& target_high,
int& source_back_low, int& source_back_high, int sentence_id,
int min_source_gap_size, int min_target_gap_size,
int max_new_x, bool allow_low_x, bool allow_high_x,
bool allow_arbitrary_expansion) const {
int prev_target_low = target_phrase_low;
int prev_target_high = target_phrase_high;
FindProjection(source_phrase_low, source_phrase_high, source_low,
source_high, target_phrase_low, target_phrase_high);
if (target_phrase_low == -1) {
// TODO(pauldb): Low priority corner case inherited from Adam's code:
// If w is unaligned, but we don't require aligned terminals, returning an
// error here prevents the extraction of the allowed rule
// X -> X_1 w X_2 / X_1 X_2
return false;
}
int source_sent_len = source_data_array->GetSentenceLength(sentence_id);
int target_sent_len = target_data_array->GetSentenceLength(sentence_id);
if (prev_target_low != -1 && target_phrase_low != prev_target_low) {
if (prev_target_low - target_phrase_low < min_target_gap_size) {
target_phrase_low = prev_target_low - min_target_gap_size;
if (target_phrase_low < 0) {
return false;
}
}
}
if (prev_target_high != -1 && target_phrase_high != prev_target_high) {
if (target_phrase_high - prev_target_high < min_target_gap_size) {
target_phrase_high = prev_target_high + min_target_gap_size;
if (target_phrase_high > target_sent_len) {
return false;
}
}
}
if (target_phrase_high - target_phrase_low > max_rule_span) {
return false;
}
source_back_low = source_back_high = -1;
FindProjection(target_phrase_low, target_phrase_high, target_low, target_high,
source_back_low, source_back_high);
int new_x = 0;
bool new_low_x = false, new_high_x = false;
while (true) {
source_back_low = min(source_back_low, source_phrase_low);
source_back_high = max(source_back_high, source_phrase_high);
if (source_back_low == source_phrase_low &&
source_back_high == source_phrase_high) {
return true;
}
if (!allow_low_x && source_back_low < source_phrase_low) {
// Extension on the left side not allowed.
return false;
}
if (!allow_high_x && source_back_high > source_phrase_high) {
// Extension on the right side not allowed.
return false;
}
// Extend left side.
if (source_back_low < source_phrase_low) {
if (new_low_x == false) {
if (new_x >= max_new_x) {
return false;
}
new_low_x = true;
++new_x;
}
if (source_phrase_low - source_back_low < min_source_gap_size) {
source_back_low = source_phrase_low - min_source_gap_size;
if (source_back_low < 0) {
return false;
}
}
}
// Extend right side.
if (source_back_high > source_phrase_high) {
if (new_high_x == false) {
if (new_x >= max_new_x) {
return false;
}
new_high_x = true;
++new_x;
}
if (source_back_high - source_phrase_high < min_source_gap_size) {
source_back_high = source_phrase_high + min_source_gap_size;
if (source_back_high > source_sent_len) {
return false;
}
}
}
if (source_back_high - source_back_low > max_rule_span) {
// Rule span too wide.
return false;
}
prev_target_low = target_phrase_low;
prev_target_high = target_phrase_high;
FindProjection(source_back_low, source_phrase_low, source_low, source_high,
target_phrase_low, target_phrase_high);
FindProjection(source_phrase_high, source_back_high, source_low,
source_high, target_phrase_low, target_phrase_high);
if (prev_target_low == target_phrase_low &&
prev_target_high == target_phrase_high) {
return true;
}
if (!allow_arbitrary_expansion) {
// Arbitrary expansion not allowed.
return false;
}
if (target_phrase_high - target_phrase_low > max_rule_span) {
// Target side too wide.
return false;
}
source_phrase_low = source_back_low;
source_phrase_high = source_back_high;
FindProjection(target_phrase_low, prev_target_low, target_low, target_high,
source_back_low, source_back_high);
FindProjection(prev_target_high, target_phrase_high, target_low,
target_high, source_back_low, source_back_high);
}
return false;
}
void RuleExtractorHelper::FindProjection(
int source_phrase_low, int source_phrase_high,
const vector<int>& source_low, const vector<int>& source_high,
int& target_phrase_low, int& target_phrase_high) const {
for (size_t i = source_phrase_low; i < source_phrase_high; ++i) {
if (source_low[i] != -1) {
if (target_phrase_low == -1 || source_low[i] < target_phrase_low) {
target_phrase_low = source_low[i];
}
target_phrase_high = max(target_phrase_high, source_high[i]);
}
}
}
bool RuleExtractorHelper::GetGaps(
vector<pair<int, int> >& source_gaps, vector<pair<int, int> >& target_gaps,
const vector<int>& matching, const vector<int>& chunklen,
const vector<int>& source_low, const vector<int>& source_high,
const vector<int>& target_low, const vector<int>& target_high,
int source_phrase_low, int source_phrase_high, int source_back_low,
int source_back_high, int& num_symbols, bool& met_constraints) const {
int sentence_id = source_data_array->GetSentenceId(matching[0]);
int source_sent_start = source_data_array->GetSentenceStart(sentence_id);
if (source_back_low < source_phrase_low) {
source_gaps.push_back(make_pair(source_back_low, source_phrase_low));
if (num_symbols >= max_rule_symbols) {
// Source side contains too many symbols.
return false;
}
++num_symbols;
if (require_tight_phrases && (source_low[source_back_low] == -1 ||
source_low[source_phrase_low - 1] == -1)) {
// Inside edges of preceding gap are not tight.
return false;
}
} else if (require_tight_phrases && source_low[source_phrase_low] == -1) {
// This is not a hard error. We can't extract this phrase, but we might
// still be able to extract a superphrase.
met_constraints = false;
}
for (size_t i = 0; i + 1 < chunklen.size(); ++i) {
int gap_start = matching[i] + chunklen[i] - source_sent_start;
int gap_end = matching[i + 1] - source_sent_start;
source_gaps.push_back(make_pair(gap_start, gap_end));
}
if (source_phrase_high < source_back_high) {
source_gaps.push_back(make_pair(source_phrase_high, source_back_high));
if (num_symbols >= max_rule_symbols) {
// Source side contains too many symbols.
return false;
}
++num_symbols;
if (require_tight_phrases && (source_low[source_phrase_high] == -1 ||
source_low[source_back_high - 1] == -1)) {
// Inside edges of following gap are not tight.
return false;
}
} else if (require_tight_phrases &&
source_low[source_phrase_high - 1] == -1) {
// This is not a hard error. We can't extract this phrase, but we might
// still be able to extract a superphrase.
met_constraints = false;
}
target_gaps.resize(source_gaps.size(), make_pair(-1, -1));
for (size_t i = 0; i < source_gaps.size(); ++i) {
if (!FindFixPoint(source_gaps[i].first, source_gaps[i].second, source_low,
source_high, target_gaps[i].first, target_gaps[i].second,
target_low, target_high, source_gaps[i].first,
source_gaps[i].second, sentence_id, 0, 0, 0, false, false,
false)) {
// Gap fails integrity check.
return false;
}
}
return true;
}
vector<int> RuleExtractorHelper::GetGapOrder(
const vector<pair<int, int> >& gaps) const {
vector<int> gap_order(gaps.size());
for (size_t i = 0; i < gap_order.size(); ++i) {
for (size_t j = 0; j < i; ++j) {
if (gaps[gap_order[j]] < gaps[i]) {
++gap_order[i];
} else {
++gap_order[j];
}
}
}
return gap_order;
}
unordered_map<int, int> RuleExtractorHelper::GetSourceIndexes(
const vector<int>& matching, const vector<int>& chunklen,
int starts_with_x) const {
unordered_map<int, int> source_indexes;
int sentence_id = source_data_array->GetSentenceId(matching[0]);
int source_sent_start = source_data_array->GetSentenceStart(sentence_id);
int num_symbols = starts_with_x;
for (size_t i = 0; i < matching.size(); ++i) {
for (size_t j = 0; j < chunklen[i]; ++j) {
source_indexes[matching[i] + j - source_sent_start] = num_symbols;
++num_symbols;
}
++num_symbols;
}
return source_indexes;
}
} // namespace extractor
|