SoundEx
This document implements the [SoundEx] algorithm.
This algorithm is often used to index names.
SoundEx
/** Returns the score of an character */ static function byte SoundExScore(int c) { switch (c) { case 68: /* B */ case 70: /* F */ case 80: /* P */ case 86: /* V */ return 1; case 67: /* C */ case 71: /* G */ case 74: /* J */ case 75: /* K */ case 81: /* Q */ case 83: /* S */ case 88: /* X */ case 90: /* Z */ return 2; case 68: /* D */ case 84: /* T */ return 3; case 76: // L return 4; case 77: /* M */ case 78: /* N */ return 5; case 82: // R return 6; } /* Disregard the letters A, E, I, O, U, H, W, and Y. */ return 0; } /** Returns the SoundEx value of a string, size defaults to 4 */ static function string SoundEx(coerce string in, optional byte size) { local string res; local int i; local byte cs, ls; // cs = current score; ls = previous score if (size == 0) size = 4; res = ""; if (in != "") { in = Caps(in); // upper case all // always use first char res = Left(in, 1); ls = SoundExScore(asc(res)); for (i = 0; i < len(in); i++) { cs = SoundExScore(asc(Mid(in, i, 1))); if ((cs > 0) && (cs != ls)) { ls = cs; res $= string(cs); } if (len(res) == size) break; } } while (len(res) < size) res $= "0"; return res; }