/// <summary> /// Handles 'CC' cases. /// </summary> private int HandleCC(string value, DoubleMetaphoneResult result, int index) { if (Contains(value, index + 2, 1, "I", "E", "H") && !Contains(value, index + 2, 2, "HU")) { //-- "bellocchio" but not "bacchus" --// if ((index == 1 && CharAt(value, index - 1) == 'A') || Contains(value, index - 1, 5, "UCCEE", "UCCES")) { //-- "accident", "accede", "succeed" --// result.Append("KS"); } else { //-- "bacci", "bertucci", other Italian --// result.Append('X'); } index += 3; } else { // Pierce's rule result.Append('K'); index += 2; } return(index); }
/// <summary> /// Handles 'D' cases. /// </summary> private int HandleD(string value, DoubleMetaphoneResult result, int index) { if (Contains(value, index, 2, "DG")) { //-- "Edge" --// if (Contains(value, index + 2, 1, "I", "E", "Y")) { result.Append('J'); index += 3; //-- "Edgar" --// } else { result.Append("TK"); index += 2; } } else if (Contains(value, index, 2, "DT", "DD")) { result.Append('T'); index += 2; } else { result.Append('T'); index++; } return(index); }
/// <summary> /// Handles 'T' cases. /// </summary> private int HandleT(string value, DoubleMetaphoneResult result, int index) { if (Contains(value, index, 4, "TION")) { result.Append('X'); index += 3; } else if (Contains(value, index, 3, "TIA", "TCH")) { result.Append('X'); index += 3; } else if (Contains(value, index, 2, "TH") || Contains(value, index, 3, "TTH")) { if (Contains(value, index + 2, 2, "OM", "AM") || //-- special case "thomas", "thames" or germanic --// Contains(value, 0, 4, "VAN ", "VON ") || Contains(value, 0, 3, "SCH")) { result.Append('T'); } else { result.Append('0', 'T'); } index += 2; } else { result.Append('T'); index = Contains(value, index + 1, 1, "T", "D") ? index + 2 : index + 1; } return(index); }
//-- BEGIN HANDLERS --// /// <summary> /// Handles 'A', 'E', 'I', 'O', 'U', and 'Y' cases. /// </summary> private int HandleAEIOUY(DoubleMetaphoneResult result, int index) { if (index == 0) { result.Append('A'); } return(index + 1); }
//-- BEGIN HANDLERS --// /** * Handles 'A', 'E', 'I', 'O', 'U', and 'Y' cases */ private int handleAEIOUY(String value, DoubleMetaphoneResult result, int index) { if (index == 0) { result.append('A'); } return(index + 1); }
/// <summary> /// Handles 'P' cases. /// </summary> private int HandleP(string value, DoubleMetaphoneResult result, int index) { if (CharAt(value, index + 1) == 'H') { result.Append('F'); index += 2; } else { result.Append('P'); index = Contains(value, index + 1, 1, "P", "B") ? index + 2 : index + 1; } return(index); }
/// <summary> /// Handles 'J' cases. /// </summary> private int HandleJ(string value, DoubleMetaphoneResult result, int index, bool slavoGermanic) { if (Contains(value, index, 4, "JOSE") || Contains(value, 0, 4, "SAN ")) { //-- obvious Spanish, "Jose", "San Jacinto" --// if ((index == 0 && (CharAt(value, index + 4) == ' ') || value.Length == 4) || Contains(value, 0, 4, "SAN ")) { result.Append('H'); } else { result.Append('J', 'H'); } index++; } else { if (index == 0 && !Contains(value, index, 4, "JOSE")) { result.Append('J', 'A'); } else if (IsVowel(CharAt(value, index - 1)) && !slavoGermanic && (CharAt(value, index + 1) == 'A' || CharAt(value, index + 1) == 'O')) { result.Append('J', 'H'); } else if (index == value.Length - 1) { result.Append('J', ' '); } else if (!Contains(value, index + 1, 1, L_T_K_S_N_M_B_Z) && !Contains(value, index - 1, 1, "S", "K", "L")) { result.Append('J'); } if (CharAt(value, index + 1) == 'J') { index += 2; } else { index++; } } return(index); }
/** * Handles 'W' cases */ private int handleW(String value, DoubleMetaphoneResult result, int index) { if (contains(value, index, 2, "WR")) { //-- can also be in middle of word --// result.append('R'); index += 2; } else { if (index == 0 && (isVowel(charAt(value, index + 1)) || contains(value, index, 2, "WH"))) { if (isVowel(charAt(value, index + 1))) { //-- Wasserman should match Vasserman --// result.append('A', 'F'); } else { //-- need Uomo to match Womo --// result.append('A'); } index++; } else if ((index == value.length() - 1 && isVowel(charAt(value, index - 1))) || contains(value, index - 1, 5, "EWSKI", "EWSKY", "OWSKI", "OWSKY") || contains(value, 0, 3, "SCH")) { //-- Arnow should match Arnoff --// result.appendAlternate('F'); index++; } else if (contains(value, index, 4, "WICZ", "WITZ")) { //-- Polish e.g. "filipowicz" --// result.append("TS", "FX"); index += 4; } else { index++; } } return(index); }
/// <summary> /// Handles 'R' cases. /// </summary> private int HandleR(string value, DoubleMetaphoneResult result, int index, bool slavoGermanic) { if (index == value.Length - 1 && !slavoGermanic && Contains(value, index - 2, 2, "IE") && !Contains(value, index - 4, 2, "ME", "MA")) { result.AppendAlternate('R'); } else { result.Append('R'); } return(CharAt(value, index + 1) == 'R' ? index + 2 : index + 1); }
/// <summary> /// Handles 'H' cases. /// </summary> private int HandleH(string value, DoubleMetaphoneResult result, int index) { //-- only keep if first & before vowel or between 2 vowels --// if ((index == 0 || IsVowel(CharAt(value, index - 1))) && IsVowel(CharAt(value, index + 1))) { result.Append('H'); index += 2; //-- also takes car of "HH" --// } else { index++; } return(index); }
/** * Handles 'GH' cases */ private int handleGH(String value, DoubleMetaphoneResult result, int index) { if (index > 0 && !isVowel(charAt(value, index - 1))) { result.append('K'); index += 2; } else if (index == 0) { if (charAt(value, index + 2) == 'I') { result.append('J'); } else { result.append('K'); } index += 2; } else if ((index > 1 && contains(value, index - 2, 1, "B", "H", "D")) || (index > 2 && contains(value, index - 3, 1, "B", "H", "D")) || (index > 3 && contains(value, index - 4, 1, "B", "H"))) { //-- Parker's rule (with some further refinements) - "hugh" index += 2; } else { if (index > 2 && charAt(value, index - 1) == 'U' && contains(value, index - 3, 1, "C", "G", "L", "R", "T")) { //-- "laugh", "McLaughlin", "cough", "gough", "rough", "tough" result.append('F'); } else if (index > 0 && charAt(value, index - 1) != 'I') { result.append('K'); } index += 2; } return(index); }
/** * Handles 'SC' cases */ private int handleSC(String value, DoubleMetaphoneResult result, int index) { if (charAt(value, index + 2) == 'H') { //-- Schlesinger's rule --// if (contains(value, index + 3, 2, "OO", "ER", "EN", "UY", "ED", "EM")) { //-- Dutch origin, e.g. "school", "schooner" --// if (contains(value, index + 3, 2, "ER", "EN")) { //-- "schermerhorn", "schenker" --// result.append("X", "SK"); } else { result.append("SK"); } } else { if (index == 0 && !isVowel(charAt(value, 3)) && charAt(value, 3) != 'W') { result.append('X', 'S'); } else { result.append('X'); } } } else if (contains(value, index + 2, 1, "I", "E", "Y")) { result.append('S'); } else { result.append("SK"); } return(index + 3); }
/// <summary> /// Handles 'X' cases. /// </summary> private int HandleX(string value, DoubleMetaphoneResult result, int index) { if (index == 0) { result.Append('S'); index++; } else { if (!((index == value.Length - 1) && (Contains(value, index - 3, 3, "IAU", "EAU") || Contains(value, index - 2, 2, "AU", "OU")))) { //-- French e.g. breaux --// result.Append("KS"); } index = Contains(value, index + 1, 1, "C", "X") ? index + 2 : index + 1; } return(index); }
/** * Handles 'CH' cases */ private int handleCH(String value, DoubleMetaphoneResult result, int index) { if (index > 0 && contains(value, index, 4, "CHAE")) { // Michael result.append('K', 'X'); return(index + 2); } else if (conditionCH0(value, index)) { //-- Greek roots ("chemistry", "chorus", etc.) --// result.append('K'); return(index + 2); } else if (conditionCH1(value, index)) { //-- Germanic, Greek, or otherwise 'ch' for 'kh' sound --// result.append('K'); return(index + 2); } else { if (index > 0) { if (contains(value, 0, 2, "MC")) { result.append('K'); } else { result.append('X', 'K'); } } else { result.append('X'); } return(index + 2); } }
/// <summary> /// Handles 'L' cases. /// </summary> private int HandleL(string value, DoubleMetaphoneResult result, int index) { if (CharAt(value, index + 1) == 'L') { if (ConditionL0(value, index)) { result.AppendPrimary('L'); } else { result.Append('L'); } index += 2; } else { index++; result.Append('L'); } return(index); }
/** * Handles 'Z' cases */ private int handleZ(String value, DoubleMetaphoneResult result, int index, bool slavoGermanic) { if (charAt(value, index + 1) == 'H') { //-- Chinese pinyin e.g. "zhao" or Angelina "Zhang" --// result.append('J'); index += 2; } else { if (contains(value, index + 1, 2, "ZO", "ZI", "ZA") || (slavoGermanic && (index > 0 && charAt(value, index - 1) != 'T'))) { result.append("S", "TS"); } else { result.append('S'); } index = charAt(value, index + 1) == 'Z' ? index + 2 : index + 1; } return(index); }
/// <summary> /// Encode a value with Double Metaphone, optionally using the alternate encoding. /// </summary> /// <param name="value">String to encode.</param> /// <param name="alternate">Use alternate encode.</param> /// <returns>An encoded string.</returns> public virtual string GetDoubleMetaphone(string value, bool alternate) { value = CleanInput(value); if (value == null) { return(null); } bool slavoGermanic = IsSlavoGermanic(value); int index = IsSilentStart(value) ? 1 : 0; DoubleMetaphoneResult result = new DoubleMetaphoneResult(this.MaxCodeLen); while (!result.IsComplete && index <= value.Length - 1) { switch (value[index]) { case 'A': case 'E': case 'I': case 'O': case 'U': case 'Y': index = HandleAEIOUY(result, index); break; case 'B': result.Append('P'); index = CharAt(value, index + 1) == 'B' ? index + 2 : index + 1; break; case '\u00C7': // A C with a Cedilla result.Append('S'); index++; break; case 'C': index = HandleC(value, result, index); break; case 'D': index = HandleD(value, result, index); break; case 'F': result.Append('F'); index = CharAt(value, index + 1) == 'F' ? index + 2 : index + 1; break; case 'G': index = HandleG(value, result, index, slavoGermanic); break; case 'H': index = HandleH(value, result, index); break; case 'J': index = HandleJ(value, result, index, slavoGermanic); break; case 'K': result.Append('K'); index = CharAt(value, index + 1) == 'K' ? index + 2 : index + 1; break; case 'L': index = HandleL(value, result, index); break; case 'M': result.Append('M'); index = ConditionM0(value, index) ? index + 2 : index + 1; break; case 'N': result.Append('N'); index = CharAt(value, index + 1) == 'N' ? index + 2 : index + 1; break; case '\u00D1': // N with a tilde (spanish ene) result.Append('N'); index++; break; case 'P': index = HandleP(value, result, index); break; case 'Q': result.Append('K'); index = CharAt(value, index + 1) == 'Q' ? index + 2 : index + 1; break; case 'R': index = HandleR(value, result, index, slavoGermanic); break; case 'S': index = HandleS(value, result, index, slavoGermanic); break; case 'T': index = HandleT(value, result, index); break; case 'V': result.Append('F'); index = CharAt(value, index + 1) == 'V' ? index + 2 : index + 1; break; case 'W': index = HandleW(value, result, index); break; case 'X': index = HandleX(value, result, index); break; case 'Z': index = HandleZ(value, result, index, slavoGermanic); break; default: index++; break; } } return(alternate ? result.Alternate : result.Primary); }
/** * Encode a value with Double Metaphone, optionally using the alternate * encoding. * * @param value String to encode * @param alternate use alternate encode * @return an encoded string */ public String doubleMetaphone(String value, bool alternate) { value = cleanInput(value); if (value == null) { return(null); } bool slavoGermanic = isSlavoGermanic(value); int index = isSilentStart(value) ? 1 : 0; DoubleMetaphoneResult result = new DoubleMetaphoneResult(this.getMaxCodeLen(), this); while (!result.isComplete() && index <= value.length() - 1) { switch (value.charAt(index)) { case 'A': case 'E': case 'I': case 'O': case 'U': case 'Y': index = handleAEIOUY(value, result, index); break; case 'B': result.append('P'); index = charAt(value, index + 1) == 'B' ? index + 2 : index + 1; break; case '\u00C7': // A C with a Cedilla result.append('S'); index++; break; case 'C': index = handleC(value, result, index); break; case 'D': index = handleD(value, result, index); break; case 'F': result.append('F'); index = charAt(value, index + 1) == 'F' ? index + 2 : index + 1; break; case 'G': index = handleG(value, result, index, slavoGermanic); break; case 'H': index = handleH(value, result, index); break; case 'J': index = handleJ(value, result, index, slavoGermanic); break; case 'K': result.append('K'); index = charAt(value, index + 1) == 'K' ? index + 2 : index + 1; break; case 'L': index = handleL(value, result, index); break; case 'M': result.append('M'); index = conditionM0(value, index) ? index + 2 : index + 1; break; case 'N': result.append('N'); index = charAt(value, index + 1) == 'N' ? index + 2 : index + 1; break; case '\u00D1': // N with a tilde (spanish ene) result.append('N'); index++; break; case 'P': index = handleP(value, result, index); break; case 'Q': result.append('K'); index = charAt(value, index + 1) == 'Q' ? index + 2 : index + 1; break; case 'R': index = handleR(value, result, index, slavoGermanic); break; case 'S': index = handleS(value, result, index, slavoGermanic); break; case 'T': index = handleT(value, result, index); break; case 'V': result.append('F'); index = charAt(value, index + 1) == 'V' ? index + 2 : index + 1; break; case 'W': index = handleW(value, result, index); break; case 'X': index = handleX(value, result, index); break; case 'Z': index = handleZ(value, result, index, slavoGermanic); break; default: index++; break; } } return(alternate ? result.getAlternate() : result.getPrimary()); }
/// <summary> /// Handles 'S' cases. /// </summary> private int HandleS(string value, DoubleMetaphoneResult result, int index, bool slavoGermanic) { if (Contains(value, index - 1, 3, "ISL", "YSL")) { //-- special cases "island", "isle", "carlisle", "carlysle" --// index++; } else if (index == 0 && Contains(value, index, 5, "SUGAR")) { //-- special case "sugar-" --// result.Append('X', 'S'); index++; } else if (Contains(value, index, 2, "SH")) { if (Contains(value, index + 1, 4, "HEIM", "HOEK", "HOLM", "HOLZ")) { //-- germanic --// result.Append('S'); } else { result.Append('X'); } index += 2; } else if (Contains(value, index, 3, "SIO", "SIA") || Contains(value, index, 4, "SIAN")) { //-- Italian and Armenian --// if (slavoGermanic) { result.Append('S'); } else { result.Append('S', 'X'); } index += 3; } else if ((index == 0 && Contains(value, index + 1, 1, "M", "N", "L", "W")) || Contains(value, index + 1, 1, "Z")) { //-- german & anglicisations, e.g. "smith" match "schmidt" // // "snider" match "schneider" --// //-- also, -sz- in slavic language although in hungarian it // // is pronounced "s" --// result.Append('S', 'X'); index = Contains(value, index + 1, 1, "Z") ? index + 2 : index + 1; } else if (Contains(value, index, 2, "SC")) { index = HandleSC(value, result, index); } else { if (index == value.Length - 1 && Contains(value, index - 2, 2, "AI", "OI")) { //-- french e.g. "resnais", "artois" --// result.AppendAlternate('S'); } else { result.Append('S'); } index = Contains(value, index + 1, 1, "S", "Z") ? index + 2 : index + 1; } return(index); }
/// <summary> /// Handles 'C' cases. /// </summary> private int HandleC(string value, DoubleMetaphoneResult result, int index) { if (ConditionC0(value, index)) { // very confusing, moved out result.Append('K'); index += 2; } else if (index == 0 && Contains(value, index, 6, "CAESAR")) { result.Append('S'); index += 2; } else if (Contains(value, index, 2, "CH")) { index = HandleCH(value, result, index); } else if (Contains(value, index, 2, "CZ") && !Contains(value, index - 2, 4, "WICZ")) { //-- "Czerny" --// result.Append('S', 'X'); index += 2; } else if (Contains(value, index + 1, 3, "CIA")) { //-- "focaccia" --// result.Append('X'); index += 3; } else if (Contains(value, index, 2, "CC") && !(index == 1 && CharAt(value, 0) == 'M')) { //-- double "cc" but not "McClelland" --// return(HandleCC(value, result, index)); } else if (Contains(value, index, 2, "CK", "CG", "CQ")) { result.Append('K'); index += 2; } else if (Contains(value, index, 2, "CI", "CE", "CY")) { //-- Italian vs. English --// if (Contains(value, index, 3, "CIO", "CIE", "CIA")) { result.Append('S', 'X'); } else { result.Append('S'); } index += 2; } else { result.Append('K'); if (Contains(value, index + 1, 2, " C", " Q", " G")) { //-- Mac Caffrey, Mac Gregor --// index += 3; } else if (Contains(value, index + 1, 1, "C", "K", "Q") && !Contains(value, index + 1, 2, "CE", "CI")) { index += 2; } else { index++; } } return(index); }
/// <summary> /// Handles 'G' cases. /// </summary> private int HandleG(string value, DoubleMetaphoneResult result, int index, bool slavoGermanic) { if (CharAt(value, index + 1) == 'H') { index = HandleGH(value, result, index); } else if (CharAt(value, index + 1) == 'N') { if (index == 1 && IsVowel(CharAt(value, 0)) && !slavoGermanic) { result.Append("KN", "N"); } else if (!Contains(value, index + 2, 2, "EY") && CharAt(value, index + 1) != 'Y' && !slavoGermanic) { result.Append("N", "KN"); } else { result.Append("KN"); } index = index + 2; } else if (Contains(value, index + 1, 2, "LI") && !slavoGermanic) { result.Append("KL", "L"); index += 2; } else if (index == 0 && (CharAt(value, index + 1) == 'Y' || Contains(value, index + 1, 2, ES_EP_EB_EL_EY_IB_IL_IN_IE_EI_ER))) { //-- -ges-, -gep-, -gel-, -gie- at beginning --// result.Append('K', 'J'); index += 2; } else if ((Contains(value, index + 1, 2, "ER") || CharAt(value, index + 1) == 'Y') && !Contains(value, 0, 6, "DANGER", "RANGER", "MANGER") && !Contains(value, index - 1, 1, "E", "I") && !Contains(value, index - 1, 3, "RGY", "OGY")) { //-- -ger-, -gy- --// result.Append('K', 'J'); index += 2; } else if (Contains(value, index + 1, 1, "E", "I", "Y") || Contains(value, index - 1, 4, "AGGI", "OGGI")) { //-- Italian "biaggi" --// if (Contains(value, 0, 4, "VAN ", "VON ") || Contains(value, 0, 3, "SCH") || Contains(value, index + 1, 2, "ET")) { //-- obvious germanic --// result.Append('K'); } else if (Contains(value, index + 1, 3, "IER")) { result.Append('J'); } else { result.Append('J', 'K'); } index += 2; } else if (CharAt(value, index + 1) == 'G') { index += 2; result.Append('K'); } else { index++; result.Append('K'); } return(index); }