/
lplmapping.c
476 lines (384 loc) · 11.1 KB
/
lplmapping.c
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* lplmapping.c, 11 Apr 15 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pcre.h>
#include "spellchk.h"
#include "lplmapping.h"
#define DEBUG_L2P 0
#define DEBUG_P2L 0
int trace_letchk_fail,
print_rule_match,
print_phon_seq,
do_word_to_phon,
do_phon_to_word;
pcre *let_re,
*phon_re;
int num_left_context;
pcre *(left_context_pat[MAX_LEFT_CONTEXT_PAT]);
short l_match_pos[MAX_PHON_SEQ_LEN],
p_match_pos[MAX_PHON_SEQ_LEN];
int l_word_len,
num_rules;
int p2l_callout_num;
char l_rev_word[MAX_WORD_LEN];
char gen_word[MAX_WORD_LEN];
char rule_phon_list[MAX_MAPPING_RULES][MAX_LET_PHON_LEN];
int num_phon_left_pat;
int phon_let_left_ind[MAX_MAPPING_RULES];
pcre *(phon_left_pat[MAX_MAPPING_RULES]);
char phon_rule_let_seq[MAX_MAPPING_RULES][MAX_LET_SEQ_LEN];
pcre * (phon_right_pat[MAX_MAPPING_RULES]);
// Phoneme symbol/single character mapping variable
struct phon_hash_rec phon_hash_tab[MAX_DIFF_PHON];
int num_diff_phon = 0;
char generated_words[MAX_GEN_WORDS][MAX_WORD_LEN];
int num_generated_words;
int left_matches(pcre_callout_block *match_info)
{
/*
* Match the left context of a pattern.
* Works by matching left to right on the reversed character sequence.
*/
int rc = pcre_exec(
left_context_pat[match_info->callout_number],
NULL, // we didn't study the pattern
l_rev_word+
(l_word_len-match_info->start_match), // subject string
match_info->start_match+1, // length of the subject string
0, // start at offset 0 in the subject
0, // default options
NULL, // vector of integers for substring information
0); // number of elements (NOT size in bytes)
if (rc < -1)
printf("Error: something unexpected happened in lm.pcre_exec: %d\n", rc);
#if DEBUG_L2P
printf("%d %s %d %s %d\n", match_info->callout_number,
l_rev_word,
match_info->start_match,
l_rev_word+
(l_word_len-match_info->start_match),
rc);
#endif
return rc < 0; // phon_call needs to return 0 for the match to continue
}
int let2phon_callout(pcre_callout_block *match_info)
{
int i;
#if 0
PRINT_FIELD(subject_length);
PRINT_FIELD(start_match);
PRINT_FIELD(current_position);
PRINT_FIELD(capture_top);
PRINT_FIELD(capture_last);
PRINT_FIELD(pattern_position);
PRINT_FIELD(next_item_length);
/*
for (i=0; i < match_info->capture_top; i++)
printf(": %d", match_info->offset_vector[i]);
printf("\n");
*/
#endif
/*
* A pcre ?C callout has been encountered during pattern match.
* It could be information about a left context that needs matching
* or the rule number we have just matched.
*/
if (match_info->callout_number <= num_left_context)
return left_matches(match_info);
if (match_info->callout_number >= CALLOUT_START+CALLOUT_NUM_BASE)
l_match_pos[match_info->start_match]=9*CALLOUT_NUM_BASE+
match_info->callout_number;
else
{
for (i=match_info->start_match+1; i < MAX_MATCH_POINTS; i++)
l_match_pos[i]=0;
if (l_match_pos[match_info->start_match] > 10*CALLOUT_NUM_BASE)
l_match_pos[match_info->start_match]=CALLOUT_NUM_BASE*
(l_match_pos[match_info->start_match]-10*CALLOUT_NUM_BASE-CALLOUT_START)+
match_info->callout_number-CALLOUT_START;
else
l_match_pos[match_info->start_match]=match_info->callout_number-
CALLOUT_START;
}
#if DEBUG_L2P
printf("(%d:%d) ", match_info->callout_number-CALLOUT_START,
match_info->start_match);
printf("%d", l_match_pos[match_info->start_match]);
#endif
return 0;
}
int match_gen_word(int rule_num)
{
char p_rev_word[MAX_WORD_LEN];
int i,
rc,
gen_word_len;
gen_word_len=strlen(gen_word);
for (i=0; i < gen_word_len; i++)
p_rev_word[gen_word_len-1-i]=gen_word[i];
p_rev_word[gen_word_len]='\0';
#if DEBUG_P2L
printf("rw: %s %d", p_rev_word, rule_num);
#endif
rc = pcre_exec(
phon_left_pat[phon_let_left_ind[rule_num]], // result of pcre_compile()
NULL, // we didn't study the pattern
p_rev_word, // subject string
gen_word_len, // length of the subject string
0, // start at offset 0 in the subject
0, // default options
NULL, // vector of integers for substring information
0); // number of elements (NOT size in bytes)
#if DEBUG_P2L
printf(" rc %d\n", rc);
#endif
if (rc < -1)
printf("Error: something unexpected happened in gw.pcre_exec: %d\n", rc);
return rc >= 0;
}
void save_gen_word(char *gen_word)
{
int i;
for (i=0; i < num_generated_words; i++)
if (strcmp(generated_words[i], gen_word) == 0)
return;
if (num_generated_words < MAX_GEN_WORDS)
{
strcpy(generated_words[num_generated_words], gen_word);
num_generated_words++;
}
}
void check_gen_words(phon_seq_t phon_seq)
{
// Addtional checks on the generated words
int i;
phon_seq_t gen_word_phon;
for (i=0; i < num_generated_words; i++)
{
// Does the letter sequence following characteristics of English?
if (let_seq_ok(generated_words[i]))
{
gen_word_phon=let2phon(generated_words[i], FALSE);
if (strcmp(gen_word_phon.phon_list, phon_seq.phon_list) != 0)
generated_words[i][0]='\0';
}
else
{
if (trace_letchk_fail)
printf("letchk fail: %s\n", generated_words[i]);
generated_words[i][0]='\0';
}
}
for (i=0; i < num_generated_words; i++)
{
if (generated_words[i][0] != '\0')
printf("%s\n", generated_words[i]);
}
}
int phon2let_callout(pcre_callout_block *match_info)
{
int i;
#if 0
PRINT_FIELD(subject_length);
PRINT_FIELD(start_match);
PRINT_FIELD(current_position);
PRINT_FIELD(capture_top);
PRINT_FIELD(capture_last);
PRINT_FIELD(pattern_position);
PRINT_FIELD(next_item_length);
/*
for (i=0; i < match_info->capture_top; i++)
printf(": %d", match_info->offset_vector[i]);
printf("\n");
*/
#endif
/*
* A pcre ?C callout has been encountered during pattern match.
* It could be information about a left context that needs matching
* or the rule number we have just matched.
*/
if (match_info->callout_number == 255) // Unconditionally fail
{
save_gen_word(gen_word);
return 1;
}
if (match_info->callout_number >= CALLOUT_NUM_BASE)
{
/*
* Adding in another CALLOUT_NUM_BASE lets the next call
* distinguish between a 'stale' value and a partially
* created value that it needs to complete.
*/
p2l_callout_num=9*CALLOUT_NUM_BASE+match_info->callout_number;
return 0;
}
else
{
if (p2l_callout_num > 10*CALLOUT_NUM_BASE)
p2l_callout_num=CALLOUT_NUM_BASE*
(p2l_callout_num-10*CALLOUT_NUM_BASE)+
match_info->callout_number;
else
p2l_callout_num=match_info->callout_number;
if (match_gen_word(p2l_callout_num))
{
for (i=match_info->start_match+1; i < MAX_MATCH_POINTS; i++)
p_match_pos[i]=0;
if (match_info->start_match == 0)
gen_word[0]='\0';
else
{
if (p_match_pos[match_info->start_match] == 0)
p_match_pos[match_info->start_match]=strlen(gen_word);
else
gen_word[p_match_pos[match_info->start_match]]='\0';
}
strcat(gen_word, phon_rule_let_seq[p2l_callout_num]);
}
else
return 1;
}
#if DEBUG_P2L
printf("[%d:%d:%d] ", match_info->callout_number,
match_info->start_match,
p_match_pos[match_info->start_match]);
#endif
return 0;
}
char phon_to_mapch(char *p_str)
{
int i;
for (i=0; i < num_diff_phon; i++)
if (strcmp(phon_hash_tab[i].phon_str, p_str) == 0)
return phon_hash_tab[i].mapch;
printf("Unknown phoneme: %s\n", p_str);
return '\0';
}
char *mapch_to_phon(char ch)
{
int i;
for (i=0; i < num_diff_phon; i++)
if (phon_hash_tab[i].mapch == ch)
return phon_hash_tab[i].phon_str;
printf(">> Internal error (mapch phon): %c\n", ch);
return "";
}
void print_phoneme_seq(phon_seq_t ph_seq)
{
printf("%s\n", ph_seq.phon_list);
}
phon_seq_t phon_let2str(phon_seq_t phon_seq)
{
phon_seq_t phon_vseq;
char *p_str = phon_seq.phon_list;
phon_vseq.phon_list[0]='\0';
while (*p_str != '\0')
{
strcat(phon_vseq.phon_list, mapch_to_phon(*p_str));
strcat(phon_vseq.phon_list, " ");
p_str++;
}
return phon_vseq;
}
phon_seq_t let2phon(char *word_str, BOOL do_print)
{
int rc;
int i;
int substr_vec[MAX_SUBSTRING_ELEM];
phon_seq_t phon_seq;
#if DEBUG_L2P
printf(">> %s\n", word_str);
#endif
phon_seq.phon_list[0]='\0';
l_word_len=strlen(word_str);
if (l_word_len == 0)
return phon_seq;
l_match_pos[0]=0;
pcre_callout=let2phon_callout;
for (i=0; i < l_word_len; i++)
l_rev_word[l_word_len-1-i]=word_str[i];
l_rev_word[l_word_len]='\0';
for (i=0; i < MAX_SUBSTRING_ELEM; i++)
l_match_pos[i]=0;
rc = pcre_exec(
let_re, // result of pcre_compile()
NULL, // we didn't study the pattern
word_str, // subject string
l_word_len, // length of the subject string
0, // start at offset 0 in the subject
0, // default options
substr_vec, // vector of integers for substring information
MAX_SUBSTRING_ELEM); // number of elements (NOT size in bytes)
if (rc < -1)
printf("Error: something unexpected happened in lp.pcre_exec: %d\n", rc);
for (i=0; i < MAX_SUBSTRING_ELEM; i++)
{
if (l_match_pos[i] != 0)
{
strcat(phon_seq.phon_list, rule_phon_list[l_match_pos[i]]);
if (do_print && print_rule_match)
printf("%3d %s\n", l_match_pos[i],
mapch_to_phon(*rule_phon_list[l_match_pos[i]]));
}
}
if (do_print && print_phon_seq)
{
phon_seq_t dummy = phon_let2str(phon_seq); // hostage to l-value rules
printf("%s\n", dummy.phon_list);
}
return phon_seq;
}
let_seq_t phon2let(phon_seq_t phon_seq)
{
int i,
rc,
phon_len;
int substr_vec[MAX_SUBSTRING_ELEM];
let_seq_t let_seq;
num_generated_words=0;
let_seq.let_list[0]='\0';
phon_len=strlen(phon_seq.phon_list);
if (phon_len == 0)
return let_seq;
for (i=0; i < MAX_SUBSTRING_ELEM; i++)
p_match_pos[i]=0;
gen_word[0]='\0';
pcre_callout=phon2let_callout;
#if DEBUG_P2L
printf(">> %s | ", phon_seq.phon_list);
print_phoneme_seq(phon_let2str(phon_seq));
#endif
rc = pcre_exec(
phon_re, // result of pcre_compile()
NULL, // we didn't study the pattern
phon_seq.phon_list, // subject string
phon_len, // length of the subject string
0, // start at offset 0 in the subject
0, // default options
substr_vec, // vector of integers for substring information
MAX_SUBSTRING_ELEM); // number of elements (NOT size in bytes)
if (rc < -1)
printf("Error: something unexpected happened in pcre_exec: %d\n", rc);
#if DEBUG_P2L
{
printf("rc %d %s\n", rc, gen_word);
}
#endif
check_gen_words(phon_seq);
//printf("<< %s\n", let_seq.let_list);
return let_seq;
}
pcre *compile_pat(char *new_pat, int options)
{
const char *err_str;
int err_offset;
pcre *new_re=pcre_compile(new_pat,
PCRE_ANCHORED | options, &err_str, &err_offset, NULL);
if (new_re == NULL)
{
printf("Pattern compiler failed: %s (%d)\n", err_str, err_offset);
exit(1);
}
return new_re;
}