Important alert: (current site time 7/16/2013 4:13:56 AM EDT)
 

VB icon

ConvertToSoundex

Email
Submitted on: 9/22/1998
By: Darrell Sparti, MCSD  
Level: Not Given
User Rating: By 4 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script
Views: 28594
author picture
(About the author)
 
     Converts a name or word string to a four digit code following Soundex rules. Similar code is used by geniological groups and the US Census Bureau for looking up names by phonetic sound. For example, the name Darrell can be spelled many different ways. Regardles of how you spell it, (Daryl, Derrel, Darel, etc.) the Soundex code is always D640. Therefore, you assign a field in your database to the Soundex code and then query the database using the code, all instances of Darrell regarless of spelling will be returned. Refer to the code comment section for more information.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: ConvertToSoundex
' Description:Converts a name or word string to a four digit code following Soundex rules.
Similar code is used by geniological groups and the US Census Bureau for
looking up names by phonetic sound. For example, the name Darrell can
be spelled many different ways. Regardles of how you spell it, (Daryl, Derrel,
Darel, etc.) the Soundex code is always D640. Therefore, you assign a field
in your database to the Soundex code and then query the database using
the code, all instances of Darrell regarless of spelling will be returned. Refer
to the code comment section for more information.
' By: Darrell Sparti, MCSD
'
' Inputs:A single name or word string.
'
' Returns:A four digit alphanumeric Soundex code.
'
' Side Effects:This code has not been commercially tested.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1061&lngWId=1'for details.'**************************************

'***********************************************************************
'Function Name:ConvertToSoundex
'Argument:A single name or word string
'Return value:A 4 character code based on Soundex rules
'Author:Darrell Sparti
'EMail:dsparti@allwest.net
'Date: 9-20-98
'Description:All Soundex codes have 4 alphanumeric
' characters, no more and no less, regardless
' of the length of the string. The first
' character is a letter and the other 3 are
' numbers. The first letter of the string is
' the first letter of the Soundex code. The
' 3 digits are defined sequentially from the
' string using the following key:
' 1 = bpfv
' 2 = cskgjqxz
' 3 = dt
' 4 = l
' 5 = mn
' 6 = r
' No Code = aehiouyw
' If the end of the string is reached before
' filling in 3 numbers, 0's complete the code.
' Example: Swartz= S632
' Example: Darrell= D640
' Example: Schultz = S432
'NOTE:I have noticed some errors in other versions
'of soundex code. Most noticably is the
'fact that not only must the code ignore
'the second letter in repeating letters
'(ll,rr,tt,etc. for example), it must also
'ignore letters next to one another with the
'same soundex code (s and c for example).
'Other wise, in the example above, Schultz
'would return a value of S243 which is
'incorrect.
'********************************************************************
Option Explicit
Public Function ConvertToSoundex(sInString As String) As String
Dim sSoundexCode As String
Dim sCurrentCharacter As String
Dim sPreviousCharacter As String
Dim iCharacterCount As Integer
'Convert the string to upper case letters and remove spaces
sInString = UCase$(Trim(sInString))
'The soundex code will start with the first character _
of the string
sSoundexCode = Left(sInString, 1)
'Check the other characters starting at the second character
iCharacterCount = 2
'Continue the conversion until the soundex code is 4 _
characters long regarless of the length of the string
Do While Not Len(sSoundexCode) = 4
 
 'If the previous character has the same soundex code as _
 current character or the previous character is the same _
 as the current character, ignor it and move onto the next
 
 sCurrentCharacter = Mid$(sInString, iCharacterCount, 1)
 sPreviousCharacter = Mid$(sInString, iCharacterCount - 1, 1)
 
 If sCurrentCharacter = sPreviousCharacter Then
 iCharacterCount = iCharacterCount + 1
 ElseIf InStr("BFPV", sCurrentCharacter) Then
 If InStr("BFPV", sPreviousCharacter) Then
iCharacterCount = iCharacterCount + 1
 End If
 ElseIf InStr("CGJKQSXZ", sCurrentCharacter) Then
 If InStr("CGJKQSXZ", sPreviousCharacter) Then
iCharacterCount = iCharacterCount + 1
 End If
 ElseIf InStr("DT", sCurrentCharacter) Then
If InStr("DT", sPreviousCharacter) Then
iCharacterCount = iCharacterCount + 1
End If
 ElseIf InStr("MN", sCurrentCharacter) Then
