metaphone.c 12 KB

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