PageRenderTime 68ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/main/src/com/google/refine/clustering/binning/Metaphone3.java

http://google-refine.googlecode.com/
Java | 7460 lines | 4716 code | 652 blank | 2092 comment | 2026 complexity | 8774dc1d2a94526d600a7cc2d818d021 MD5 | raw file
Possible License(s): JSON, LGPL-2.1, MIT, Apache-2.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. Copyright 2010, Lawrence Philips
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above
  10. copyright notice, this list of conditions and the following disclaimer
  11. in the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Google Inc. nor the names of its
  14. contributors may be used to endorse or promote products derived from
  15. this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*
  29. * A request from the author: Please comment and sign any changes you make to
  30. * the Metaphone 3 reference implementation.
  31. * <br>
  32. * Please do NOT reformat this module to Google Refine's coding standard,
  33. * but instead keep the original format so that it can be more easily compare
  34. * to any modified fork of the original.
  35. */
  36. /**
  37. * Metaphone 3<br>
  38. * VERSION 2.1.3
  39. *
  40. * by Lawrence Philips<br>
  41. *
  42. * Metaphone 3 is designed to return an *approximate* phonetic key (and an alternate
  43. * approximate phonetic key when appropriate) that should be the same for English
  44. * words, and most names familiar in the United States, that are pronounced *similarly*.
  45. * The key value is *not* intended to be an *exact* phonetic, or even phonemic,
  46. * representation of the word. This is because a certain degree of 'fuzziness' has
  47. * proven to be useful in compensating for variations in pronunciation, as well as
  48. * misheard pronunciations. For example, although americans are not usually aware of it,
  49. * the letter 's' is normally pronounced 'z' at the end of words such as "sounds".<br><br>
  50. *
  51. * The 'approximate' aspect of the encoding is implemented according to the following rules:<br><br>
  52. *
  53. * (1) All vowels are encoded to the same value - 'A'. If the parameter encodeVowels
  54. * is set to false, only *initial* vowels will be encoded at all. If encodeVowels is set
  55. * to true, 'A' will be encoded at all places in the word that any vowels are normally
  56. * pronounced. 'W' as well as 'Y' are treated as vowels. Although there are differences in
  57. * the pronunciation of 'W' and 'Y' in different circumstances that lead to their being
  58. * classified as vowels under some circumstances and as consonants in others, for the purposes
  59. * of the 'fuzziness' component of the Soundex and Metaphone family of algorithms they will
  60. * be always be treated here as vowels.<br><br>
  61. *
  62. * (2) Voiced and un-voiced consonant pairs are mapped to the same encoded value. This
  63. * means that:<br>
  64. * 'D' and 'T' -> 'T'<br>
  65. * 'B' and 'P' -> 'P'<br>
  66. * 'G' and 'K' -> 'K'<br>
  67. * 'Z' and 'S' -> 'S'<br>
  68. * 'V' and 'F' -> 'F'<br><br>
  69. *
  70. * - In addition to the above voiced/unvoiced rules, 'CH' and 'SH' -> 'X', where 'X'
  71. * represents the "-SH-" and "-CH-" sounds in Metaphone 3 encoding.<br><br>
  72. *
  73. * - Also, the sound that is spelled as "TH" in English is encoded to '0' (zero symbol). (Although
  74. * Americans are not usually aware of it, "TH" is pronounced in a voiced (e.g. "that") as
  75. * well as an unvoiced (e.g. "theater") form, which are naturally mapped to the same encoding.)<br><br>
  76. *
  77. * The encodings in this version of Metaphone 3 are according to pronunciations common in the
  78. * United States. This means that they will be inaccurate for consonant pronunciations that
  79. * are different in the United Kingdom, for example "tube" -> "CHOOBE" -> XAP rather than american TAP.<br><br>
  80. *
  81. * Metaphone 3 was preceded by by Soundex, patented in 1919, and Metaphone and Double Metaphone,
  82. * developed by Lawrence Philips. All of these algorithms resulted in a significant number of
  83. * incorrect encodings. Metaphone3 was tested against a database of about 100 thousand English words,
  84. * names common in the United States, and non-English words found in publications in the United States,
  85. * with an emphasis on words that are commonly mispronounced, prepared by the Moby Words website,
  86. * but with the Moby Words 'phonetic' encodings algorithmically mapped to Double Metaphone encodings.
  87. * Metaphone3 increases the accuracy of encoding of english words, common names, and non-English
  88. * words found in american publications from the 89% for Double Metaphone, to over 98%.<br><br>
  89. *
  90. * DISCLAIMER:
  91. * Anthropomorphic Software LLC claims only that Metaphone 3 will return correct encodings,
  92. * within the 'fuzzy' definition of correct as above, for a very high percentage of correctly
  93. * spelled English and commonly recognized non-English words. Anthropomorphic Software LLC
  94. * warns the user that a number of words remain incorrectly encoded, that misspellings may not
  95. * be encoded 'properly', and that people often have differing ideas about the pronunciation
  96. * of a word. Therefore, Metaphone 3 is not guaranteed to return correct results every time, and
  97. * so a desired target word may very well be missed. Creators of commercial products should
  98. * keep in mind that systems like Metaphone 3 produce a 'best guess' result, and should
  99. * condition the expectations of end users accordingly.<br><br>
  100. *
  101. * METAPHONE3 IS PROVIDED "AS IS" WITHOUT
  102. * WARRANTY OF ANY KIND. LAWRENCE PHILIPS AND ANTHROPOMORPHIC SOFTWARE LLC
  103. * MAKE NO WARRANTIES, EXPRESS OR IMPLIED, THAT IT IS FREE OF ERROR,
  104. * OR ARE CONSISTENT WITH ANY PARTICULAR STANDARD OF MERCHANTABILITY,
  105. * OR THAT IT WILL MEET YOUR REQUIREMENTS FOR ANY PARTICULAR APPLICATION.
  106. * LAWRENCE PHILIPS AND ANTHROPOMORPHIC SOFTWARE LLC DISCLAIM ALL LIABILITY
  107. * FOR DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES RESULTING FROM USE
  108. * OF THIS SOFTWARE.
  109. *
  110. * @author Lawrence Philips
  111. *
  112. * Metaphone 3 is designed to return an <i>approximate</i> phonetic key (and an alternate
  113. * approximate phonetic key when appropriate) that should be the same for English
  114. * words, and most names familiar in the United States, that are pronounced "similarly".
  115. * The key value is <i>not</i> intended to be an exact phonetic, or even phonemic,
  116. * representation of the word. This is because a certain degree of 'fuzziness' has
  117. * proven to be useful in compensating for variations in pronunciation, as well as
  118. * misheard pronunciations. For example, although americans are not usually aware of it,
  119. * the letter 's' is normally pronounced 'z' at the end of words such as "sounds".<br><br>
  120. *
  121. * The 'approximate' aspect of the encoding is implemented according to the following rules:<br><br>
  122. *
  123. * (1) All vowels are encoded to the same value - 'A'. If the parameter encodeVowels
  124. * is set to false, only *initial* vowels will be encoded at all. If encodeVowels is set
  125. * to true, 'A' will be encoded at all places in the word that any vowels are normally
  126. * pronounced. 'W' as well as 'Y' are treated as vowels. Although there are differences in
  127. * the pronunciation of 'W' and 'Y' in different circumstances that lead to their being
  128. * classified as vowels under some circumstances and as consonants in others, for the purposes
  129. * of the 'fuzziness' component of the Soundex and Metaphone family of algorithms they will
  130. * be always be treated here as vowels.<br><br>
  131. *
  132. * (2) Voiced and un-voiced consonant pairs are mapped to the same encoded value. This
  133. * means that:<br>
  134. * 'D' and 'T' -> 'T'<br>
  135. * 'B' and 'P' -> 'P'<br>
  136. * 'G' and 'K' -> 'K'<br>
  137. * 'Z' and 'S' -> 'S'<br>
  138. * 'V' and 'F' -> 'F'<br><br>
  139. *
  140. * - In addition to the above voiced/unvoiced rules, 'CH' and 'SH' -> 'X', where 'X'
  141. * represents the "-SH-" and "-CH-" sounds in Metaphone 3 encoding.<br><br>
  142. *
  143. * - Also, the sound that is spelled as "TH" in English is encoded to '0' (zero symbol). (Although
  144. * americans are not usually aware of it, "TH" is pronounced in a voiced (e.g. "that") as
  145. * well as an unvoiced (e.g. "theater") form, which are naturally mapped to the same encoding.)<br><br>
  146. *
  147. * In the "Exact" encoding, voiced/unvoiced pairs are <i>not</i> mapped to the same encoding, except
  148. * for the voiced and unvoiced versions of 'TH', sounds such as 'CH' and 'SH', and for 'S' and 'Z',
  149. * so that the words whose metaph keys match will in fact be closer in pronunciation that with the
  150. * more approximate setting. Keep in mind that encoding settings for search strings should always
  151. * be exactly the same as the encoding settings of the stored metaph keys in your database!
  152. * Because of the considerably increased accuracy of Metaphone3, it is now possible to use this
  153. * setting and have a very good chance of getting a correct encoding.
  154. * <br><br>
  155. * In the Encode Vowels encoding, all non-initial vowels and diphthongs will be encoded to
  156. * 'A', and there will only be one such vowel encoding character between any two consonants.
  157. * It turns out that there are some surprising wrinkles to encoding non-initial vowels in
  158. * practice, pre-eminently in inversions between spelling and pronunciation such as e.g.
  159. * "wrinkle" => 'RANKAL', where the last two sounds are inverted when spelled.
  160. * <br><br>
  161. * The encodings in this version of Metaphone 3 are according to pronunciations common in the
  162. * United States. This means that they will be inaccurate for consonant pronunciations that
  163. * are different in the United Kingdom, for example "tube" -> "CHOOBE" -> XAP rather than american TAP.
  164. * <br><br>
  165. *
  166. */
  167. package com.google.refine.clustering.binning;
  168. public class Metaphone3 {
  169. /** Length of word sent in to be encoded, as
  170. * measured at beginning of encoding. */
  171. int m_length;
  172. /** Length of encoded key string. */
  173. int m_metaphLength;
  174. /** Flag whether or not to encode non-initial vowels. */
  175. boolean m_encodeVowels;
  176. /** Flag whether or not to encode consonants as exactly
  177. * as possible. */
  178. boolean m_encodeExact;
  179. /** Internal copy of word to be encoded, allocated separately
  180. * from string pointed to in incoming parameter. */
  181. String m_inWord;
  182. /** Running copy of primary key. */
  183. StringBuffer m_primary;
  184. /** Running copy of secondary key. */
  185. StringBuffer m_secondary;
  186. /** Index of character in m_inWord currently being
  187. * encoded. */
  188. int m_current;
  189. /** Index of last character in m_inWord. */
  190. int m_last;
  191. /** Flag that an AL inversion has already been done. */
  192. boolean flag_AL_inversion;
  193. /** Default size of key storage allocation */
  194. int MAX_KEY_ALLOCATION = 32;
  195. /** Default maximum length of encoded key. */
  196. int DEFAULT_MAX_KEY_LENGTH = 8;
  197. ////////////////////////////////////////////////////////////////////////////////
  198. // Metaphone3 class definition
  199. ////////////////////////////////////////////////////////////////////////////////
  200. /**
  201. * Constructor, default. This constructor is most convenient when
  202. * encoding more than one word at a time. New words to encode can
  203. * be set using SetWord(char *).
  204. *
  205. */
  206. Metaphone3()
  207. {
  208. m_primary = new StringBuffer();
  209. m_secondary = new StringBuffer();
  210. m_metaphLength = DEFAULT_MAX_KEY_LENGTH;
  211. m_encodeVowels = false;
  212. m_encodeExact = false;
  213. }
  214. /**
  215. * Constructor, parameterized. The Metaphone3 object will
  216. * be initialized with the incoming string, and can be called
  217. * on to encode this string. This constructor is most convenient
  218. * when only one word needs to be encoded.
  219. *
  220. * @param in pointer to char string of word to be encoded.
  221. *
  222. */
  223. Metaphone3(String in)
  224. {
  225. this();
  226. SetWord(in);
  227. }
  228. /**
  229. * Sets word to be encoded.
  230. *
  231. * @param in pointer to EXTERNALLY ALLOCATED char string of
  232. * the word to be encoded.
  233. *
  234. */
  235. void SetWord(String in)
  236. {
  237. m_inWord = in.toUpperCase();;
  238. m_length = m_inWord.length();
  239. }
  240. /**
  241. * Sets length allocated for output keys.
  242. * If incoming number is greater than maximum allowable
  243. * length returned by GetMaximumKeyLength(), set key length
  244. * to maximum key length and return false; otherwise, set key
  245. * length to parameter value and return true.
  246. *
  247. * @param inKeyLength new length of key.
  248. * @return true if able to set key length to requested value.
  249. *
  250. */
  251. boolean SetKeyLength(int inKeyLength)
  252. {
  253. if(inKeyLength < 1)
  254. {
  255. // can't have that -
  256. // no room for terminating null
  257. inKeyLength = 1;
  258. }
  259. if(inKeyLength > MAX_KEY_ALLOCATION)
  260. {
  261. m_metaphLength = MAX_KEY_ALLOCATION;
  262. return false;
  263. }
  264. m_metaphLength = inKeyLength;
  265. return true;
  266. }
  267. /**
  268. * Adds an encoding character to the encoded key value string - one parameter version.
  269. *
  270. * @param main primary encoding character to be added to encoded key string.
  271. */
  272. void MetaphAdd(String in)
  273. {
  274. if(!(in.equals("A")
  275. && (m_primary.length() > 0)
  276. && (m_primary.charAt(m_primary.length() - 1) == 'A')))
  277. {
  278. m_primary.append(in);
  279. }
  280. if(!(in.equals("A")
  281. && (m_secondary.length() > 0)
  282. && (m_secondary.charAt(m_secondary.length() - 1) == 'A')))
  283. {
  284. m_secondary.append(in);
  285. }
  286. }
  287. /**
  288. * Adds an encoding character to the encoded key value string - two parameter version
  289. *
  290. * @param main primary encoding character to be added to encoded key string
  291. * @param alt alternative encoding character to be added to encoded alternative key string
  292. *
  293. */
  294. void MetaphAdd(String main, String alt)
  295. {
  296. if(!(main.equals("A")
  297. && (m_primary.length() > 0)
  298. && (m_primary.charAt(m_primary.length() - 1) == 'A')))
  299. {
  300. m_primary.append(main);
  301. }
  302. if(!(alt.equals("A")
  303. && (m_secondary.length() > 0)
  304. && (m_secondary.charAt(m_secondary.length() - 1) == 'A')))
  305. {
  306. if(!alt.isEmpty())
  307. {
  308. m_secondary.append(alt);
  309. }
  310. }
  311. }
  312. /**
  313. * Adds an encoding character to the encoded key value string - Exact/Approx version
  314. *
  315. * @param mainExact primary encoding character to be added to encoded key string if
  316. * m_encodeExact is set
  317. *
  318. * @param altExact alternative encoding character to be added to encoded alternative
  319. * key string if m_encodeExact is set
  320. *
  321. * @param main primary encoding character to be added to encoded key string
  322. *
  323. * @param alt alternative encoding character to be added to encoded alternative key string
  324. *
  325. */
  326. void MetaphAddExactApprox(String mainExact, String altExact, String main, String alt)
  327. {
  328. if(m_encodeExact)
  329. {
  330. MetaphAdd(mainExact, altExact);
  331. }
  332. else
  333. {
  334. MetaphAdd(main, alt);
  335. }
  336. }
  337. /**
  338. * Adds an encoding character to the encoded key value string - Exact/Approx version
  339. *
  340. * @param mainExact primary encoding character to be added to encoded key string if
  341. * m_encodeExact is set
  342. *
  343. * @param main primary encoding character to be added to encoded key string
  344. *
  345. */
  346. void MetaphAddExactApprox(String mainExact, String main)
  347. {
  348. if(m_encodeExact)
  349. {
  350. MetaphAdd(mainExact);
  351. }
  352. else
  353. {
  354. MetaphAdd(main);
  355. }
  356. }
  357. /** Retrieves maximum number of characters currently allocated for encoded key.
  358. *
  359. * @return short integer representing the length allowed for the key.
  360. */
  361. int GetKeyLength(){return m_metaphLength;}
  362. /** Retrieves maximum number of characters allowed for encoded key.
  363. *
  364. * @return short integer representing the length of allocated storage for the key.
  365. */
  366. int GetMaximumKeyLength(){return (int)MAX_KEY_ALLOCATION;}
  367. /** Sets flag that causes Metaphone3 to encode non-initial vowels. However, even
  368. * if there are more than one vowel sound in a vowel sequence (i.e.
  369. * vowel diphthong, etc.), only one 'A' will be encoded before the next consonant or the
  370. * end of the word.
  371. *
  372. * @param inEncodeVowels Non-initial vowels encoded if true, not if false.
  373. */
  374. void SetEncodeVowels(boolean inEncodeVowels){m_encodeVowels = inEncodeVowels;}
  375. /** Retrieves setting determining whether or not non-initial vowels will be encoded.
  376. *
  377. * @return true if the Metaphone3 object has been set to encode non-initial vowels, false if not.
  378. */
  379. boolean GetEncodeVowels(){return m_encodeVowels;}
  380. /** Sets flag that causes Metaphone3 to encode consonants as exactly as possible.
  381. * This does not include 'S' vs. 'Z', since americans will pronounce 'S' at the
  382. * at the end of many words as 'Z', nor does it include "CH" vs. "SH". It does cause
  383. * a distinction to be made between 'B' and 'P', 'D' and 'T', 'G' and 'K', and 'V'
  384. * and 'F'.
  385. *
  386. * @param inEncodeExact consonants to be encoded "exactly" if true, not if false.
  387. */
  388. void SetEncodeExact(boolean inEncodeExact){m_encodeExact = inEncodeExact;}
  389. /** Retrieves setting determining whether or not consonants will be encoded "exactly".
  390. *
  391. * @return true if the Metaphone3 object has been set to encode "exactly", false if not.
  392. */
  393. boolean GetEncodeExact(){return m_encodeExact;}
  394. /** Retrieves primary encoded key.
  395. *
  396. * @return a character pointer to the primary encoded key
  397. */
  398. String GetMetaph()
  399. {
  400. String primary = new String(m_primary);
  401. return primary;
  402. }
  403. /** Retrieves alternate encoded key, if any.
  404. *
  405. * @return a character pointer to the alternate encoded key
  406. */
  407. String GetAlternateMetaph()
  408. {
  409. String secondary = new String(m_secondary);
  410. return secondary;
  411. }
  412. /**
  413. * Test for close front vowels
  414. *
  415. * @return true if close front vowel
  416. */
  417. boolean Front_Vowel(int at)
  418. {
  419. if(((CharAt(at) == 'E') || (CharAt(at) == 'I') || (CharAt(at) == 'Y')))
  420. {
  421. return true;
  422. }
  423. return false;
  424. }
  425. /**
  426. * Detect names or words that begin with spellings
  427. * typical of german or slavic words, for the purpose
  428. * of choosing alternate pronunciations correctly
  429. *
  430. */
  431. boolean SlavoGermanic()
  432. {
  433. if(StringAt(0, 3, "SCH", "")
  434. || StringAt(0, 2, "SW", "")
  435. || (CharAt(0) == 'J')
  436. || (CharAt(0) == 'W'))
  437. {
  438. return true;
  439. }
  440. return false;
  441. }
  442. /**
  443. * Tests if character is a vowel
  444. *
  445. * @param inChar character to be tested in string to be encoded
  446. * @return true if character is a vowel, false if not
  447. *
  448. */
  449. boolean IsVowel(char inChar)
  450. {
  451. if((inChar == 'A')
  452. || (inChar == 'E')
  453. || (inChar == 'I')
  454. || (inChar == 'O')
  455. || (inChar == 'U')
  456. || (inChar == 'Y')
  457. || (inChar == 'Ŕ')
  458. || (inChar == 'Á')
  459. || (inChar == 'Â')
  460. || (inChar == 'Ă')
  461. || (inChar == 'Ä')
  462. || (inChar == 'Ĺ')
  463. || (inChar == 'Ć')
  464. || (inChar == 'Č')
  465. || (inChar == 'É')
  466. || (inChar == 'Ę')
  467. || (inChar == 'Ë')
  468. || (inChar == 'Ě')
  469. || (inChar == 'Í')
  470. || (inChar == 'Î')
  471. || (inChar == 'Ď')
  472. || (inChar == 'Ň')
  473. || (inChar == 'Ó')
  474. || (inChar == 'Ô')
  475. || (inChar == 'Ő')
  476. || (inChar == 'Ö')
  477. || (inChar == '?')
  478. || (inChar == 'Ř')
  479. || (inChar == 'Ů')
  480. || (inChar == 'Ú')
  481. || (inChar == 'Ű')
  482. || (inChar == 'Ü')
  483. || (inChar == 'Ý')
  484. || (inChar == '?'))
  485. {
  486. return true;
  487. }
  488. return false;
  489. }
  490. /**
  491. * Tests if character in the input string is a vowel
  492. *
  493. * @param at position of character to be tested in string to be encoded
  494. * @return true if character is a vowel, false if not
  495. *
  496. */
  497. boolean IsVowel(int at)
  498. {
  499. if((at < 0) || (at >= m_length))
  500. {
  501. return false;
  502. }
  503. char it = CharAt(at);
  504. if(IsVowel(it))
  505. {
  506. return true;
  507. }
  508. return false;
  509. }
  510. /**
  511. * Skips over vowels in a string. Has exceptions for skipping consonants that
  512. * will not be encoded.
  513. *
  514. * @param at position, in string to be encoded, of character to start skipping from
  515. *
  516. * @return position of next consonant in string to be encoded
  517. */
  518. int SkipVowels(int at)
  519. {
  520. if(at < 0)
  521. {
  522. return 0;
  523. }
  524. if(at >= m_length)
  525. {
  526. return m_length;
  527. }
  528. char it = CharAt(at);
  529. while(IsVowel(it) || (it == 'W'))
  530. {
  531. if(StringAt(at, 4, "WICZ", "WITZ", "WIAK", "")
  532. || StringAt((at - 1), 5, "EWSKI", "EWSKY", "OWSKI", "OWSKY", "")
  533. || (StringAt(at, 5, "WICKI", "WACKI", "") && ((at + 4) == m_last)))
  534. {
  535. break;
  536. }
  537. at++;
  538. if(((CharAt(at - 1) == 'W') && (CharAt(at) == 'H'))
  539. && !(StringAt(at, 3, "HOP", "")
  540. || StringAt(at, 4, "HIDE", "HARD", "HEAD", "HAWK", "HERD", "HOOK", "HAND", "HOLE", "")
  541. || StringAt(at, 5, "HEART", "HOUSE", "HOUND", "")
  542. || StringAt(at, 6, "HAMMER", "")))
  543. {
  544. at++;
  545. }
  546. if(at > (m_length - 1))
  547. {
  548. break;
  549. }
  550. it = CharAt(at);
  551. }
  552. return at;
  553. }
  554. /**
  555. * Advanced counter m_current so that it indexes the next character to be encoded
  556. *
  557. * @param ifNotEncodeVowels number of characters to advance if not encoding internal vowels
  558. * @param ifEncodeVowels number of characters to advance if encoding internal vowels
  559. *
  560. */
  561. void AdvanceCounter(int ifNotEncodeVowels, int ifEncodeVowels)
  562. {
  563. if(!m_encodeVowels)
  564. {
  565. m_current += ifNotEncodeVowels;
  566. }
  567. else
  568. {
  569. m_current += ifEncodeVowels;
  570. }
  571. }
  572. /**
  573. * Subscript safe .charAt()
  574. *
  575. * @param at index of character to access
  576. * @return null if index out of bounds, .charAt() otherwise
  577. */
  578. char CharAt(int at)
  579. {
  580. // check substring bounds
  581. if((at < 0)
  582. || (at > (m_length - 1)))
  583. {
  584. return '\0';
  585. }
  586. return m_inWord.charAt(at);
  587. }
  588. /**
  589. * Tests whether the word is the root or a regular english inflection
  590. * of it, e.g. "ache", "achy", "aches", "ached", "aching", "achingly"
  591. * This is for cases where we want to match only the root and corresponding
  592. * inflected forms, and not completely different words which may have the
  593. * same substring in them.
  594. */
  595. boolean RootOrInflections(String inWord, String root)
  596. {
  597. int len = root.length();
  598. String test;
  599. test = root + "S";
  600. if((inWord.equals(root))
  601. || (inWord.equals(test)))
  602. {
  603. return true;
  604. }
  605. if(root.charAt(len - 1) != 'E')
  606. {
  607. test = root + "ES";
  608. }
  609. if(inWord.equals(test))
  610. {
  611. return true;
  612. }
  613. if(root.charAt(len - 1) != 'E')
  614. {
  615. test = root + "ED";
  616. }
  617. else
  618. {
  619. test = root + "D";
  620. }
  621. if(inWord.equals(test))
  622. {
  623. return true;
  624. }
  625. if(root.charAt(len - 1) == 'E')
  626. {
  627. root = root.substring(0, len - 1);
  628. }
  629. test = root + "ING";
  630. if(inWord.equals(test))
  631. {
  632. return true;
  633. }
  634. test = root + "INGLY";
  635. if(inWord.equals(test))
  636. {
  637. return true;
  638. }
  639. test = root + "Y";
  640. if(inWord.equals(test))
  641. {
  642. return true;
  643. }
  644. return false;
  645. }
  646. /**
  647. * Determines if one of the substrings sent in is the same as
  648. * what is at the specified position in the string being encoded.
  649. *
  650. * @param start
  651. * @param length
  652. * @param compareStrings
  653. * @return
  654. */
  655. boolean StringAt(int start, int length, String... compareStrings)
  656. {
  657. // check substring bounds
  658. if((start < 0)
  659. || (start > (m_length - 1))
  660. || ((start + length - 1) > (m_length - 1)))
  661. {
  662. return false;
  663. }
  664. String target = m_inWord.substring(start, (start + length));
  665. for(String strFragment : compareStrings)
  666. {
  667. if(target.equals(strFragment))
  668. {
  669. return true;
  670. }
  671. }
  672. return false;
  673. }
  674. /**
  675. * Encodes input string to one or two key values according to Metaphone 3 rules.
  676. *
  677. */
  678. void Encode()
  679. {
  680. flag_AL_inversion = false;
  681. m_current = 0;
  682. m_primary.setLength(0);
  683. m_secondary.setLength(0);
  684. if(m_length < 1)
  685. {
  686. return;
  687. }
  688. //zero based index
  689. m_last = m_length - 1;
  690. ///////////main loop//////////////////////////
  691. while(!(m_primary.length() > m_metaphLength) && !(m_secondary.length() > m_metaphLength))
  692. {
  693. if(m_current >= m_length)
  694. {
  695. break;
  696. }
  697. switch(CharAt(m_current))
  698. {
  699. case 'B':
  700. Encode_B();
  701. break;
  702. case 'ß':
  703. case 'Ç':
  704. MetaphAdd("S");
  705. m_current++;
  706. break;
  707. case 'C':
  708. Encode_C();
  709. break;
  710. case 'D':
  711. Encode_D();
  712. break;
  713. case 'F':
  714. Encode_F();
  715. break;
  716. case 'G':
  717. Encode_G();
  718. break;
  719. case 'H':
  720. Encode_H();
  721. break;
  722. case 'J':
  723. Encode_J();
  724. break;
  725. case 'K':
  726. Encode_K();
  727. break;
  728. case 'L':
  729. Encode_L();
  730. break;
  731. case 'M':
  732. Encode_M();
  733. break;
  734. case 'N':
  735. Encode_N();
  736. break;
  737. case 'Ń':
  738. MetaphAdd("N");
  739. m_current++;
  740. break;
  741. case 'P':
  742. Encode_P();
  743. break;
  744. case 'Q':
  745. Encode_Q();
  746. break;
  747. case 'R':
  748. Encode_R();
  749. break;
  750. case 'S':
  751. Encode_S();
  752. break;
  753. case 'T':
  754. Encode_T();
  755. break;
  756. case 'Đ': // eth
  757. case 'Ţ': // thorn
  758. MetaphAdd("0");
  759. m_current++;
  760. break;
  761. case 'V':
  762. Encode_V();
  763. break;
  764. case 'W':
  765. Encode_W();
  766. break;
  767. case 'X':
  768. Encode_X();
  769. break;
  770. case '?':
  771. MetaphAdd("X");
  772. m_current++;
  773. break;
  774. case '?':
  775. MetaphAdd("S");
  776. m_current++;
  777. break;
  778. case 'Z':
  779. Encode_Z();
  780. break;
  781. default:
  782. if(IsVowel(CharAt(m_current)))
  783. {
  784. Encode_Vowels();
  785. break;
  786. }
  787. m_current++;
  788. }
  789. }
  790. //only give back m_metaphLength number of chars in m_metaph
  791. if(m_primary.length() > m_metaphLength)
  792. {
  793. m_primary.setLength(m_metaphLength);
  794. }
  795. if(m_secondary.length() > m_metaphLength)
  796. {
  797. m_secondary.setLength(m_metaphLength);
  798. }
  799. // it is possible for the two metaphs to be the same
  800. // after truncation. lose the second one if so
  801. if((m_primary.toString()).equals(m_secondary.toString()))
  802. {
  803. m_secondary.setLength(0);
  804. }
  805. }
  806. /**
  807. * Encodes all initial vowels to A.
  808. *
  809. * Encodes non-initial vowels to A if m_encodeVowels is true
  810. *
  811. *
  812. */
  813. void Encode_Vowels()
  814. {
  815. if(m_current == 0)
  816. {
  817. // all init vowels map to 'A'
  818. // as of Double Metaphone
  819. MetaphAdd("A");
  820. }
  821. else if(m_encodeVowels)
  822. {
  823. if(CharAt(m_current) != 'E')
  824. {
  825. if(Skip_Silent_UE())
  826. {
  827. return;
  828. }
  829. if (O_Silent())
  830. {
  831. m_current++;
  832. return;
  833. }
  834. // encode all vowels and
  835. // diphthongs to the same value
  836. MetaphAdd("A");
  837. }
  838. else
  839. {
  840. Encode_E_Pronounced();
  841. }
  842. }
  843. if(!(!IsVowel(m_current - 2) && StringAt((m_current - 1), 4, "LEWA", "LEWO", "LEWI", "")))
  844. {
  845. m_current = SkipVowels(m_current);
  846. }
  847. else
  848. {
  849. m_current++;
  850. }
  851. }
  852. /**
  853. * Encodes cases where non-initial 'e' is pronounced, taking
  854. * care to detect unusual cases from the greek.
  855. *
  856. * Only executed if non initial vowel encoding is turned on
  857. *
  858. *
  859. */
  860. void Encode_E_Pronounced()
  861. {
  862. // special cases with two pronunciations
  863. // 'agape' 'lame' 'resume'
  864. if((StringAt(0, 4, "LAME", "SAKE", "PATE", "") && (m_length == 4))
  865. || (StringAt(0, 5, "AGAPE", "") && (m_length == 5))
  866. || ((m_current == 5) && StringAt(0, 6, "RESUME", "")))
  867. {
  868. MetaphAdd("", "A");
  869. return;
  870. }
  871. // special case "inge" => 'INGA', 'INJ'
  872. if(StringAt(0, 4, "INGE", "")
  873. && (m_length == 4))
  874. {
  875. MetaphAdd("A", "");
  876. return;
  877. }
  878. // special cases with two pronunciations
  879. // special handling due to the difference in
  880. // the pronunciation of the '-D'
  881. if((m_current == 5) && StringAt(0, 7, "BLESSED", "LEARNED", ""))
  882. {
  883. MetaphAddExactApprox("D", "AD", "T", "AT");
  884. m_current += 2;
  885. return;
  886. }
  887. // encode all vowels and diphthongs to the same value
  888. if((!E_Silent()
  889. && !flag_AL_inversion
  890. && !Silent_Internal_E())
  891. || E_Pronounced_Exceptions())
  892. {
  893. MetaphAdd("A");
  894. }
  895. // now that we've visited the vowel in question
  896. flag_AL_inversion = false;
  897. }
  898. /**
  899. * Tests for cases where non-initial 'o' is not pronounced
  900. * Only executed if non initial vowel encoding is turned on
  901. *
  902. * @return true if encoded as silent - no addition to m_metaph key
  903. *
  904. */
  905. boolean O_Silent()
  906. {
  907. // if "iron" at beginning or end of word and not "irony"
  908. if ((CharAt(m_current) == 'O')
  909. && StringAt((m_current - 2), 4, "IRON", ""))
  910. {
  911. if ((StringAt(0, 4, "IRON", "")
  912. || (StringAt((m_current - 2), 4, "IRON", "")
  913. && (m_last == (m_current + 1))))
  914. && !StringAt((m_current - 2), 6, "IRONIC", ""))
  915. {
  916. return true;
  917. }
  918. }
  919. return false;
  920. }
  921. /**
  922. * Tests and encodes cases where non-initial 'e' is never pronounced
  923. * Only executed if non initial vowel encoding is turned on
  924. *
  925. * @return true if encoded as silent - no addition to m_metaph key
  926. *
  927. */
  928. boolean E_Silent()
  929. {
  930. if(E_Pronounced_At_End())
  931. {
  932. return false;
  933. }
  934. // 'e' silent when last letter, altho
  935. if((m_current == m_last)
  936. // also silent if before plural 's'
  937. // or past tense or participle 'd', e.g.
  938. // 'grapes' and 'banished' => PNXT
  939. || ((StringAt(m_last, 1, "S", "D", "")
  940. && (m_current > 1)
  941. && ((m_current + 1) == m_last)
  942. // and not e.g. "nested", "rises", or "pieces" => RASAS
  943. && !(StringAt((m_current - 1), 3, "TED", "SES", "CES", "")
  944. || StringAt(0, 9, "ANTIPODES", "ANOPHELES", "")
  945. || StringAt(0, 8, "MOHAMMED", "MUHAMMED", "MOUHAMED", "")
  946. || StringAt(0, 7, "MOHAMED", "")
  947. || StringAt(0, 6, "NORRED", "MEDVED", "MERCED", "ALLRED", "KHALED", "RASHED", "MASJED", "")
  948. || StringAt(0, 5, "JARED", "AHMED", "HAMED", "JAVED", "")
  949. || StringAt(0, 4, "ABED", "IMED", ""))))
  950. // e.g. 'wholeness', 'boneless', 'barely'
  951. || (StringAt((m_current + 1), 4, "NESS", "LESS", "") && ((m_current + 4) == m_last))
  952. || (StringAt((m_current + 1), 2, "LY", "") && ((m_current + 2) == m_last)
  953. && !StringAt(0, 6, "CICELY", "")))
  954. {
  955. return true;
  956. }
  957. return false;
  958. }
  959. /**
  960. * Tests for words where an 'E' at the end of the word
  961. * is pronounced
  962. *
  963. * special cases, mostly from the greek, spanish, japanese,
  964. * italian, and french words normally having an acute accent.
  965. * also, pronouns and articles
  966. *
  967. * Many Thanks to ali, QuentinCompson, JeffCO, ToonScribe, Xan,
  968. * Trafalz, and VictorLaszlo, all of them atriots from the Eschaton,
  969. * for all their fine contributions!
  970. *
  971. * @return true if 'E' at end is pronounced
  972. *
  973. */
  974. boolean E_Pronounced_At_End()
  975. {
  976. if((m_current == m_last)
  977. && (StringAt((m_current - 6), 7, "STROPHE", "")
  978. // if a vowel is before the 'E', vowel eater will have eaten it.
  979. //otherwise, consonant + 'E' will need 'E' pronounced
  980. || (m_length == 2)
  981. || ((m_length == 3) && !IsVowel(0))
  982. // these german name endings can be relied on to have the 'e' pronounced
  983. || (StringAt((m_last - 2), 3, "BKE", "DKE", "FKE", "KKE", "LKE",
  984. "NKE", "MKE", "PKE", "TKE", "VKE", "ZKE", "")
  985. && !StringAt(0, 5, "FINKE", "FUNKE", "")
  986. && !StringAt(0, 6, "FRANKE", ""))
  987. || StringAt((m_last - 4), 5, "SCHKE", "")
  988. || (StringAt(0, 4, "ACME", "NIKE", "CAFE", "RENE", "LUPE", "JOSE", "ESME", "") && (m_length == 4))
  989. || (StringAt(0, 5, "LETHE", "CADRE", "TILDE", "SIGNE", "POSSE", "LATTE", "ANIME", "DOLCE", "CROCE",
  990. "ADOBE", "OUTRE", "JESSE", "JAIME", "JAFFE", "BENGE", "RUNGE",
  991. "CHILE", "DESME", "CONDE", "URIBE", "LIBRE", "ANDRE", "") && (m_length == 5))
  992. || (StringAt(0, 6, "HECATE", "PSYCHE", "DAPHNE", "PENSKE", "CLICHE", "RECIPE",
  993. "TAMALE", "SESAME", "SIMILE", "FINALE", "KARATE", "RENATE", "SHANTE",
  994. "OBERLE", "COYOTE", "KRESGE", "STONGE", "STANGE", "SWAYZE", "FUENTE",
  995. "SALOME", "URRIBE", "") && (m_length == 6))
  996. || (StringAt(0, 7, "ECHIDNE", "ARIADNE", "MEINEKE", "PORSCHE", "ANEMONE", "EPITOME",
  997. "SYNCOPE", "SOUFFLE", "ATTACHE", "MACHETE", "KARAOKE", "BUKKAKE",
  998. "VICENTE", "ELLERBE", "VERSACE", "") && (m_length == 7))
  999. || (StringAt(0, 8, "PENELOPE", "CALLIOPE", "CHIPOTLE", "ANTIGONE", "KAMIKAZE", "EURIDICE",
  1000. "YOSEMITE", "FERRANTE", "") && (m_length == 8))
  1001. || (StringAt(0, 9, "HYPERBOLE", "GUACAMOLE", "XANTHIPPE", "") && (m_length == 9))
  1002. || (StringAt(0, 10, "SYNECDOCHE", "") && (m_length == 10))))
  1003. {
  1004. return true;
  1005. }
  1006. return false;
  1007. }
  1008. /**
  1009. * Detect internal silent 'E's e.g. "roseman",
  1010. * "firestone"
  1011. *
  1012. */
  1013. boolean Silent_Internal_E()
  1014. {
  1015. // 'olesen' but not 'olen' RAKE BLAKE
  1016. if((StringAt(0, 3, "OLE", "")
  1017. && E_Silent_Suffix(3) && !E_Pronouncing_Suffix(3))
  1018. || (StringAt(0, 4, "BARE", "FIRE", "FORE", "GATE", "HAGE", "HAVE",
  1019. "HAZE", "HOLE", "CAPE", "HUSE", "LACE", "LINE",
  1020. "LIVE", "LOVE", "MORE", "MOSE", "MORE", "NICE",
  1021. "RAKE", "ROBE", "ROSE", "SISE", "SIZE", "WARE",
  1022. "WAKE", "WISE", "WINE", "")
  1023. && E_Silent_Suffix(4) && !E_Pronouncing_Suffix(4))
  1024. || (StringAt(0, 5, "BLAKE", "BRAKE", "BRINE", "CARLE", "CLEVE", "DUNNE",
  1025. "HEDGE", "HOUSE", "JEFFE", "LUNCE", "STOKE", "STONE",
  1026. "THORE", "WEDGE", "WHITE", "")
  1027. && E_Silent_Suffix(5) && !E_Pronouncing_Suffix(5))
  1028. || (StringAt(0, 6, "BRIDGE", "CHEESE", "")
  1029. && E_Silent_Suffix(6) && !E_Pronouncing_Suffix(6))
  1030. || StringAt((m_current - 5), 7, "CHARLES", ""))
  1031. {
  1032. return true;
  1033. }
  1034. return false;
  1035. }
  1036. /**
  1037. * Detect conditions required
  1038. * for the 'E' not to be pronounced
  1039. *
  1040. */
  1041. boolean E_Silent_Suffix(int at)
  1042. {
  1043. if((m_current == (at - 1))
  1044. && (m_length > (at + 1))
  1045. && (IsVowel((at + 1))
  1046. || (StringAt(at, 2, "ST", "SL", "")
  1047. && (m_length > (at + 2)))))
  1048. {
  1049. return true;
  1050. }
  1051. return false;
  1052. }
  1053. /**
  1054. * Detect endings that will
  1055. * cause the 'e' to be pronounced
  1056. *
  1057. */
  1058. boolean E_Pronouncing_Suffix(int at)
  1059. {
  1060. // e.g. 'bridgewood' - the other vowels will get eaten
  1061. // up so we need to put one in here
  1062. if((m_length == (at + 4)) && StringAt(at, 4, "WOOD", ""))
  1063. {
  1064. return true;
  1065. }
  1066. // same as above
  1067. if((m_length == (at + 5)) && StringAt(at, 5, "WATER", "WORTH", ""))
  1068. {
  1069. return true;
  1070. }
  1071. // e.g. 'bridgette'
  1072. if((m_length == (at + 3)) && StringAt(at, 3, "TTE", "LIA", "NOW", "ROS", "RAS", ""))
  1073. {
  1074. return true;
  1075. }
  1076. // e.g. 'olena'
  1077. if((m_length == (at + 2)) && StringAt(at, 2, "TA", "TT", "NA", "NO", "NE",
  1078. "RS", "RE", "LA", "AU", "RO", "RA", ""))
  1079. {
  1080. return true;
  1081. }
  1082. // e.g. 'bridget'
  1083. if((m_length == (at + 1)) && StringAt(at, 1, "T", "R", ""))
  1084. {
  1085. return true;
  1086. }
  1087. return false;
  1088. }
  1089. /**
  1090. * Exceptions where 'E' is pronounced where it
  1091. * usually wouldn't be, and also some cases
  1092. * where 'LE' transposition rules don't apply
  1093. * and the vowel needs to be encoded here
  1094. *
  1095. * @return true if 'E' pronounced
  1096. *
  1097. */
  1098. boolean E_Pronounced_Exceptions()
  1099. {
  1100. // greek names e.g. "herakles" or hispanic names e.g. "robles", where 'e' is pronounced, other exceptions
  1101. if((((m_current + 1) == m_last)
  1102. && (StringAt((m_current - 3), 5, "OCLES", "ACLES", "AKLES", "")
  1103. || StringAt(0, 4, "INES", "")
  1104. || StringAt(0, 5, "LOPES", "ESTES", "GOMES", "NUNES", "ALVES", "ICKES",
  1105. "INNES", "PERES", "WAGES", "NEVES", "BENES", "DONES", "")
  1106. || StringAt(0, 6, "CORTES", "CHAVES", "VALDES", "ROBLES", "TORRES", "FLORES", "BORGES",
  1107. "NIEVES", "MONTES", "SOARES", "VALLES", "GEDDES", "ANDRES", "VIAJES",
  1108. "CALLES", "FONTES", "HERMES", "ACEVES", "BATRES", "MATHES", "")
  1109. || StringAt(0, 7, "DELORES", "MORALES", "DOLORES", "ANGELES", "ROSALES", "MIRELES", "LINARES",
  1110. "PERALES", "PAREDES", "BRIONES", "SANCHES", "CAZARES", "REVELES", "ESTEVES",
  1111. "ALVARES", "MATTHES", "SOLARES", "CASARES", "CACERES", "STURGES", "RAMIRES",
  1112. "FUNCHES", "BENITES", "FUENTES", "PUENTES", "TABARES", "HENTGES", "VALORES", "")
  1113. || StringAt(0, 8, "GONZALES", "MERCEDES", "FAGUNDES", "JOHANNES", "GONSALES", "BERMUDES",
  1114. "CESPEDES", "BETANCES", "TERRONES", "DIOGENES", "CORRALES", "CABRALES",
  1115. "MARTINES", "GRAJALES", "")
  1116. || StringAt(0, 9, "CERVANTES", "FERNANDES", "GONCALVES", "BENEVIDES", "CIFUENTES", "SIFUENTES",
  1117. "SERVANTES", "HERNANDES", "BENAVIDES", "")
  1118. || StringAt(0, 10, "ARCHIMEDES", "CARRIZALES", "MAGALLANES", "")))
  1119. || StringAt(m_current - 2, 4, "FRED", "DGES", "DRED", "GNES", "")
  1120. || StringAt((m_current - 5), 7, "PROBLEM", "RESPLEN", "")
  1121. || StringAt((m_current - 4), 6, "REPLEN", "")
  1122. || StringAt((m_current - 3), 4, "SPLE", ""))
  1123. {
  1124. return true;
  1125. }
  1126. return false;
  1127. }
  1128. /**
  1129. * Encodes "-UE".
  1130. *
  1131. * @return true if encoding handled in this routine, false if not
  1132. */
  1133. boolean Skip_Silent_UE()
  1134. {
  1135. // always silent except for cases listed below
  1136. if((StringAt((m_current - 1), 3, "QUE", "GUE", "")
  1137. && !StringAt(0, 8, "BARBEQUE", "PALENQUE", "APPLIQUE", "")
  1138. // '-que' cases usually french but missing the acute accent
  1139. && !StringAt(0, 6, "RISQUE", "")
  1140. && !StringAt((m_current - 3), 5, "ARGUE", "SEGUE", "")
  1141. && !StringAt(0, 7, "PIROGUE", "ENRIQUE", "")
  1142. && !StringAt(0, 10, "COMMUNIQUE", ""))
  1143. && (m_current > 1)
  1144. && (((m_current + 1) == m_last)
  1145. || StringAt(0, 7, "JACQUES", "")))
  1146. {
  1147. m_current = SkipVowels(m_current);
  1148. return true;
  1149. }
  1150. return false;
  1151. }
  1152. /**
  1153. * Encodes 'B'
  1154. *
  1155. *
  1156. */
  1157. void Encode_B()
  1158. {
  1159. if(Encode_Silent_B())
  1160. {
  1161. return;
  1162. }
  1163. // "-mb", e.g", "dumb", already skipped over under
  1164. // 'M', altho it should really be handled here...
  1165. MetaphAddExactApprox("B", "P");
  1166. if((CharAt(m_current + 1) == 'B')
  1167. || ((CharAt(m_current + 1) == 'P')
  1168. && ((m_current + 1 < m_last) && (CharAt(m_current + 2) != 'H'))))
  1169. {
  1170. m_current += 2;
  1171. }
  1172. else
  1173. {
  1174. m_current++;
  1175. }
  1176. }
  1177. /**
  1178. * Encodes silent 'B' for cases not covered under "-mb-"
  1179. *
  1180. *
  1181. * @return true if encoding handled in this routine, false if not
  1182. *
  1183. */
  1184. boolean Encode_Silent_B()
  1185. {
  1186. //'debt', 'doubt', 'subtle'
  1187. if(StringAt((m_current - 2), 4, "DEBT", "")
  1188. || StringAt((m_current - 2), 5, "SUBTL", "")
  1189. || StringAt((m_current - 2), 6, "SUBTIL", "")
  1190. || StringAt((m_current - 3), 5, "DOUBT", ""))
  1191. {
  1192. MetaphAdd("T");
  1193. m_current += 2;
  1194. return true;
  1195. }
  1196. return false;
  1197. }
  1198. /**
  1199. * Encodes 'C'
  1200. *
  1201. */
  1202. void Encode_C()
  1203. {
  1204. if(Encode_Silent_C_At_Beginning()
  1205. || Encode_CA_To_S()
  1206. || Encode_CO_To_S()
  1207. || Encode_CH()
  1208. || Encode_CCIA()
  1209. || Encode_CC()
  1210. || Encode_CK_CG_CQ()
  1211. || Encode_C_Front_Vowel()
  1212. || Encode_Silent_C()
  1213. || Encode_CZ()
  1214. || Encode_CS())
  1215. {
  1216. return;
  1217. }
  1218. //else
  1219. if(!StringAt((m_current - 1), 1, "C", "K", "G", "Q", ""))
  1220. {
  1221. MetaphAdd("K");
  1222. }
  1223. //name sent in 'mac caffrey', 'mac gregor
  1224. if(StringAt((m_current + 1), 2, " C", " Q", " G", ""))
  1225. {
  1226. m_current += 2;
  1227. }
  1228. else
  1229. {
  1230. if(StringAt((m_current + 1), 1, "C", "K", "Q", "")
  1231. && !StringAt((m_current + 1), 2, "CE", "CI", ""))
  1232. {
  1233. m_current += 2;
  1234. // account for combinations such as Ro-ckc-liffe
  1235. if(StringAt((m_current), 1, "C", "K", "Q", "")
  1236. && !StringAt((m_current + 1), 2, "CE", "CI", ""))
  1237. {
  1238. m_current++;
  1239. }
  1240. }
  1241. else
  1242. {
  1243. m_current++;
  1244. }
  1245. }
  1246. }
  1247. /**
  1248. * Encodes cases where 'C' is silent at beginning of word
  1249. *
  1250. * @return true if encoding handled in this routine, false if not
  1251. *
  1252. */
  1253. boolean Encode_Silent_C_At_Beginning()
  1254. {
  1255. //skip these when at start of word
  1256. if((m_current == 0)
  1257. && StringAt(m_current, 2, "CT", "CN", ""))
  1258. {
  1259. m_current += 1;
  1260. return true;
  1261. }
  1262. return false;
  1263. }
  1264. /**
  1265. * Encodes exceptions where "-CA-" should encode to S
  1266. * instead of K including cases where the cedilla has not been used
  1267. *
  1268. * @return true if encoding handled in this routine, false if not
  1269. *
  1270. */
  1271. boolean Encode_CA_To_S()
  1272. {
  1273. // Special case: 'caesar'.
  1274. // Also, where cedilla not used, as in "linguica" => LNKS
  1275. if(((m_current == 0) && StringAt(m_current, 4, "CAES", "CAEC", "CAEM", ""))
  1276. || StringAt(0, 8, "FRANCAIS", "FRANCAIX", "LINGUICA", "")
  1277. || StringAt(0, 6, "FACADE", "")
  1278. || StringAt(0, 9, "GONCALVES", "PROVENCAL", ""))
  1279. {
  1280. MetaphAdd("S");
  1281. AdvanceCounter(2, 1);
  1282. return true;
  1283. }
  1284. return false;
  1285. }
  1286. /**
  1287. * Encodes exceptions where "-CO-" encodes to S instead of K
  1288. * including cases where the cedilla has not been used
  1289. *
  1290. * @return true if encoding handled in this routine, false if not
  1291. *
  1292. */
  1293. boolean Encode_CO_To_S()
  1294. {
  1295. // e.g. 'coelecanth' => SLKN0
  1296. if((StringAt(m_current, 4, "COEL", "")
  1297. && (IsVowel(m_current + 4) || ((m_current + 3) == m_last)))
  1298. || StringAt(m_current, 5, "COENA", "COENO", "")
  1299. || StringAt(0, 8, "FRANCOIS", "MELANCON", "")
  1300. || StringAt(0, 6, "GARCON", ""))
  1301. {
  1302. MetaphAdd("S");
  1303. AdvanceCounter(3, 1);
  1304. return true;
  1305. }
  1306. return false;
  1307. }
  1308. /**
  1309. * Encode "-CH-"
  1310. *
  1311. * @return true if encoding handled in this routine, false if not
  1312. *
  1313. */
  1314. boolean Encode_CH()
  1315. {
  1316. if(StringAt(m_current, 2, "CH", ""))
  1317. {
  1318. if(Encode_CHAE()
  1319. || Encode_CH_To_H()
  1320. || Encode_Silent_CH()
  1321. || Encode_ARCH()
  1322. // Encode_CH_To_X() should be
  1323. // called before the germanic
  1324. // and greek encoding functions
  1325. || Encode_CH_To_X()
  1326. || Encode_English_CH_To_K()
  1327. || Encode_Germanic_CH_To_K()
  1328. || Encode_Greek_CH_Initial()
  1329. || Encode_Greek_CH_Non_Initial())
  1330. {
  1331. return true;
  1332. }
  1333. if(m_current > 0)
  1334. {
  1335. if(StringAt(0, 2, "MC", "")
  1336. && (m_current == 1))
  1337. {
  1338. //e.g., "McHugh"
  1339. MetaphAdd("K");
  1340. }
  1341. else
  1342. {
  1343. MetaphAdd("X", "K");
  1344. }
  1345. }
  1346. else
  1347. {
  1348. MetaphAdd("X");
  1349. }
  1350. m_current += 2;
  1351. return true;
  1352. }
  1353. return false;
  1354. }
  1355. /**
  1356. * Encodes "-CHAE-"
  1357. *
  1358. * @return true if encoding handled in this routine, false if not
  1359. *
  1360. */
  1361. boolean Encode_CHAE()
  1362. {
  1363. // e.g. 'michael'
  1364. if(((m_current > 0) && StringAt((m_current + 2), 2, "AE", "")))
  1365. {
  1366. if(StringAt(0, 7, "RACHAEL", ""))
  1367. {
  1368. MetaphAdd("X");
  1369. }
  1370. else if(!StringAt((m_current - 1), 1, "C", "K", "G", "Q", ""))
  1371. {
  1372. MetaphAdd("K");
  1373. }
  1374. AdvanceCounter(4, 2);
  1375. return true;
  1376. }
  1377. return false;
  1378. }
  1379. /**
  1380. * Encdoes transliterations from the hebrew where the
  1381. * sound 'kh' is represented as "-CH-". The normal pronounciation
  1382. * of this in english is either 'h' or 'kh', and alternate
  1383. * spellings most often use "-H-"
  1384. *
  1385. * @return true if encoding handled in this routine, false if not
  1386. *
  1387. */
  1388. boolean Encode_CH_To_H()
  1389. {
  1390. // hebrew => 'H', e.g. 'channukah', 'chabad'
  1391. if(((m_current == 0)
  1392. && (StringAt((m_current + 2), 3, "AIM", "ETH", "ELM", "")
  1393. || StringAt((m_current + 2), 4, "ASID", "AZAN", "")
  1394. || StringAt((m_current + 2), 5, "UPPAH", "UTZPA", "ALLAH", "ALUTZ", "AMETZ", "")
  1395. || StringAt((m_current + 2), 6, "ESHVAN", "ADARIM", "ANUKAH", "")
  1396. || StringAt((m_current + 2), 7, "ALLLOTH", "ANNUKAH", "AROSETH", "")))
  1397. // and an irish name with the same encoding
  1398. || StringAt((m_current - 3), 7, "CLACHAN", ""))
  1399. {
  1400. MetaphAdd("H");
  1401. AdvanceCounter(3, 2);
  1402. return true;
  1403. }
  1404. return false;
  1405. }
  1406. /**
  1407. * Encodes cases where "-CH-" is not pronounced
  1408. *
  1409. * @return true if encoding handled in this routine, false if not
  1410. *
  1411. */
  1412. boolean Encode_Silent_CH()
  1413. {
  1414. // '-ch-' not pronounced
  1415. if(StringAt((m_current - 2), 7, "FUCHSIA", "")
  1416. || StringAt((m_current - 2), 5, "YACHT", "")
  1417. || StringAt(0, 8, "STRACHAN", "")
  1418. || StringAt(0, 8, "CRICHTON", "")
  1419. || (StringAt((m_current - 3), 6, "DRACHM", ""))
  1420. && !StringAt((m_current - 3), 7, "DRACHMA", ""))
  1421. {
  1422. m_current += 2;
  1423. return true;
  1424. }
  1425. return false;
  1426. }
  1427. /**
  1428. * Encodes "-CH-" to X
  1429. * English language patterns
  1430. *
  1431. * @return true if encoding handled in this routine, false if not
  1432. *
  1433. */
  1434. boolean Encode_CH_To_X()
  1435. {
  1436. // e.g. 'approach', 'beach'
  1437. if((StringAt((m_current - 2), 4, "OACH", "EACH", "EECH", "OUCH", "OOCH", "MUCH", "SUCH", "")
  1438. && !StringAt((m_current - 3), 5, "JOACH", ""))
  1439. // e.g. 'dacha', 'macho'
  1440. || (((m_current + 2) == m_last ) && StringAt((m_current - 1), 4, "ACHA", "ACHO", ""))
  1441. || (StringAt(m_current, 4, "CHOT", "CHOD", "CHAT", "") && ((m_current + 3) == m_last))
  1442. || ((StringAt((m_current - 1), 4, "OCHE", "") && ((m_current + 2) == m_last))
  1443. && !StringAt((m_current - 2), 5, "DOCHE", ""))
  1444. || StringAt((m_current - 4), 6, "ATTACH", "DETACH", "KOVACH", "")
  1445. || StringAt((m_current - 5), 7, "SPINACH", "")
  1446. || StringAt(0, 6, "MACHAU", "")
  1447. || StringAt((m_current - 4), 8, "PARACHUT", "")
  1448. || StringAt((m_current - 5), 8, "MASSACHU", "")
  1449. || (StringAt((m_current - 3), 5, "THACH", "") && !StringAt((m_current - 1), 4, "ACHE", ""))
  1450. || StringAt((m_current - 2), 6, "VACHON", "") )
  1451. {
  1452. MetaphAdd("X");
  1453. m_current += 2;
  1454. return true;
  1455. }
  1456. return false;
  1457. }
  1458. /**
  1459. * Encodes "-CH-" to K in contexts of
  1460. * initial "A" or "E" follwed by "CH"
  1461. *
  1462. * @return true if encoding handled in this routine, false if not
  1463. *
  1464. */
  1465. boolean Encode_English_CH_To_K()
  1466. {
  1467. //'ache', 'echo', alternate spelling of 'michael'
  1468. if(((m_current == 1) && RootOrInflections(m_inWord, "ACHE"))
  1469. || (((m_current > 3) && RootOrInflections(m_inWord.substring(m_current - 1), "ACHE"))
  1470. && (StringAt(0, 3, "EAR", "")
  1471. || StringAt(0, 4, "HEAD", "BACK", "")
  1472. || StringAt(0, 5, "HEART", "BELLY", "TOOTH", "")))
  1473. || StringAt((m_current - 1), 4, "ECHO", "")
  1474. || StringAt((m_current - 2), 7, "MICHEAL", "")
  1475. || StringAt((m_current - 4), 7, "JERICHO", "")
  1476. || StringAt((m_current - 5), 7, "LEPRECH", ""))
  1477. {
  1478. MetaphAdd("K", "X");
  1479. m_current += 2;
  1480. return true;
  1481. }
  1482. return false;
  1483. }
  1484. /**
  1485. * Encodes "-CH-" to K in mostly germanic context
  1486. * of internal "-ACH-", with exceptions
  1487. *
  1488. * @return true if encoding handled in this routine, false if not
  1489. *
  1490. */
  1491. boolean Encode_Germanic_CH_To_K()
  1492. {
  1493. // various germanic
  1494. // "<consonant><vowel>CH-"implies a german word where 'ch' => K
  1495. if(((m_current > 1)
  1496. && !IsVowel(m_current - 2)
  1497. && StringAt((m_current - 1), 3, "ACH", "")
  1498. && !StringAt((m_current - 2), 7, "MACHADO", "MACHUCA", "LACHANC", "LACHAPE", "KACHATU", "")
  1499. && !StringAt((m_current - 3), 7, "KHACHAT", "")
  1500. && ((CharAt(m_current + 2) != 'I')
  1501. && ((CharAt(m_current + 2) != 'E')
  1502. || StringAt((m_current - 2), 6, "BACHER", "MACHER", "MACHEN", "LACHER", "")) )
  1503. // e.g. 'brecht', 'fuchs'
  1504. || (StringAt((m_current + 2), 1, "T", "S", "")
  1505. && !(StringAt(0, 11, "WHICHSOEVER", "") || StringAt(0, 9, "LUNCHTIME", "") ))
  1506. // e.g. 'andromache'
  1507. || StringAt(0, 4, "SCHR", "")
  1508. || ((m_current > 2) && StringAt((m_current - 2), 5, "MACHE", ""))
  1509. || ((m_current == 2) && StringAt((m_current - 2), 4, "ZACH", ""))
  1510. || StringAt((m_current - 4), 6, "SCHACH", "")
  1511. || StringAt((m_current - 1), 5, "ACHEN", "")
  1512. || StringAt((m_current - 3), 5, "SPICH", "ZURCH", "BUECH", "")
  1513. || (StringAt((m_current - 3), 5, "KIRCH", "JOACH", "BLECH", "MALCH", "")
  1514. // "kirch" and "blech" both get 'X'
  1515. && !(StringAt((m_current - 3),

Large files files are truncated, but you can click here to view the full file