The SOUNDEX function in SQL, analyzes the phonetics of a given string of text and returns a value which can then be used to find similar values thus searching through text for similar sounding strings. The algorithm is actually a simple set of values assigned to similar sounding consonants.
Here’s the algorithm:
Letters B, F, P, V = 1
Letters C, G, J, K, Q, S, X, Z = 2
Letters D, T = 3
Letter L = 4
Letters M,N = 5
Letter R = 6
Letters A, E, I, O, U, H, W, Y are disregarded
Anything string resulting in a code with greater than four characters is truncated
Any string resulting in fewer than four characters is concatenated by zeros
Using the Soundex algorithm, I converted my name Tim Bartel to T500 B634.
Try it on your name using this JavaScript Soundex name converter:
There are three differences from the code that’s embedded on this page and the one below.
1. I’m not using a form to post back to the same page because of WordPress, so I’m using JavaScript in the button call to get the input values. View page source if you want to compare.
2.Removed carriage returns from the JavaScript – again to placate WordPress.
3. Layout of input fields using a table (It’s still vogue to layout forms and data with tables… just not entire pages – just gotta watch out for minimum widths on small mobile devices).
<!DOCTYPE html> <html> <body> <script language="JavaScript" type="text/javascript"> function soundex(first,last){ var soundex=""; var person={fname:first,lname:last}; if(first, last){ for (x in person){ var a = person[x].toUpperCase().split(''), f = a.shift(), r = '', codes = { A: '', E: '', I: '', O: '', U: '', H: '', W: '', Y: '', B: 1, F: 1, P: 1, V: 1, C: 2, G: 2, J: 2, K: 2, Q: 2, S: 2, X: 2, Z: 2, D: 3, T: 3, L: 4, M: 5, N: 5, R: 6 }; r = f + a.map(function (v, i, a) { return codes[v] ;}).filter(function (v, i, a) { return ((i === 0) ? v !== codes[f] : v !== a[i - 1]); }).join(''); soundex=soundex + (r + '000').slice(0, 4) + " "; } } else{ soundex = 'you must enter a first and last name'; } document.getElementById("sndxname").innerHTML=soundex; } </script> <form name="soundexForm" id="soundexForm" > First Name: <input type="text" name="fname" value="" / > <br /> Last Name: <input type="text" name="lname" value="" / > <br /> <input type="button" value="Soundex Me" onclick="soundex(document.soundexForm.fname.value,document.soundexForm.lname.value);" /> <br /> The soundex value of your name is: <span style="font-weight:bold;" id="sndxname" name="sndxname"></ span> </ form> </body> </html>
I used a soundex script posted to GitHub by Shawn Dumas (https://gist.github.com/shawndumas/1262659). I made the following changes:
1. Added explicit blank values for characters H, W and Y.
2. Removed unnecessary case change.
3. Added check for undefined variable where form is left blank
4. loop through first and last names
5. Concatenate first and last names into one string
6. Return results to page by innerHTML
7. Corrected many syntax errors – mostly missing semi-colons
Leave a Reply