phonetic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* phonetic.c - routines to do phonetic matching */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <ctype.h>
  45. #include <sys/types.h>
  46. #include "syntax.h"
  47. #include "portable.h"
  48. #if !defined(METAPHONE) && !defined(SOUNDEX)
  49. #define METAPHONE
  50. #endif
  51. #define iswordbreak(s) \
  52. (isascii(*(s)) \
  53. ? (isspace(*(s)) || \
  54. ispunct(*(s)) || \
  55. isdigit(*(s)) || \
  56. *(s) == '\0') \
  57. : utf8iswordbreak(s))
  58. static int
  59. utf8iswordbreak( const char* s )
  60. {
  61. switch( LDAP_UTF8GETCC( s )) {
  62. case 0x00A0: /* non-breaking space */
  63. case 0x3000: /* ideographic space */
  64. case 0xFEFF: /* zero-width non-breaking space */
  65. return 1;
  66. default: break;
  67. }
  68. return 0;
  69. }
  70. char *
  71. first_word( char *s )
  72. {
  73. if ( s == NULL ) {
  74. return( NULL );
  75. }
  76. while ( iswordbreak( s ) ) {
  77. if ( *s == '\0' ) {
  78. return( NULL );
  79. } else {
  80. LDAP_UTF8INC( s );
  81. }
  82. }
  83. return( s );
  84. }
  85. char *
  86. next_word( char *s )
  87. {
  88. if ( s == NULL ) {
  89. return( NULL );
  90. }
  91. while ( ! iswordbreak( s ) ) {
  92. LDAP_UTF8INC( s );
  93. }
  94. while ( iswordbreak( s ) ) {
  95. if ( *s == '\0' ) {
  96. return( NULL );
  97. } else {
  98. LDAP_UTF8INC( s );
  99. }
  100. }
  101. return( s );
  102. }
  103. char *
  104. word_dup( char *w )
  105. {
  106. char *s, *ret;
  107. char save;
  108. for ( s = w; !iswordbreak( s ); LDAP_UTF8INC( s ))
  109. ; /* NULL */
  110. save = *s;
  111. *s = '\0';
  112. ret = slapi_ch_strdup( w );
  113. *s = save;
  114. return( ret );
  115. }
  116. #ifndef MAXPHONEMELEN
  117. #define MAXPHONEMELEN 6
  118. #endif
  119. #if defined(SOUNDEX)
  120. /* lifted from isode-8.0 */
  121. char *
  122. phonetic( char *s )
  123. {
  124. char code, adjacent, ch;
  125. char *p;
  126. char **c;
  127. int i, cmax;
  128. char phoneme[MAXPHONEMELEN + 1];
  129. p = s;
  130. if ( p == NULL || *p == '\0' ) {
  131. return( NULL );
  132. }
  133. adjacent = '0';
  134. phoneme[0] = TOUPPER(*p);
  135. phoneme[1] = '\0';
  136. for ( i = 0; i < 99 && (! iswordbreak(p)); LDAP_UTF8INC( p )) {
  137. ch = TOUPPER (*p);
  138. code = '0';
  139. switch (ch) {
  140. case 'B':
  141. case 'F':
  142. case 'P':
  143. case 'V':
  144. code = (adjacent != '1') ? '1' : '0';
  145. break;
  146. case 'S':
  147. case 'C':
  148. case 'G':
  149. case 'J':
  150. case 'K':
  151. case 'Q':
  152. case 'X':
  153. case 'Z':
  154. code = (adjacent != '2') ? '2' : '0';
  155. break;
  156. case 'D':
  157. case 'T':
  158. code = (adjacent != '3') ? '3' : '0';
  159. break;
  160. case 'L':
  161. code = (adjacent != '4') ? '4' : '0';
  162. break;
  163. case 'M':
  164. case 'N':
  165. code = (adjacent != '5') ? '5' : '0';
  166. break;
  167. case 'R':
  168. code = (adjacent != '6') ? '6' : '0';
  169. break;
  170. default:
  171. adjacent = '0';
  172. }
  173. if ( i == 0 ) {
  174. adjacent = code;
  175. i++;
  176. } else if ( code != '0' ) {
  177. if ( i == MAXPHONEMELEN )
  178. break;
  179. adjacent = phoneme[i] = code;
  180. i++;
  181. }
  182. }
  183. if ( i > 0 )
  184. phoneme[i] = '\0';
  185. return( slapi_ch_strdup( phoneme ) );
  186. }
  187. #else
  188. #if defined(METAPHONE)
  189. /*
  190. * Metaphone copied from C Gazette, June/July 1991, pp 56-57,
  191. * author Gary A. Parker, with changes by Bernard Tiffany of the
  192. * University of Michigan, and more changes by Tim Howes of the
  193. * University of Michigan.
  194. */
  195. /* Character coding array */
  196. static char vsvfn[26] = {
  197. 1, 16, 4, 16, 9, 2, 4, 16, 9, 2, 0, 2, 2,
  198. /* A B C D E F G H I J K L M */
  199. 2, 1, 4, 0, 2, 4, 4, 1, 0, 0, 0, 8, 0};
  200. /* N O P Q R S T U V W X Y Z */
  201. /* Macros to access character coding array */
  202. #define vowel(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 1) /* AEIOU */
  203. #define same(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 2) /* FJLMNR */
  204. #define varson(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 4) /* CGPST */
  205. #define frontv(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 8) /* EIY */
  206. #define noghf(x) ((x) != '\0' && vsvfn[(x) - 'A'] & 16) /* BDH */
  207. char *
  208. phonetic( char *Word )
  209. {
  210. char *n, *n_start, *n_end; /* pointers to string */
  211. char *metaph_end; /* pointers to metaph */
  212. char ntrans[42]; /* word with uppercase letters */
  213. int KSflag; /* state flag for X -> KS */
  214. char buf[MAXPHONEMELEN + 2];
  215. char *Metaph;
  216. /*
  217. * Copy Word to internal buffer, dropping non-alphabetic characters
  218. * and converting to upper case
  219. */
  220. n = ntrans + 4; n_end = ntrans + 35;
  221. while (!iswordbreak( Word ) && n < n_end) {
  222. if (isascii(*Word)) {
  223. if (isalpha(*Word)) {
  224. *n++ = TOUPPER(*Word);
  225. }
  226. ++Word;
  227. } else {
  228. auto const size_t len = LDAP_UTF8COPY(n, Word);
  229. n += len; Word += len;
  230. }
  231. }
  232. Metaph = buf;
  233. *Metaph = '\0';
  234. if (n == ntrans + 4) {
  235. return( slapi_ch_strdup( buf ) ); /* Return if null */
  236. }
  237. n_end = n; /* Set n_end to end of string */
  238. /* ntrans[0] will always be == 0 */
  239. ntrans[0] = '\0';
  240. ntrans[1] = '\0';
  241. ntrans[2] = '\0';
  242. ntrans[3] = '\0';
  243. *n++ = 0;
  244. *n++ = 0;
  245. *n++ = 0;
  246. *n = 0; /* Pad with nulls */
  247. n = ntrans + 4; /* Assign pointer to start */
  248. /* Check for PN, KN, GN, AE, WR, WH, and X at start */
  249. switch (*n) {
  250. case 'P':
  251. case 'K':
  252. case 'G':
  253. /* 'PN', 'KN', 'GN' becomes 'N' */
  254. if (*(n + 1) == 'N')
  255. *n++ = 0;
  256. break;
  257. case 'A':
  258. /* 'AE' becomes 'E' */
  259. if (*(n + 1) == 'E')
  260. *n++ = 0;
  261. break;
  262. case 'W':
  263. /* 'WR' becomes 'R', and 'WH' to 'H' */
  264. if (*(n + 1) == 'R')
  265. *n++ = 0;
  266. else if (*(n + 1) == 'H') {
  267. *(n + 1) = *n;
  268. *n++ = 0;
  269. }
  270. break;
  271. case 'X':
  272. /* 'X' becomes 'S' */
  273. *n = 'S';
  274. break;
  275. }
  276. /*
  277. * Now, loop step through string, stopping at end of string or when
  278. * the computed 'metaph' is MAXPHONEMELEN characters long
  279. */
  280. KSflag = 0; /* state flag for KS translation */
  281. for (metaph_end = Metaph + MAXPHONEMELEN, n_start = n;
  282. n <= n_end && Metaph < metaph_end; n++) {
  283. if (KSflag) {
  284. KSflag = 0;
  285. *Metaph++ = 'S';
  286. } else if (!isascii(*n)) {
  287. *Metaph++ = *n;
  288. } else {
  289. /* Drop duplicates except for CC */
  290. if (*(n - 1) == *n && *n != 'C')
  291. continue;
  292. /* Check for F J L M N R or first letter vowel */
  293. if (same(*n) || (n == n_start && vowel(*n))) {
  294. *Metaph++ = *n;
  295. } else {
  296. switch (*n) {
  297. case 'B':
  298. /*
  299. * B unless in -MB
  300. */
  301. if (n < (n_end - 1) && *(n - 1) != 'M') {
  302. *Metaph++ = *n;
  303. }
  304. break;
  305. case 'C':
  306. /*
  307. * X if in -CIA-, -CH- else S if in
  308. * -CI-, -CE-, -CY- else dropped if
  309. * in -SCI-, -SCE-, -SCY- else K
  310. */
  311. if (*(n - 1) != 'S' || !frontv(*(n + 1))) {
  312. if (*(n + 1) == 'I' && *(n + 2) == 'A') {
  313. *Metaph++ = 'X';
  314. } else if (frontv(*(n + 1))) {
  315. *Metaph++ = 'S';
  316. } else if (*(n + 1) == 'H') {
  317. *Metaph++ = ((n == n_start && !vowel(*(n + 2)))
  318. || *(n - 1) == 'S')
  319. ? (char) 'K' : (char) 'X';
  320. } else {
  321. *Metaph++ = 'K';
  322. }
  323. }
  324. break;
  325. case 'D':
  326. /*
  327. * J if in DGE or DGI or DGY else T
  328. */
  329. *Metaph++ = (*(n + 1) == 'G' && frontv(*(n + 2)))
  330. ? (char) 'J' : (char) 'T';
  331. break;
  332. case 'G':
  333. /*
  334. * F if in -GH and not B--GH, D--GH,
  335. * -H--GH, -H---GH else dropped if
  336. * -GNED, -GN, -DGE-, -DGI-, -DGY-
  337. * else J if in -GE-, -GI-, -GY- and
  338. * not GG else K
  339. */
  340. if ((*(n + 1) != 'J' || vowel(*(n + 2))) &&
  341. (*(n + 1) != 'N' || ((n + 1) < n_end &&
  342. (*(n + 2) != 'E' || *(n + 3) != 'D'))) &&
  343. (*(n - 1) != 'D' || !frontv(*(n + 1))))
  344. *Metaph++ = (frontv(*(n + 1)) &&
  345. *(n + 2) != 'G') ? (char) 'G' : (char) 'K';
  346. else if (*(n + 1) == 'H' && !noghf(*(n - 3)) &&
  347. *(n - 4) != 'H')
  348. *Metaph++ = 'F';
  349. break;
  350. case 'H':
  351. /*
  352. * H if before a vowel and not after
  353. * C, G, P, S, T else dropped
  354. */
  355. if (!varson(*(n - 1)) && (!vowel(*(n - 1)) ||
  356. vowel(*(n + 1))))
  357. *Metaph++ = 'H';
  358. break;
  359. case 'K':
  360. /*
  361. * dropped if after C else K
  362. */
  363. if (*(n - 1) != 'C')
  364. *Metaph++ = 'K';
  365. break;
  366. case 'P':
  367. /*
  368. * F if before H, else P
  369. */
  370. *Metaph++ = *(n + 1) == 'H' ?
  371. (char) 'F' : (char) 'P';
  372. break;
  373. case 'Q':
  374. /*
  375. * K
  376. */
  377. *Metaph++ = 'K';
  378. break;
  379. case 'S':
  380. /*
  381. * X in -SH-, -SIO- or -SIA- else S
  382. */
  383. *Metaph++ = (*(n + 1) == 'H' ||
  384. (*(n + 1) == 'I' && (*(n + 2) == 'O' ||
  385. *(n + 2) == 'A')))
  386. ? (char) 'X' : (char) 'S';
  387. break;
  388. case 'T':
  389. /*
  390. * X in -TIA- or -TIO- else 0 (zero)
  391. * before H else dropped if in -TCH-
  392. * else T
  393. */
  394. if (*(n + 1) == 'I' && (*(n + 2) == 'O' ||
  395. *(n + 2) == 'A'))
  396. *Metaph++ = 'X';
  397. else if (*(n + 1) == 'H')
  398. *Metaph++ = '0';
  399. else if (*(n + 1) != 'C' || *(n + 2) != 'H')
  400. *Metaph++ = 'T';
  401. break;
  402. case 'V':
  403. /*
  404. * F
  405. */
  406. *Metaph++ = 'F';
  407. break;
  408. case 'W':
  409. /*
  410. * W after a vowel, else dropped
  411. */
  412. case 'Y':
  413. /*
  414. * Y unless followed by a vowel
  415. */
  416. if (vowel(*(n + 1)))
  417. *Metaph++ = *n;
  418. break;
  419. case 'X':
  420. /*
  421. * KS
  422. */
  423. if (n == n_start)
  424. *Metaph++ = 'S';
  425. else {
  426. *Metaph++ = 'K'; /* Insert K, then S */
  427. KSflag = 1;
  428. }
  429. break;
  430. case 'Z':
  431. /*
  432. * S
  433. */
  434. *Metaph++ = 'S';
  435. break;
  436. }
  437. }
  438. }
  439. }
  440. *Metaph = 0; /* Null terminate */
  441. return( slapi_ch_strdup( buf ) );
  442. }
  443. #endif /* METAPHONE */
  444. #endif /* !SOUNDEX */