metaphone.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Thies C. Arntzen <thies@thieso.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. /*
  20. Based on CPANs "Text-Metaphone-1.96" by Michael G Schwern <schwern@pobox.com>
  21. */
  22. #include "php.h"
  23. #include "php_metaphone.h"
  24. static int metaphone(unsigned char *word, int word_len, long max_phonemes, char **phoned_word, int traditional);
  25. /* {{{ proto string metaphone(string text[, int phones])
  26. Break english phrases down into their phonemes */
  27. PHP_FUNCTION(metaphone)
  28. {
  29. char *str;
  30. char *result = 0;
  31. int str_len;
  32. long phones = 0;
  33. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &str_len,
  34. &phones) == FAILURE) {
  35. return;
  36. }
  37. if (metaphone((unsigned char *)str, str_len, phones, &result, 1) == 0) {
  38. RETVAL_STRING(result, 0);
  39. } else {
  40. if (result) {
  41. efree(result);
  42. }
  43. RETURN_FALSE;
  44. }
  45. }
  46. /* }}} */
  47. /*
  48. this is now the original code by Michael G Schwern:
  49. i've changed it just a slightly bit (use emalloc,
  50. get rid of includes etc)
  51. - thies - 13.09.1999
  52. */
  53. /*----------------------------- */
  54. /* this used to be "metaphone.h" */
  55. /*----------------------------- */
  56. /* Special encodings */
  57. #define SH 'X'
  58. #define TH '0'
  59. /*----------------------------- */
  60. /* end of "metaphone.h" */
  61. /*----------------------------- */
  62. /*----------------------------- */
  63. /* this used to be "metachar.h" */
  64. /*----------------------------- */
  65. /* Metachar.h ... little bits about characters for metaphone */
  66. /*-- Character encoding array & accessing macros --*/
  67. /* Stolen directly out of the book... */
  68. char _codes[26] =
  69. {
  70. 1, 16, 4, 16, 9, 2, 4, 16, 9, 2, 0, 2, 2, 2, 1, 4, 0, 2, 4, 4, 1, 0, 0, 0, 8, 0
  71. /* a b c d e f g h i j k l m n o p q r s t u v w x y z */
  72. };
  73. #define ENCODE(c) (isalpha(c) ? _codes[((toupper(c)) - 'A')] : 0)
  74. #define isvowel(c) (ENCODE(c) & 1) /* AEIOU */
  75. /* These letters are passed through unchanged */
  76. #define NOCHANGE(c) (ENCODE(c) & 2) /* FJMNR */
  77. /* These form dipthongs when preceding H */
  78. #define AFFECTH(c) (ENCODE(c) & 4) /* CGPST */
  79. /* These make C and G soft */
  80. #define MAKESOFT(c) (ENCODE(c) & 8) /* EIY */
  81. /* These prevent GH from becoming F */
  82. #define NOGHTOF(c) (ENCODE(c) & 16) /* BDH */
  83. /*----------------------------- */
  84. /* end of "metachar.h" */
  85. /*----------------------------- */
  86. /* I suppose I could have been using a character pointer instead of
  87. * accesssing the array directly... */
  88. /* Look at the next letter in the word */
  89. #define Next_Letter (toupper(word[w_idx+1]))
  90. /* Look at the current letter in the word */
  91. #define Curr_Letter (toupper(word[w_idx]))
  92. /* Go N letters back. */
  93. #define Look_Back_Letter(n) (w_idx >= n ? toupper(word[w_idx-n]) : '\0')
  94. /* Previous letter. I dunno, should this return null on failure? */
  95. #define Prev_Letter (Look_Back_Letter(1))
  96. /* Look two letters down. It makes sure you don't walk off the string. */
  97. #define After_Next_Letter (Next_Letter != '\0' ? toupper(word[w_idx+2]) \
  98. : '\0')
  99. #define Look_Ahead_Letter(n) (toupper(Lookahead(word+w_idx, n)))
  100. /* Allows us to safely look ahead an arbitrary # of letters */
  101. /* I probably could have just used strlen... */
  102. static char Lookahead(char *word, int how_far)
  103. {
  104. char letter_ahead = '\0'; /* null by default */
  105. int idx;
  106. for (idx = 0; word[idx] != '\0' && idx < how_far; idx++);
  107. /* Edge forward in the string... */
  108. letter_ahead = word[idx]; /* idx will be either == to how_far or
  109. * at the end of the string
  110. */
  111. return letter_ahead;
  112. }
  113. /* phonize one letter
  114. * We don't know the buffers size in advance. On way to solve this is to just
  115. * re-allocate the buffer size. We're using an extra of 2 characters (this
  116. * could be one though; or more too). */
  117. #define Phonize(c) { \
  118. if (p_idx >= max_buffer_len) { \
  119. *phoned_word = safe_erealloc(*phoned_word, 2, sizeof(char), max_buffer_len); \
  120. max_buffer_len += 2; \
  121. } \
  122. (*phoned_word)[p_idx++] = c; \
  123. }
  124. /* Slap a null character on the end of the phoned word */
  125. #define End_Phoned_Word { \
  126. if (p_idx == max_buffer_len) { \
  127. *phoned_word = safe_erealloc(*phoned_word, 1, sizeof(char), max_buffer_len); \
  128. } \
  129. (*phoned_word)[p_idx] = '\0'; \
  130. }
  131. /* How long is the phoned word? */
  132. #define Phone_Len (p_idx)
  133. /* Note is a letter is a 'break' in the word */
  134. #define Isbreak(c) (!isalpha(c))
  135. /* {{{ metaphone
  136. */
  137. static int metaphone(unsigned char *word, int word_len, long max_phonemes, char **phoned_word, int traditional)
  138. {
  139. int w_idx = 0; /* point in the phonization we're at. */
  140. int p_idx = 0; /* end of the phoned phrase */
  141. int max_buffer_len = 0; /* maximum length of the destination buffer */
  142. /*-- Parameter checks --*/
  143. /* Negative phoneme length is meaningless */
  144. if (max_phonemes < 0)
  145. return -1;
  146. /* Empty/null string is meaningless */
  147. /* Overly paranoid */
  148. /* assert(word != NULL && word[0] != '\0'); */
  149. if (word == NULL)
  150. return -1;
  151. /*-- Allocate memory for our phoned_phrase --*/
  152. if (max_phonemes == 0) { /* Assume largest possible */
  153. max_buffer_len = word_len;
  154. *phoned_word = safe_emalloc(sizeof(char), word_len, 1);
  155. } else {
  156. max_buffer_len = max_phonemes;
  157. *phoned_word = safe_emalloc(sizeof(char), max_phonemes, 1);
  158. }
  159. /*-- The first phoneme has to be processed specially. --*/
  160. /* Find our first letter */
  161. for (; !isalpha(Curr_Letter); w_idx++) {
  162. /* On the off chance we were given nothing but crap... */
  163. if (Curr_Letter == '\0') {
  164. End_Phoned_Word
  165. return SUCCESS; /* For testing */
  166. }
  167. }
  168. switch (Curr_Letter) {
  169. /* AE becomes E */
  170. case 'A':
  171. if (Next_Letter == 'E') {
  172. Phonize('E');
  173. w_idx += 2;
  174. }
  175. /* Remember, preserve vowels at the beginning */
  176. else {
  177. Phonize('A');
  178. w_idx++;
  179. }
  180. break;
  181. /* [GKP]N becomes N */
  182. case 'G':
  183. case 'K':
  184. case 'P':
  185. if (Next_Letter == 'N') {
  186. Phonize('N');
  187. w_idx += 2;
  188. }
  189. break;
  190. /* WH becomes W,
  191. WR becomes R
  192. W if followed by a vowel */
  193. case 'W':
  194. if (Next_Letter == 'R') {
  195. Phonize(Next_Letter);
  196. w_idx += 2;
  197. } else if (Next_Letter == 'H' || isvowel(Next_Letter)) {
  198. Phonize('W');
  199. w_idx += 2;
  200. }
  201. /* else ignore */
  202. break;
  203. /* X becomes S */
  204. case 'X':
  205. Phonize('S');
  206. w_idx++;
  207. break;
  208. /* Vowels are kept */
  209. /* We did A already
  210. case 'A':
  211. case 'a':
  212. */
  213. case 'E':
  214. case 'I':
  215. case 'O':
  216. case 'U':
  217. Phonize(Curr_Letter);
  218. w_idx++;
  219. break;
  220. default:
  221. /* do nothing */
  222. break;
  223. }
  224. /* On to the metaphoning */
  225. for (; Curr_Letter != '\0' &&
  226. (max_phonemes == 0 || Phone_Len < max_phonemes);
  227. w_idx++) {
  228. /* How many letters to skip because an eariler encoding handled
  229. * multiple letters */
  230. unsigned short int skip_letter = 0;
  231. /* THOUGHT: It would be nice if, rather than having things like...
  232. * well, SCI. For SCI you encode the S, then have to remember
  233. * to skip the C. So the phonome SCI invades both S and C. It would
  234. * be better, IMHO, to skip the C from the S part of the encoding.
  235. * Hell, I'm trying it.
  236. */
  237. /* Ignore non-alphas */
  238. if (!isalpha(Curr_Letter))
  239. continue;
  240. /* Drop duplicates, except CC */
  241. if (Curr_Letter == Prev_Letter &&
  242. Curr_Letter != 'C')
  243. continue;
  244. switch (Curr_Letter) {
  245. /* B -> B unless in MB */
  246. case 'B':
  247. if (Prev_Letter != 'M')
  248. Phonize('B');
  249. break;
  250. /* 'sh' if -CIA- or -CH, but not SCH, except SCHW.
  251. * (SCHW is handled in S)
  252. * S if -CI-, -CE- or -CY-
  253. * dropped if -SCI-, SCE-, -SCY- (handed in S)
  254. * else K
  255. */
  256. case 'C':
  257. if (MAKESOFT(Next_Letter)) { /* C[IEY] */
  258. if (After_Next_Letter == 'A' &&
  259. Next_Letter == 'I') { /* CIA */
  260. Phonize(SH);
  261. }
  262. /* SC[IEY] */
  263. else if (Prev_Letter == 'S') {
  264. /* Dropped */
  265. } else {
  266. Phonize('S');
  267. }
  268. } else if (Next_Letter == 'H') {
  269. if ((!traditional) && (After_Next_Letter == 'R' || Prev_Letter == 'S')) { /* Christ, School */
  270. Phonize('K');
  271. } else {
  272. Phonize(SH);
  273. }
  274. skip_letter++;
  275. } else {
  276. Phonize('K');
  277. }
  278. break;
  279. /* J if in -DGE-, -DGI- or -DGY-
  280. * else T
  281. */
  282. case 'D':
  283. if (Next_Letter == 'G' &&
  284. MAKESOFT(After_Next_Letter)) {
  285. Phonize('J');
  286. skip_letter++;
  287. } else
  288. Phonize('T');
  289. break;
  290. /* F if in -GH and not B--GH, D--GH, -H--GH, -H---GH
  291. * else dropped if -GNED, -GN,
  292. * else dropped if -DGE-, -DGI- or -DGY- (handled in D)
  293. * else J if in -GE-, -GI, -GY and not GG
  294. * else K
  295. */
  296. case 'G':
  297. if (Next_Letter == 'H') {
  298. if (!(NOGHTOF(Look_Back_Letter(3)) ||
  299. Look_Back_Letter(4) == 'H')) {
  300. Phonize('F');
  301. skip_letter++;
  302. } else {
  303. /* silent */
  304. }
  305. } else if (Next_Letter == 'N') {
  306. if (Isbreak(After_Next_Letter) ||
  307. (After_Next_Letter == 'E' &&
  308. Look_Ahead_Letter(3) == 'D')) {
  309. /* dropped */
  310. } else
  311. Phonize('K');
  312. } else if (MAKESOFT(Next_Letter) &&
  313. Prev_Letter != 'G') {
  314. Phonize('J');
  315. } else {
  316. Phonize('K');
  317. }
  318. break;
  319. /* H if before a vowel and not after C,G,P,S,T */
  320. case 'H':
  321. if (isvowel(Next_Letter) &&
  322. !AFFECTH(Prev_Letter))
  323. Phonize('H');
  324. break;
  325. /* dropped if after C
  326. * else K
  327. */
  328. case 'K':
  329. if (Prev_Letter != 'C')
  330. Phonize('K');
  331. break;
  332. /* F if before H
  333. * else P
  334. */
  335. case 'P':
  336. if (Next_Letter == 'H') {
  337. Phonize('F');
  338. } else {
  339. Phonize('P');
  340. }
  341. break;
  342. /* K
  343. */
  344. case 'Q':
  345. Phonize('K');
  346. break;
  347. /* 'sh' in -SH-, -SIO- or -SIA- or -SCHW-
  348. * else S
  349. */
  350. case 'S':
  351. if (Next_Letter == 'I' &&
  352. (After_Next_Letter == 'O' ||
  353. After_Next_Letter == 'A')) {
  354. Phonize(SH);
  355. } else if (Next_Letter == 'H') {
  356. Phonize(SH);
  357. skip_letter++;
  358. } else if ((!traditional) && (Next_Letter == 'C' && Look_Ahead_Letter(2) == 'H' && Look_Ahead_Letter(3) == 'W')) {
  359. Phonize(SH);
  360. skip_letter += 2;
  361. } else {
  362. Phonize('S');
  363. }
  364. break;
  365. /* 'sh' in -TIA- or -TIO-
  366. * else 'th' before H
  367. * else T
  368. */
  369. case 'T':
  370. if (Next_Letter == 'I' &&
  371. (After_Next_Letter == 'O' ||
  372. After_Next_Letter == 'A')) {
  373. Phonize(SH);
  374. } else if (Next_Letter == 'H') {
  375. Phonize(TH);
  376. skip_letter++;
  377. } else if (!(Next_Letter == 'C' && After_Next_Letter == 'H')) {
  378. Phonize('T');
  379. }
  380. break;
  381. /* F */
  382. case 'V':
  383. Phonize('F');
  384. break;
  385. /* W before a vowel, else dropped */
  386. case 'W':
  387. if (isvowel(Next_Letter))
  388. Phonize('W');
  389. break;
  390. /* KS */
  391. case 'X':
  392. Phonize('K');
  393. Phonize('S');
  394. break;
  395. /* Y if followed by a vowel */
  396. case 'Y':
  397. if (isvowel(Next_Letter))
  398. Phonize('Y');
  399. break;
  400. /* S */
  401. case 'Z':
  402. Phonize('S');
  403. break;
  404. /* No transformation */
  405. case 'F':
  406. case 'J':
  407. case 'L':
  408. case 'M':
  409. case 'N':
  410. case 'R':
  411. Phonize(Curr_Letter);
  412. break;
  413. default:
  414. /* nothing */
  415. break;
  416. } /* END SWITCH */
  417. w_idx += skip_letter;
  418. } /* END FOR */
  419. End_Phoned_Word;
  420. return 0;
  421. } /* END metaphone */
  422. /* }}} */
  423. /*
  424. * Local variables:
  425. * tab-width: 4
  426. * c-basic-offset: 4
  427. * End:
  428. * vim600: sw=4 ts=4 fdm=marker
  429. * vim<600: sw=4 ts=4
  430. */