Debian Codegraph @

modules/metaphone2/metaphone2.c

raw
1 /* This file is part of GNU Dico.
2 Copyright (C) 2008-2024 Sergey Poznyakoff
3
4 GNU Dico is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Dico is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <dico.h>
19 #include <appi18n.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <assert.h>
23
24 #define METAPH_SEGM_SIZE 16
25
26 struct metaph_segment {
27 char segm[METAPH_SEGM_SIZE];
28 struct metaph_segment *next;
29 };
30
31 struct metaph_code {
32 size_t length;
33 size_t nsegm;
34 struct metaph_segment *segm_head, *segm_tail;
35 };
36
37 static size_t
38 metaph_code_length(struct metaph_code *code)
39 {
40 return code ? code->length : 0;
41 }
42
43 static void
44 metaph_code_trim(struct metaph_code *code, size_t length)
45 {
46 if (code && code->length > length)
47 code->length = length;
48 }
49
50 static struct metaph_code *
51 metaph_code_create(void)
52 {
53 struct metaph_code *code = malloc(sizeof *code);
54 if (code) {
55 code->length = 0;
56 code->nsegm = 0;
57 code->segm_head = code->segm_tail = NULL;
58 }
59 return code;
60 }
61
62 static struct metaph_segment *
63 metaph_code_alloc_segment(struct metaph_code *code)
64 {
65 struct metaph_segment *newseg = malloc(sizeof newseg[0]);
66 if (!newseg)
67 return NULL;
68 newseg->next = NULL;
69 memset(newseg->segm, 0, METAPH_SEGM_SIZE);
70 if (code->segm_tail)
71 code->segm_tail->next = newseg;
72 else
73 code->segm_head = newseg;
74 code->segm_tail = newseg;
75 code->nsegm++;
76 return newseg;
77 }
78
79 static struct metaph_code *
80 metaph_code_dup(struct metaph_code *src)
81 {
82 struct metaph_code *dst = metaph_code_create();
83 struct metaph_segment *src_segm, *dst_segm;
84 if (!dst)
85 return NULL;
86 for (src_segm = src->segm_head; src_segm; src_segm = src_segm->next) {
87 dst_segm = metaph_code_alloc_segment(dst);
88 memcpy(dst_segm->segm, src_segm->segm, METAPH_SEGM_SIZE);
89 }
90 dst->length = src->length;
91 return dst;
92 }
93
94 static void
95 metaph_code_free(struct metaph_code *code)
96 {
97 if (code) {
98 struct metaph_segment *s = code->segm_head;
99
100 while (s) {
101 struct metaph_segment *next = s->next;
102 free(s);
103 s = next;
104 }
105 free(code);
106 }
107 }
108
109 static int
110 metaph_code_eq(struct metaph_code *a, struct metaph_code *b)
111 {
112 struct metaph_segment *sa, *sb;
113 size_t length;
114
115 if (a == NULL || b == NULL)
116 return 0;
117
118 if (a->length != b->length)
119 return 0;
120 length = a->length;
121 sa = a->segm_head;
122 sb = b->segm_head;
123 while (length) {
124 size_t segm_length = length < METAPH_SEGM_SIZE
125 ? length : METAPH_SEGM_SIZE;
126 if (memcmp(sa->segm, sb->segm, segm_length))
127 return 0;
128 length -= segm_length;
129 sa = sa->next;
130 sb = sb->next;
131 }
132 return 1;
133 }
134
135 static int
136 metaph_code_get_buffer(struct metaph_code *code,
137 char **ret_ptr, size_t *ret_size)
138 {
139 size_t s = code->nsegm * METAPH_SEGM_SIZE - code->length;
140 if (s == 0) {
141 if (!metaph_code_alloc_segment(code))
142 return -1;
143 s = METAPH_SEGM_SIZE;
144 }
145 *ret_ptr = code->segm_tail->segm + code->length % METAPH_SEGM_SIZE;
146 *ret_size = s;
147 return 0;
148 }
149
150 static int
151 metaph_code_add(struct metaph_code *code, char const *str)
152 {
153 if (str) {
154 size_t length = strlen(str);
155
156 while (length) {
157 char *p;
158 size_t n;
159
160 if (metaph_code_get_buffer(code, &p, &n))
161 return -1;
162 if (n > length)
163 n = length;
164 memcpy(p, str, n);
165 str += n;
166 code->length += n;
167 length -= n;
168 }
169 }
170 return 0;
171 }
172
173 static void
174 metaph_code_dump(struct metaph_code *code)
175 {
176 if (!code)
177 printf("%s\n", "NULL");
178 else {
179 struct metaph_segment *s;
180 size_t length;
181
182 printf("length = %zu\n", code->length);
183 printf("nsegm = %zu\n", code->nsegm);
184 length = code->length;
185 for (s = code->segm_head; s; s = s->next) {
186 size_t i;
187
188 putchar('\'');
189 for (i = 0; length > 0 && i < METAPH_SEGM_SIZE; i++, length--)
190 putchar(s->segm[i]);
191 printf("'\n");
192 }
193 }
194 }
195
196 static void
197 metaph_code_print(struct metaph_code *code)
198 {
199 if (!code)
200 printf("%s\n", "NULL");
201 else {
202 struct metaph_segment *s;
203 size_t length;
204
205 length = code->length;
206 for (s = code->segm_head; s; s = s->next) {
207 size_t i;
208 for (i = 0; length > 0 && i < METAPH_SEGM_SIZE; i++, length--)
209 putchar(s->segm[i]);
210 }
211 }
212 }
213
214 typedef struct metaph_code *double_metaphone_code[2];
215
216 static int
217 double_metaphone_add(double_metaphone_code code,
218 char const *primary,
219 char const *secondary)
220 {
221 if (secondary) {
222 if (!code[1]) {
223 code[1] = metaph_code_dup(code[0]);
224 if (!code[1]) {
225 DICO_LOG_MEMERR();
226 return -1;
227 }
228 }
229 if (metaph_code_add(code[1], secondary))
230 return -1;
231 } else if (code[1]) {
232 if (metaph_code_add(code[1], primary))
233 return -1;
234 }
235 metaph_code_add(code[0], primary);
236 return 0;
237 }
238
239 /* Return true if POS + OFF is within the STR and the substring at
240 STR[POS + OFF] matches one of the alternatives in PAT. Alternatives
241 are delimited by | characters.
242 */
243 static int
244 looking_at(unsigned const *str, size_t pos, int off, char const *pat)
245 {
246 size_t i;
247
248 if (off < 0 && pos < -off)
249 return 0;
250 pos += off;
251 i = pos;
252 while (*pat) {
253 if (*pat == str[i]) {
254 ++i;
255 ++pat;
256 if (!*pat || *pat == '|')
257 return 1;
258 } else {
259 /* reset */
260 while (*pat && *pat != '|')
261 ++pat;
262 if (*pat)
263 ++pat;
264 i = pos;
265 }
266 }
267 return 0;
268 }
269
270 static void
271 double_metaphone_free(double_metaphone_code code)
272 {
273 metaph_code_free(code[0]);
274 metaph_code_free(code[1]);
275 }
276
277 #define ISVOWEL(s,p,i) looking_at(s, p, i, "A|E|I|O|U|Y")
278
279 static int
280 is_slavo_germanic(unsigned const *str)
281 {
282 static unsigned pat[] = {
283 'W', 0,
284 'K', 0,
285 'C', 'Z', 0,
286 'W', 'I', 'T', 'Z', 0,
287 0
288 };
289 int i;
290
291 for (i = 0; pat[i]; i++) {
292 if (utf8_wc_strstr(str, pat))
293 return 1;
294 while (pat[i])
295 ++i;
296 }
297 return 0;
298 }
299
300 static int
301 double_metaphone_encode(double_metaphone_code code,
302 char const *str, size_t max_length)
303 {
304 unsigned *buf; /* STR converted to UTF */
305 size_t current = 0; /* Current position */
306 size_t length, last;
307 #define DMETAPH_ADD(c,a,b) \
308 do { \
309 if (double_metaphone_add(c,a,b)) \
310 goto err; \
311 } while (0)
312 int slavo_germanic = -1;
313 #define IS_SLAVO_GERMANIC() \
314 ((slavo_germanic == -1) \
315 ? (slavo_germanic = is_slavo_germanic(buf)) : slavo_germanic)
316
317 if (utf8_mbstr_to_wc(str, &buf, NULL)) {
318 dico_log(L_ERR, errno, "%s: cannot convert \"%s\"", __func__, str);
319 return -1;
320 }
321 length = utf8_wc_strlen(buf);
322 last = length - 1;
323
324 code[0] = metaph_code_create();
325 if (!code[0]) {
326 dico_log(L_ERR, 0, _("%s: not enough memory"), __func__);
327 free(buf);
328 return -1;
329 }
330 code[1] = NULL;
331
332 utf8_wc_strupper(buf);
333
334 current = 0;
335 if (looking_at(buf, current, 0, "GN|KN|PN|WR|PS"))
336 ++current;
337
338 while (current < length
339 && (max_length == 0
340 || metaph_code_length(code[0]) < max_length
341 || metaph_code_length(code[1]) < max_length)) {
342 switch (buf[current]) {
343 case 'A':
344 case 'E':
345 case 'I':
346 case 'O':
347 case 'U':
348 case 'Y':
349 if (current == 0)
350 /* Map initial vowel to A */
351 DMETAPH_ADD(code, "A", NULL);
352 current++;
353 break;
354
355 case 'B':
356 DMETAPH_ADD(code, "P", NULL);
357 ++current;
358 if (buf[current] == 'B')
359 ++current;
360 break;
361
362 case L'Ç':
363 DMETAPH_ADD(code, "S", NULL);
364 ++current;
365 break;
366
367 case 'C':
368 /* Various Germanic */
369 if (current > 1
370 && !ISVOWEL(buf, current, -2)
371 && looking_at(buf, current, -1, "ACH")
372 && buf[current + 2] != 'I'
373 && (buf[current + 2] != 'E'
374 || looking_at(buf, current, -2, "BACHER|MACHER"))) {
375 DMETAPH_ADD(code, "K", NULL);
376 current += 2;
377 break;
378 }
379
380 /* Special case 'caesar' */
381 if (current == 0 && looking_at(buf, current, 0, "CAESAR")) {
382 DMETAPH_ADD(code, "S", NULL);
383 current += 2;
384 break;
385 }
386
387 /* Italian 'chianti' */
388 if (looking_at(buf, current, 0, "CHIA")) {
389 DMETAPH_ADD(code, "K", NULL);
390 current += 2;
391 break;
392 }
393
394 if (looking_at(buf, current, 0, "CH")) {
395 /* 'michael'? */
396 if (current > 0 && looking_at(buf, current, 0, "CHAE")) {
397 DMETAPH_ADD(code, "K", "X");
398 current += 2;
399 break;
400 }
401
402 /* Greek roots e.g. 'chemistry', 'chorus' */
403 if (current == 0
404 && (looking_at(buf, current, 1,
405 "HARAC|HARIS|HOR|HYM|HIA|HEM"))
406 && !looking_at(buf, 0, 0, "CHORE")) {
407 DMETAPH_ADD(code, "K", NULL);
408 current += 2;
409 break;
410 }
411
412 /* Germanic, greek, or otherwise 'ch' for 'kh' sound */
413 if ((looking_at(buf, 0, 0, "VAN |VON ")
414 || looking_at(buf, 0, 0, "SCH"))
415 /* 'architect but not 'arch', 'orchestra', 'orchid' */
416 || looking_at(buf, current, -2, "ORCHES|ARCHIT|ORCHID")
417 || looking_at(buf, current, +2, "T|S")
418 || ((looking_at(buf, current, -1, "A|O|U|E") || current == 0)
419 /* e.g., 'wachtler', 'wechsler', but not 'tichner' */
420 && looking_at(buf, current, +2,
421 "L|R|N|M|B|H|F|V|W| "))) {
422 DMETAPH_ADD(code, "K", NULL);
423 } else {
424 if (current > 0) {
425 if (looking_at(buf, 0, 0, "MC"))
426 /* e.g., "McHugh" */
427 DMETAPH_ADD(code, "K", NULL);
428 else
429 DMETAPH_ADD(code, "X", "K");
430 } else
431 DMETAPH_ADD(code, "X", NULL);
432 }
433 current += 2;
434 break;
435 }
436 /* E.g, 'czerny' */
437 if (looking_at(buf, current, 0, "CZ")
438 && !looking_at(buf, current, -2, "WICZ")) {
439 DMETAPH_ADD(code, "S", "X");
440 current += 2;
441 break;
442 }
443
444 /* E.g., 'focaccia' */
445 if (looking_at(buf, current, +1, "CIA")) {
446 DMETAPH_ADD(code, "X", NULL);
447 current += 3;
448 break;
449 }
450
451 /* Double 'C', but not if e.g. 'McClellan' */
452 if (looking_at(buf, current, 0, "CC") &&
453 !(current == 1 && buf[0] == 'M')) {
454 /* 'bellocchio' but not 'bacchus' */
455 if (looking_at(buf, current, +2, "I|E|H")
456 && !looking_at(buf, current, +2, "HU")) {
457 /* 'accident', 'accede' 'succeed' */
458 if ((current == 1 && buf[current - 1] == 'A')
459 || looking_at(buf, current, -1, "UCCEE|UCCES"))
460 DMETAPH_ADD(code, "KS", NULL);
461 /* 'bacci', 'bertucci', other italian */
462 else
463 DMETAPH_ADD(code, "X", NULL);
464 current += 3;
465 break;
466 } else {
467 /* Pierce's rule */
468 DMETAPH_ADD(code, "K", NULL);
469 current += 2;
470 break;
471 }
472 }
473
474 if (looking_at(buf, current, 0, "CK|CG|CQ")) {
475 DMETAPH_ADD(code, "K", NULL);
476 current += 2;
477 break;
478 }
479
480 if (looking_at(buf, current, 0, "CI|CE|CY")) {
481 /* Italian vs. english */
482 if (looking_at(buf, current, 0, "CIO|CIE|CIA"))
483 DMETAPH_ADD(code, "S", "X");
484 else
485 DMETAPH_ADD(code, "S", NULL);
486 current += 2;
487 break;
488 }
489
490 DMETAPH_ADD(code, "K", NULL);
491
492 /* Name sent in 'mac caffrey', 'mac gregor' */
493 if (looking_at(buf, current, +1, " C| Q| G"))
494 current += 3;
495 else
496 if (looking_at(buf, current, +1, "C|K|Q")
497 && !looking_at(buf, current, +1, "CE|CI"))
498 current += 2;
499 else
500 current++;
501 break;
502
503 case 'D':
504 if (looking_at(buf, current, 0, "DG")) {
505 if (looking_at(buf, current, +2, "I|E|Y")) {
506 /* E.g. 'edge' */
507 DMETAPH_ADD(code, "J", NULL);
508 current += 3;
509 break;
510 } else {
511 /* E.g. 'edgar' */
512 DMETAPH_ADD(code, "TK", NULL);
513 current += 2;
514 break;
515 }
516 }
517
518 if (looking_at(buf, current, 0, "DT|DD")) {
519 DMETAPH_ADD(code, "T", NULL);
520 current += 2;
521 break;
522 }
523 DMETAPH_ADD(code, "T", NULL);
524 current++;
525 break;
526
527 case 'F':
528 ++current;
529 if (buf[current] == 'F')
530 ++current;
531 DMETAPH_ADD(code, "F", NULL);
532 break;
533
534 case 'G':
535 if (buf[current + 1] == 'H') {
536 if ((current > 0) && !ISVOWEL(buf, current, -1)) {
537 DMETAPH_ADD(code, "K", NULL);
538 current += 2;
539 break;
540 }
541
542 if (current < 3) {
543 /* 'ghislane', 'ghiradelli' */
544 if (current == 0) {
545 if (buf[current + 2] == 'I')
546 DMETAPH_ADD(code, "J", NULL);
547 else
548 DMETAPH_ADD(code, "K", NULL);
549 current += 2;
550 break;
551 }
552 }
553 /* Parker's rule (with some further refinements) - e.g., 'hugh'
554 */
555 if ((current > 1
556 && looking_at(buf, current, -2, "B|H|D"))
557 /* e.g., 'bough' */
558 || (current > 2 && looking_at(buf, current, -3, "B|H|D"))
559 /* e.g., 'broughton' */
560 || (current > 3 && looking_at(buf, current, -4, "B|H"))) {
561 current += 2;
562 break;
563 } else {
564 /* e.g., 'laugh', 'McLaughlin', 'cough', 'gough', 'rough',
565 'tough'
566 */
567 if (current > 2
568 && buf[current - 1] == 'U'
569 && looking_at(buf, current, -3, "C|G|L|R|T")) {
570 DMETAPH_ADD(code, "F", NULL);
571 } else if (current > 0 && buf[current - 1] != 'I')
572 DMETAPH_ADD(code, "K", NULL);
573
574 current += 2;
575 break;
576 }
577 }
578
579 if (buf[current + 1] == 'N') {
580 if (current == 1 && ISVOWEL(buf, 0, 0) && !IS_SLAVO_GERMANIC()) {
581 DMETAPH_ADD(code, "KN", "N");
582 } else
583 /* not e.g. 'cagney' */
584 if (!looking_at(buf, current, +2, "EY")
585 && (buf[current + 1] != 'Y') && !IS_SLAVO_GERMANIC()) {
586 DMETAPH_ADD(code, "N", "KN");
587 } else
588 DMETAPH_ADD(code, "KN", NULL);
589 current += 2;
590 break;
591 }
592 /* 'tagliaro' */
593 if (looking_at(buf, current, +1, "LI") && !IS_SLAVO_GERMANIC()) {
594 DMETAPH_ADD(code, "KL", "L");
595 current += 2;
596 break;
597 }
598 /* -ges-,-gep-,-gel-, -gie- at the beginning */
599 if (current == 0
600 && (buf[current + 1] == 'Y'
601 || looking_at(buf, current, +1,
602 "ES|EP|EB|EL|EY|IB|IL|IN|IE|EI|ER"))) {
603 DMETAPH_ADD(code, "K", "J");
604 current += 2;
605 break;
606 }
607 /* -ger-, -gy- */
608 if ((looking_at(buf, current, +1, "ER")
609 || (buf[current + 1] == 'Y'))
610 && !looking_at(buf, 0, 0, "DANGER|RANGER|MANGER")
611 && !looking_at(buf, current, -1, "E|I|RGY|OGY")) {
612 DMETAPH_ADD(code, "K", "J");
613 current += 2;
614 break;
615 }
616 /* italian e.g, 'biaggi' */
617 if (looking_at(buf, current, +1, "E|I|Y")
618 || looking_at(buf, current, -1, "AGGI|OGGI")) {
619 /* obvious germanic */
620 if ((looking_at(buf, 0, 0, "VAN |VON ")
621 || looking_at(buf, 0, 0, "SCH"))
622 || looking_at(buf, current, +1, "ET"))
623 DMETAPH_ADD(code, "K", NULL);
624 else
625 /* always soft if french ending */
626 if (looking_at(buf, current, +1, "IER "))
627 DMETAPH_ADD(code, "J", NULL);
628 else
629 DMETAPH_ADD(code, "J", "K");
630 current += 2;
631 break;
632 }
633
634 ++current;
635 if (buf[current] == 'G')
636 ++current;
637 DMETAPH_ADD(code, "K", NULL);
638 break;
639
640 case 'H':
641 /* only keep if first & before vowel or btw. 2 vowels */
642 if ((current == 0 || ISVOWEL(buf, current, -1))
643 && ISVOWEL(buf, current, +1)) {
644 DMETAPH_ADD(code, "H", NULL);
645 current += 2;
646 } else /* also takes care of 'HH' */
647 current++;
648 break;
649
650 case 'J':
651 /* obvious spanish, 'jose', 'san jacinto' */
652 if (looking_at(buf, current, 0, "JOSE")
653 || looking_at(buf, 0, 0, "SAN ")) {
654 if ((current == 0 && buf[current + 4] == ' ')
655 || looking_at(buf, 0, 0, "SAN "))
656 DMETAPH_ADD(code, "H", NULL);
657 else
658 DMETAPH_ADD(code, "J", "H");
659
660 current++;
661 break;
662 }
663
664 if (current == 0 && !looking_at(buf, current, 0, "JOSE"))
665 DMETAPH_ADD(code, "J", "A"); /* Yankelovich | Jankelowicz */
666 else
667 /* spanish pron. of e.g. 'bajador' */
668 if (ISVOWEL(buf, current, -1)
669 && !IS_SLAVO_GERMANIC()
670 && (buf[current + 1] == 'A' || buf[current + 1] == 'O'))
671 DMETAPH_ADD(code, "J", "H");
672 else if (current == last)
673 DMETAPH_ADD(code, "J", " ");
674 else if (!looking_at(buf, current, +1, "L|T|K|S|N|M|B|Z")
675 && !looking_at(buf, current, -1, "S|K|L"))
676 DMETAPH_ADD(code, "J", NULL);
677
678 ++current;
679 if (buf[current] == 'J')
680 ++current;
681 break;
682
683 case 'K':
684 ++current;
685 if (buf[current] == 'K')
686 ++current;
687 DMETAPH_ADD(code, "K", NULL);
688 break;
689
690 case 'L':
691 if (buf[current + 1] == 'L') {
692 /* spanish e.g. 'cabrillo', 'gallegos' */
693 if ((current == length - 3
694 && looking_at(buf, current, -1, "ILLO|ILLA|ALLE"))
695 || ((looking_at(buf, last, -1, "AS|OS")
696 || looking_at(buf, last, 0, "A|O"))
697 && looking_at(buf, current, -1, "ALLE"))) {
698 DMETAPH_ADD(code, "L", " ");
699 current += 2;
700 break;
701 }
702 current += 2;
703 } else
704 current++;
705 DMETAPH_ADD(code, "L", NULL);
706 break;
707
708 case 'M':
709 if ((looking_at(buf, current, -1, "UMB")
710 && (current + 1 == last
711 || looking_at(buf, current, +2, "ER")))
712 /* 'dumb','thumb' */
713 || (buf[current + 1] == 'M'))
714 current += 2;
715 else
716 current++;
717 DMETAPH_ADD(code, "M", NULL);
718 break;
719
720 case 'N':
721 ++current;
722 if (buf[current] == 'N')
723 ++current;
724 DMETAPH_ADD(code, "N", NULL);
725 break;
726
727 case L'Ń':
728 case L'Ñ':
729 current++;
730 DMETAPH_ADD(code, "N", NULL);
731 break;
732
733 case 'P':
734 if (buf[current + 1] == 'H') {
735 DMETAPH_ADD(code, "F", NULL);
736 current += 2;
737 break;
738 }
739 /* also account for "campbell", "raspberry" */
740 if (looking_at(buf, current, +1, "P|B"))
741 current += 2;
742 else
743 current++;
744 DMETAPH_ADD(code, "P", NULL);
745 break;
746
747 case 'Q':
748 ++current;
749 if (buf[current] == 'Q')
750 ++current;
751 DMETAPH_ADD(code, "K", NULL);
752 break;
753
754 case 'R':
755 /* french e.g. 'rogier', but exclude 'hochmeier' */
756 if (current == last
757 && !IS_SLAVO_GERMANIC()
758 && looking_at(buf, current, -2, "IE")
759 && !looking_at(buf, current, -4, "ME|MA"))
760 DMETAPH_ADD(code, NULL, "R");
761 else
762 DMETAPH_ADD(code, "R", NULL);
763
764 ++current;
765 if (buf[current] == 'R')
766 ++current;
767 break;
768
769 case 'S':
770 /* special cases 'island', 'isle', 'carlisle', 'carlysle' */
771 if (looking_at(buf, current, -1, "ISL|YSL")) {
772 current++;
773 break;
774 }
775 /* special case 'sugar-' */
776 if (current == 0 && looking_at(buf, current, 0, "SUGAR")) {
777 DMETAPH_ADD(code, "X", "S");
778 current++;
779 break;
780 }
781
782 if (looking_at(buf, current, 0, "SH")) {
783 /* germanic */
784 if (looking_at(buf, current, +1, "HEIM|HOEK|HOLM|HOLZ"))
785 DMETAPH_ADD(code, "S", NULL);
786 else
787 DMETAPH_ADD(code, "X", NULL);
788 current += 2;
789 break;
790 }
791 /* italian & armenian */
792 if (looking_at(buf, current, 0, "SIO|SIA")
793 || looking_at(buf, current, 0, "SIAN")) {
794 if (!IS_SLAVO_GERMANIC())
795 DMETAPH_ADD(code, "S", "X");
796 else
797 DMETAPH_ADD(code, "S", NULL);
798 current += 3;
799 break;
800 }
801 /* german & anglicisations, e.g. 'smith' match 'schmidt',
802 'snider' match 'schneider' */
803 /* also, -sz- in slavic language altho in hungarian it is
804 pronounced 's'
805 */
806 if ((current == 0
807 && looking_at(buf, current, +1, "M|N|L|W"))
808 || looking_at(buf, current, +1, "Z")) {
809 DMETAPH_ADD(code, "S", "X");
810 if (looking_at(buf, current, +1, "Z"))
811 current += 2;
812 else
813 current++;
814 break;
815 }
816
817 if (looking_at(buf, current, 0, "SC")) {
818 /* Schlesinger's rule */
819 if (buf[current + 2] == 'H') {
820 /* Dutch origin, e.g. 'school', 'schooner' */
821 if (looking_at(buf, current, +3, "OO|ER|EN|UY|ED|EM")) {
822 /* 'schermerhorn', 'schenker' */
823 if (looking_at(buf, current, +3, "ER|EN"))
824 DMETAPH_ADD(code, "X", "SK");
825 else
826 DMETAPH_ADD(code, "SK", NULL);
827 current += 3;
828 break;
829 } else {
830 if (current == 0 && !ISVOWEL(buf, 3, 0) && buf[3] != 'W')
831 DMETAPH_ADD(code, "X", "S");
832 else
833 DMETAPH_ADD(code, "X", NULL);
834 current += 3;
835 break;
836 }
837 }
838
839 if (looking_at(buf, current, +2, "I|E|Y")) {
840 DMETAPH_ADD(code, "S", NULL);
841 current += 3;
842 break;
843 }
844 DMETAPH_ADD(code, "SK", NULL);
845 current += 3;
846 break;
847 }
848 /* french e.g. 'resnais', 'artois' */
849 if (current == last && looking_at(buf, current, -2, "AI|OI"))
850 DMETAPH_ADD(code, NULL, "S");
851 else
852 DMETAPH_ADD(code, "S", NULL);
853
854 if (looking_at(buf, current, +1, "S|Z"))
855 current += 2;
856 else
857 current++;
858 break;
859
860 case 'T':
861 if (looking_at(buf, current, 0, "TION")) {
862 DMETAPH_ADD(code, "X", NULL);
863 current += 3;
864 break;
865 }
866
867 if (looking_at(buf, current, 0, "TIA|TCH")) {
868 DMETAPH_ADD(code, "X", NULL);
869 current += 3;
870 break;
871 }
872
873 if (looking_at(buf, current, 0, "TH|TTH")) {
874 /* special case 'thomas', 'thames' or germanic */
875 if (looking_at(buf, current, +2, "OM|AM")
876 || looking_at(buf, 0, 0, "VAN |VON |SCH")) {
877 DMETAPH_ADD(code, "T", NULL);
878 } else {
879 DMETAPH_ADD(code, "0", "T");
880 }
881 current += 2;
882 break;
883 }
884
885 if (looking_at(buf, current, +1, "T|D"))
886 current += 2;
887 else
888 current++;
889 DMETAPH_ADD(code, "T", NULL);
890 break;
891
892 case 'V':
893 if (buf[current + 1] == 'V')
894 current += 2;
895 else
896 current++;
897 DMETAPH_ADD(code, "F", NULL);
898 break;
899
900 case 'W':
901 /* can also be in middle of word */
902 if (looking_at(buf, current, 0, "WR")) {
903 DMETAPH_ADD(code, "R", NULL);
904 current += 2;
905 break;
906 }
907
908 if (current == 0
909 && (ISVOWEL(buf, current, +1)
910 || looking_at(buf, current, 0, "WH"))) {
911 /* Wasserman should match Vasserman */
912 if (ISVOWEL(buf, current, +1))
913 DMETAPH_ADD(code, "A", "F");
914 else
915 /* need Uomo to match Womo */
916 DMETAPH_ADD(code, "A", NULL);
917 }
918 /* Arnow should match Arnoff */
919 if ((current == last && ISVOWEL(buf, current, -1))
920 || looking_at(buf, current, -1, "EWSKI|EWSKY|OWSKI|OWSKY")
921 || looking_at(buf, 0, 0, "SCH")) {
922 DMETAPH_ADD(code, NULL, "F");
923 current++;
924 break;
925 }
926 /* polish e.g. 'filipowicz' */
927 if (looking_at(buf, current, 0, "WICZ|WITZ")) {
928 DMETAPH_ADD(code, "TS", "FX");
929 current += 4;
930 break;
931 }
932 /* else skip it */
933 current++;
934 break;
935
936 case 'X':
937 /* french e.g. breaux */
938 if (!(current == last
939 && (looking_at(buf, current, -3, "IAU|EAU")
940 || looking_at(buf, current, -2, "AU|OU"))))
941 DMETAPH_ADD(code, "KS", NULL);
942
943 if (looking_at(buf, current, +1, "C|X"))
944 current += 2;
945 else
946 current++;
947 break;
948
949 case 'Z':
950 /* chinese pinyin e.g. 'zhao' */
951 if (buf[current + 1] == 'H') {
952 DMETAPH_ADD(code, "J", NULL);
953 current += 2;
954 break;
955 } else if (looking_at(buf, current, +1, "ZO|ZI|ZA")
956 || (IS_SLAVO_GERMANIC()
957 && (current > 0 && buf[current - 1] != 'T'))) {
958 DMETAPH_ADD(code, "S", "TS");
959 } else
960 DMETAPH_ADD(code, "S", NULL);
961
962 ++current;
963 if (buf[current] == 'Z')
964 ++current;
965 break;
966
967 default:
968 ++current;
969 }
970 }
971
972 free(buf);
973
974 if (max_length) {
975 /* Trim returned codes */
976 metaph_code_trim(code[0], max_length);
977 metaph_code_trim(code[1], max_length);
978 }
979
980 return 0;
981 err:
982 free(buf);
983 double_metaphone_free(code);
984 return -1;
985 }
986
987 static int
988 double_metaphone_eq(double_metaphone_code a, double_metaphone_code b)
989 {
990 return metaph_code_eq(a[0], b[0])
991 || metaph_code_eq(a[1], b[1])
992 || metaph_code_eq(a[0], b[1])
993 || metaph_code_eq(a[1], b[0]);
994 }
995
996 static size_t double_metaphone_length = 4;
997
998 static int
999 metaphone2_sel(int cmd, struct dico_key *key, const char *dict_word)
1000 {
1001 double_metaphone_code code;
1002 int res;
1003
1004 switch (cmd) {
1005 case DICO_SELECT_BEGIN:
1006 if (double_metaphone_encode(code, key->word, double_metaphone_length))
1007 return 1;
1008 key->call_data = malloc(sizeof code);
1009 if (!key->call_data)
1010 return 1;
1011 memcpy(key->call_data, code, sizeof(code));
1012 break;
1013
1014 case DICO_SELECT_RUN:
1015 if (double_metaphone_encode(code, dict_word, double_metaphone_length))
1016 return 1;
1017 res = double_metaphone_eq(*(double_metaphone_code*)key->call_data,
1018 code);
1019 double_metaphone_free(code);
1020 return res;
1021
1022 case DICO_SELECT_END:
1023 double_metaphone_free(*(double_metaphone_code*)key->call_data);
1024 free(key->call_data);
1025 break;
1026 }
1027 return 0;
1028 }
1029
1030 static struct dico_strategy metaphone2_strat = {
1031 "metaphone2",
1032 "Match Double Metaphone encodings",
1033 metaphone2_sel
1034 };
1035
1036 static int
1037 metaphone2_init(int argc, char **argv)
1038 {
1039 long size = 0;
1040
1041 struct dico_option metaphone2_option[] = {
1042 { DICO_OPTSTR(size), dico_opt_long, &size },
1043 { NULL }
1044 };
1045
1046 if (dico_parseopt(metaphone2_option, argc, argv, 0, NULL))
1047 return 1;
1048 if (size > 0)
1049 double_metaphone_length = size;
1050
1051 dico_strategy_add(&metaphone2_strat);
1052 return 0;
1053 }
1054
1055 static int
1056 metaphone2_run_test(int argc, char **argv)
1057 {
1058 char *modname, *cmd;
1059
1060 argc--;
1061 modname = *argv++;
1062 dico_set_program_name(modname);
1063
1064 if (argc == 0) {
1065 dico_log(L_ERR, 0, "bad argument list");
1066 return 1;
1067 }
1068
1069 argc--;
1070 cmd = *argv++;
1071 if (strcmp(cmd, "build") == 0) {
1072 struct metaph_code *code;
1073
1074 code = metaph_code_create();
1075 assert(code != NULL);
1076 while (argc--) {
1077 int res = metaph_code_add(code, *argv++);
1078 assert(res == 0);
1079 }
1080 metaph_code_dump(code);
1081 metaph_code_free(code);
1082 } else if (strcmp(cmd, "compare") == 0) {
1083 struct metaph_code *a, *b;
1084 int res;
1085
1086 a = metaph_code_create();
1087 assert(a != NULL);
1088 while (argc--) {
1089 char *arg = *argv++;
1090 if (strcmp(arg, ":") == 0)
1091 break;
1092 else {
1093 res = metaph_code_add(a, arg);
1094 assert(res == 0);
1095 }
1096 }
1097
1098 if (argc <= 0) {
1099 dico_log(L_ERR, 0, "bad argument list");
1100 return 1;
1101 }
1102
1103 b = metaph_code_create();
1104 assert(b != NULL);
1105 while (argc--) {
1106 res = metaph_code_add(b, *argv++);
1107 assert(res == 0);
1108 }
1109 res = metaph_code_eq(a, b);
1110 metaph_code_free(a);
1111 metaph_code_free(b);
1112 printf("%d\n", res);
1113 } else if (strcmp(cmd, "encode") == 0) {
1114 unsigned long len = 0;
1115 if (strncmp(*argv, "-length=", 8) == 0) {
1116 char *p;
1117 len = strtoul(*argv + 8, &p, 10);
1118 assert(*p == 0);
1119 --argc;
1120 ++argv;
1121 }
1122 while (argc--) {
1123 char *arg = *argv++;
1124 double_metaphone_code code;
1125
1126 if (double_metaphone_encode(code, arg, len))
1127 dico_log(L_ERR, errno, "can't encode");
1128 else {
1129 printf("%s: ", arg);
1130 if (code[0]) {
1131 printf("'");
1132 metaph_code_print(code[0]);
1133 printf("'");
1134 } else {
1135 printf("NULL");
1136 }
1137 printf(" -- ");
1138 if (code[1]) {
1139 printf("'");
1140 metaph_code_print(code[1]);
1141 printf("'");
1142 } else {
1143 printf("NULL");
1144 }
1145 putchar('\n');
1146 double_metaphone_free(code);
1147 }
1148 }
1149 } else {
1150 dico_log(L_ERR, 0, "unrecognized unit test: %s", cmd);
1151 dico_log(L_INFO, 0, "usage:");
1152 printf("usage: %s build SEQ [SEQ...]\n"
1153 " build a metaphone code block from the sequence of letters\n",
1154 modname);
1155 printf(" %s compare SEQ [SEQ...] : SEQ [SEQ...]\n"
1156 " build two blocks and compare them\n",
1157 modname);
1158 printf(" %s encode [-length=N] WORD [WORD...]\n"
1159 " encode the supplied words\n",
1160 modname);
1161 return 1;
1162 }
1163 return 0;
1164 }
1165
1166 struct dico_database_module DICO_EXPORT(metaphone2, module) = {
1167 .dico_version = DICO_MODULE_VERSION,
1168 .dico_capabilities = DICO_CAPA_NODB,
1169 .dico_init = metaphone2_init,
1170 .dico_run_test = metaphone2_run_test
1171 };