« Dynamic image resizing in PHP | Home | Oracle: redo log switches by date »

JavaScript: Soundex implementation

There is a special algorithm for comparision strings, which sound similar (Soundex).

Here is JavaScript Soundex implementation:

function soundex ( s_src )
{
var s_rez = "0000" ;
var new_code, prev, idx

a_codes = { "bfpv": 1, "cgjkqsxz":2, "dt": 3, "l": 4, "mn": 5, "r": 6 };

s_src = s_src.toLowerCase().replace(/ /g,"")

if ( s_src.length < 1) {
return(s_rez);
}

s_rez = s_src.substr(0,1);
prev = "0";

for ( idx = 1 ; idx < s_src.length ; idx++) {
new_code = "0";
cur_char = s_src.substr(idx,1)

for (s_code in a_codes)
if (s_code.indexOf(cur_char) >= 0)
{ new_code = a_codes[ s_code ] ; break ; }

if (new_code != prev && new_code != "0" ) {
s_rez += new_code;
}

prev = new_code;
}

s_rez = s_rez + "0000"

return s_rez.substr(0,4);
}

Topics: javascript | Submitter: checkthis

Comments

You must be logged in to post a comment.