Channels ▼
RSS

The Double Metaphone Search Algorithm


June 2000/The Double Metaphone Search Algorithm/Figure 1

Figure 1: The part of the Double Metaphone algorithm that processes the letter 'C'

case 'C':
   if((current > 1  )//various germanic
      AND !IsVowel(current - 2) 
      AND StringAt((current - 1), 3, "ACH", "") 
      AND ((GetAt(current + 2) != 'I') 
           AND ((GetAt(current + 2) != 'E') 
      OR StringAt((current - 2), 6, "BACHER", "MACHER", "")) )) {
         MetaphAdd("K"); current +=2; break;
   }
   //special case 'caesar'
   if((current == 0) AND StringAt(current, 6, "CAESAR", "")) {  
      MetaphAdd("S"); current +=2; break;
   }
   if(StringAt(current, 4, "CHIA", "")) {  //italian 'chianti'
      MetaphAdd("K"); current +=2; break;
   }
   if(StringAt(current, 2, "CH", "")) {    
      //find 'michael'
      if((current > 0) AND StringAt(current, 4, "CHAE", "")) {  
         MetaphAdd("K", "X"); current +=2; break;
      }
      if((current == 0)  //greek roots e.g. 'chemistry', 'chorus'
         AND (StringAt((current + 1), 5, "HARAC", "HARIS", "") 
         OR StringAt((current + 1), 3, "HOR", 
                     "HYM", "HIA", "HEM", ""))
         AND !StringAt(0, 5, "CHORE", "")) {

         MetaphAdd("K"); current +=2; break;
      }
      //germanic, greek, or otherwise 'ch' for 'kh' sound
      if((StringAt(0, 4, "VAN ", "VON ", "") 
         OR StringAt(0, 3, "SCH", "")) 

         // 'architect but not 'arch', 'orchestra', 'orchid'
         OR StringAt((current - 2), 6, "ORCHES", "ARCHIT", 
                     "ORCHID")
         OR StringAt((current + 2), 1, "T", "S", "")
         OR ((StringAt((current - 1), 1, "A", "O", "U", "E", "") 
             OR (current == 0))

         //e.g., 'wachtler', 'wechsler', but not 'tichner'
         AND StringAt((current + 2), 1, "L", "R", "N", "M", "B", 
                      "H", "F", "V", "W", " ", ""))) {  
         MetaphAdd("K");
      } else {    
         if(current > 0) {
            if(StringAt(0, 2, "MC", ""))  //e.g., "McHugh"
               MetaphAdd("K"); 
            else MetaphAdd("X", "K");
         }
            else  MetaphAdd("X");
      }
         current +=2; break;
   }
   //e.g, 'czerny'
   if(StringAt(current, 2, "CZ", "") 
      AND !StringAt((current - 2), 4, "WICZ", "")) { 
         MetaphAdd("S", "X"); current += 2; break;
   }
   //e.g., 'focaccia'
   if(StringAt((current + 1), 3, "CIA", "")) {  
      MetaphAdd("X"); current += 3; break;
   }
   //double 'C', but not if e.g. 'McClellan'
   if(StringAt(current, 2, "CC", "") AND !((current == 1) 
      AND (GetAt(0) == 'M')))  
      //'bellocchio' but not 'bacchus'
      if(StringAt((current + 2), 1, "I", "E", "H") 
         AND !StringAt((current + 2), 2, "HU", "")) {  
            
         //'accident', 'accede' 'succeed'
         if(((current == 1) AND (GetAt(current - 1) == 'A')) 
            OR StringAt((current - 1), 5, "UCCEE", "UCCES", ""))
                
            MetaphAdd("KS");
         else  //'bacci', 'bertucci', other italian
            MetaphAdd("X");
            
         current += 3; break;
      } else {//Pierce's rule
         MetaphAdd("K"); current += 2; break;
      }
   if(StringAt(current, 2, "CK", "CG", "CQ", "")) {
      MetaphAdd("K"); current += 2; break;
   }
   if(StringAt(current, 2, "CI", "CE", "CY", "")) {
      //italian vs. english
      if(StringAt(current, 3, "CIO", "CIE", "CIA", ""))  
         MetaphAdd("S", "X");
      else
         MetaphAdd("S");
      current += 2; break;
   }  //else
   MetaphAdd("K");

   //name sent in 'mac caffrey', 'mac gregor
   if(StringAt((current + 1), 2, " C", " Q", " G", ""))  
      current += 3;
   else
      if(StringAt((current + 1), 1, "C", "K", "Q", "") 
         AND !StringAt((current + 1), 2, "CE", "CI", ""))

         current += 2; else current += 1;
   break;


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video