If InStr("MN", sPreviousCharacter) Then
iCharacterCount = iCharacterCount + 1
End If
 Else
 End If
 
 'If the end of the string is reached before there are 4 _
 characters in the soundex code, add 0 until there are _
 a total of 4 characters in the code
 If iCharacterCount > Len(sInString) Then
 sSoundexCode = sSoundexCode & "0"
 
 'Otherwise, concatenate a number to the soundex code _
 base on soundex rules
 Else
 sCurrentCharacter = Mid$(sInString, iCharacterCount, 1)
 If InStr("BFPV", sCurrentCharacter) Then
sSoundexCode = sSoundexCode & "1"
 ElseIf InStr("CGJKQSXZ", sCurrentCharacter) Then
sSoundexCode = sSoundexCode & "2"
 ElseIf InStr("DT", sCurrentCharacter) Then
sSoundexCode = sSoundexCode & "3"
 ElseIf InStr("L", sCurrentCharacter) Then
sSoundexCode = sSoundexCode & "4"
 ElseIf InStr("MN", sCurrentCharacter) Then
sSoundexCode = sSoundexCode & "5"
 ElseIf InStr("R", sCurrentCharacter) Then
sSoundexCode = sSoundexCode & "6"
 Else
 End If
 End If
 
 'Check the next letter
 iCharacterCount = iCharacterCount + 1
Loop
'Return the soundex code for the string
ConvertToSoundex = sSoundexCode
End Function


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/1/1999 6:09:00 AMDmitry A. Kirsanov

Cool! Thanks for a great code, right what I've been searching for :)
(If this comment was disrespectful, please report it.)

 
8/1/1999 8:09:00 AManti

...
this...is
...the coolest...
thing i've...
ever seen...
(If this comment was disrespectful, please report it.)

 
8/1/1999 8:12:00 AManti

A534 L220 T200 C300 ¦300
anti likes this code ¦D
(If this comment was disrespectful, please report it.)

 
8/3/1999 3:33:00 PMmark

excellent academic algorithm and one that anyone who manages a database of many names will adore!
(If this comment was disrespectful, please report it.)

 
8/21/1999 6:18:00 AMchaolemchai

i want example program with mutimedia by visual basic
i want name of web for dowloads example program visual basic .
(If this comment was disrespectful, please report it.)

 
8/23/1999 12:39:00 AMBigEd

Optimized code that does the same thing. "PadZeros" is for SQL's "Like"

Public Function ConvertToSoundex(ByVal Word As String, Optional PadZeros As Boolean = True) As String
Dim NumericVals As String * 26
Dim lp As Long
Dim char As String * 1
Dim charVal As Integer
Dim ThisCode As String * 1
Dim PrevCode As String * 1
Dim Sdx As String
Word = Trim$(UCase$(Word))
!values for each letter A to Z
NumericVals="01230120022455012623010202"
Sdx = Left$(Word, 1)
PrevCode = ""
lp = 2
Do While lp<=Len(Word)And Len(Sdx)<=4
char = Mid$(Word, lp, 1) charVal = Asc(char) -64
If charVal>=1 And charVal<=26 Then
ThisCode = Mid$(NumericVals,charVal,1)
If ThisCode<>PrevCode And ThisCode<>"0" Then
Sdx = Sdx & ThisCode
PrevCode = ThisCode
End If
End If
lp = lp + 1
Loop
If PadZeros Then Sdx=Left$(Sdx &"0000",4)
ConvertToSoundex = Sdx
End Function
(If this comment was disrespectful, please report it.)

 
9/24/1999 4:10:00 PMDarrell Sparti

To BigEd: Though your algorythm seems optimized, it is incorrect. Your algorythm does the same thing that inspired me to write my own. For instance, S and C have the same soundex code. If you had a name like Schwartz, the Sch are one code not 2. I spent a lot of time researching soundex rules and algorythms before I wrote this one. Perhaps there are ways to optimize it but they would have to work and give you the same code as this one to be called Soundex. So if you choose to optimize my code, be sure it returns the correct values.
(If this comment was disrespectful, please report it.)

 
3/4/2000 12:35:34 AMBigEd

My code is correct as is yours.
SCH is taken care of properly since S = C (both 2) and H=zero with the following line--

ThisCode<>PrevCode And ThisCode<>"0"

It's funny that
http://www.vb2themax.com/Item.asp?PageID=CodeBank&ID=158
(02/26/2000) uses the same syntax as mine.


(If this comment was disrespectful, please report it.)

 
3/4/2000 1:24:18 AMBigEd

I stand corrected -- I see MY error. I don't look at the S since it is automatically assign BEFORE the loop.

Changing MY code from:
PrevCode = ""
TO:
PrevCode=Mid$(NumericVals,Asc(Left$(word,1))-64,1)

fixed it, thanks.

(If this comment was disrespectful, please report it.)

 
7/17/2009 11:59:55 AMpaschal

i mean converting letters to sound
that pronounceing letters
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.