1 /* phonetic.c - routines to do phonetic matching */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
19 #if !defined(SLAPD_METAPHONE) && !defined(SLAPD_PHONETIC)
20 #define SLAPD_METAPHONE
23 #define iswordbreak(x) (!isascii(x) || isspace((unsigned char) (x)) || \
24 ispunct((unsigned char) (x)) || \
25 isdigit((unsigned char) (x)) || (x) == '\0')
34 while ( iswordbreak( *s ) ) {
52 while ( ! iswordbreak( *s ) ) {
56 while ( iswordbreak( *s ) ) {
73 for ( s = w; !iswordbreak( *s ); s++ )
84 #define MAXPHONEMELEN 4
87 #if defined(SLAPD_PHONETIC)
89 /* lifted from isode-8.0 */
93 char code, adjacent, ch;
96 char phoneme[MAXPHONEMELEN + 1];
99 if ( p == NULL || *p == '\0' ) {
104 phoneme[0] = TOUPPER((unsigned char)*p);
107 for ( i = 0; i < 99 && (! iswordbreak(*p)); p++ ) {
108 ch = TOUPPER ((unsigned char)*p);
117 code = (adjacent != '1') ? '1' : '0';
127 code = (adjacent != '2') ? '2' : '0';
131 code = (adjacent != '3') ? '3' : '0';
134 code = (adjacent != '4') ? '4' : '0';
138 code = (adjacent != '5') ? '5' : '0';
141 code = (adjacent != '6') ? '6' : '0';
150 } else if ( code != '0' ) {
151 if ( i == MAXPHONEMELEN )
153 adjacent = phoneme[i] = code;
161 return( ch_strdup( phoneme ) );
165 #if defined(SLAPD_METAPHONE)
168 * Metaphone copied from C Gazette, June/July 1991, pp 56-57,
169 * author Gary A. Parker, with changes by Bernard Tiffany of the
170 * University of Michigan, and more changes by Tim Howes of the
171 * University of Michigan.
174 /* Character coding array */
175 static const char vsvfn[26] = {
176 1, 16, 4, 16, 9, 2, 4, 16, 9, 2, 0, 2, 2,
177 /* A B C D E F G H I J K L M */
178 2, 1, 4, 0, 2, 4, 4, 1, 0, 0, 0, 8, 0};
179 /* N O P Q R S T U V W X Y Z */
181 /* Macros to access character coding array */
182 #define vowel(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 1) /* AEIOU */
183 #define same(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 2) /* FJLMNR */
184 #define varson(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 4) /* CGPST */
185 #define frontv(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 8) /* EIY */
186 #define noghf(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 16) /* BDH */
189 phonetic( char *Word )
191 char *n, *n_start, *n_end; /* pointers to string */
192 char *metaph_end; /* pointers to metaph */
193 char ntrans[40]; /* word with uppercase letters */
194 int KSflag; /* state flag for X -> KS */
195 char buf[MAXPHONEMELEN + 2];
199 * Copy Word to internal buffer, dropping non-alphabetic characters
200 * and converting to upper case
203 for (n = ntrans + 4, n_end = ntrans + 35; !iswordbreak( *Word ) &&
205 if (isalpha((unsigned char)*Word))
206 *n++ = TOUPPER((unsigned char)*Word);
210 if (n == ntrans + 4) {
211 return( ch_strdup( buf ) ); /* Return if null */
213 n_end = n; /* Set n_end to end of string */
215 /* ntrans[0] will always be == 0 */
223 *n = 0; /* Pad with nulls */
224 n = ntrans + 4; /* Assign pointer to start */
226 /* Check for PN, KN, GN, AE, WR, WH, and X at start */
231 /* 'PN', 'KN', 'GN' becomes 'N' */
236 /* 'AE' becomes 'E' */
241 /* 'WR' becomes 'R', and 'WH' to 'H' */
244 else if (*(n + 1) == 'H') {
250 /* 'X' becomes 'S' */
256 * Now, loop step through string, stopping at end of string or when
257 * the computed 'metaph' is MAXPHONEMELEN characters long
260 KSflag = 0; /* state flag for KS translation */
261 for (metaph_end = Metaph + MAXPHONEMELEN, n_start = n;
262 n <= n_end && Metaph < metaph_end; n++) {
267 /* Drop duplicates except for CC */
268 if (*(n - 1) == *n && *n != 'C')
270 /* Check for F J L M N R or first letter vowel */
271 if (same(*n) || (n == n_start && vowel(*n)))
280 if (n == (n_end - 1) && *(n - 1) != 'M')
286 * X if in -CIA-, -CH- else S if in
287 * -CI-, -CE-, -CY- else dropped if
288 * in -SCI-, -SCE-, -SCY- else K
290 if (*(n - 1) != 'S' || !frontv(*(n + 1))) {
291 if (*(n + 1) == 'I' && *(n + 2) == 'A')
293 else if (frontv(*(n + 1)))
295 else if (*(n + 1) == 'H')
296 *Metaph++ = ((n == n_start && !vowel(*(n + 2)))
298 ? (char) 'K' : (char) 'X';
306 * J if in DGE or DGI or DGY else T
308 *Metaph++ = (*(n + 1) == 'G' && frontv(*(n + 2)))
309 ? (char) 'J' : (char) 'T';
314 * F if in -GH and not B--GH, D--GH,
315 * -H--GH, -H---GH else dropped if
316 * -GNED, -GN, -DGE-, -DGI-, -DGY-
317 * else J if in -GE-, -GI-, -GY- and
320 if ((*(n + 1) != 'J' || vowel(*(n + 2))) &&
321 (*(n + 1) != 'N' || ((n + 1) < n_end &&
322 (*(n + 2) != 'E' || *(n + 3) != 'D'))) &&
323 (*(n - 1) != 'D' || !frontv(*(n + 1))))
324 *Metaph++ = (frontv(*(n + 1)) &&
325 *(n + 2) != 'G') ? (char) 'G' : (char) 'K';
326 else if (*(n + 1) == 'H' && !noghf(*(n - 3)) &&
333 * H if before a vowel and not after
334 * C, G, P, S, T else dropped
336 if (!varson(*(n - 1)) && (!vowel(*(n - 1)) ||
343 * dropped if after C else K
351 * F if before H, else P
353 *Metaph++ = *(n + 1) == 'H' ?
354 (char) 'F' : (char) 'P';
366 * X in -SH-, -SIO- or -SIA- else S
368 *Metaph++ = (*(n + 1) == 'H' ||
369 (*(n + 1) == 'I' && (*(n + 2) == 'O' ||
371 ? (char) 'X' : (char) 'S';
376 * X in -TIA- or -TIO- else 0 (zero)
377 * before H else dropped if in -TCH-
380 if (*(n + 1) == 'I' && (*(n + 2) == 'O' ||
383 else if (*(n + 1) == 'H')
385 else if (*(n + 1) != 'C' || *(n + 2) != 'H')
398 * W after a vowel, else dropped
403 * Y unless followed by a vowel
416 *Metaph++ = 'K'; /* Insert K, then S */
431 *Metaph = 0; /* Null terminate */
432 return( ch_strdup( buf ) );
435 #endif /* metaphone */
436 #endif /* SLAPD_PHONETIC */