metaphone.c 12 KB

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