Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Implement Phonetic ("Sounds-like") Name Searches with Double Metaphone Part V: .NET Implementation

, 19 Mar 2007
Presents a C# implementation of Double Metaphone, for use with any of the .NET languages.
dmetaphone_demo.zip
CSWordLookup
App.ico
bin
Release
CSWordLookup.exe
Metaphone.NET.dll
obj
Release
temp
VBScriptWordLookup
VBWordDBLookup
frmMain.frm
MSSCCPRJ.SCC
VBWordDBLookup.vbp
VBWordDBLookup.vbw
VBWordLookup.exe
VBWordLookup
frmMain.frm
MSSCCPRJ.SCC
VBWordLookup.exe
VBWordLookup.vbp
VBWordLookup.vbw
WordLookupSample
Release
WordLookupSample.exe
ASPWordDBLookup
namelist.mdb
dmetaphone_demo1.zip
App.ico
namelist.mdb
VBScriptWordLookup.vbs.fascistemailclient
frmMain.frm
MSSCCPRJ.SCC
VBWordDBLookup.vbp
VBWordDBLookup.vbw
frmMain.frm
MSSCCPRJ.SCC
VBWordLookup.vbp
VBWordLookup.vbw
dmetaphone_src.zip
MetaphoneCOM
Debug
MetaphoneCOM.aps
MetaphoneCOM.bakvpj
MetaphoneCOM.rgs
MetaphoneCOM.vpj
MetaphoneCOMps.def
MetaphoneCOMPS.vpj
Release
MetaphoneCOM.dll
MetaphoneCOM.exp
MetaphoneCOM.lib
MetaphoneCOM.res
MetaphoneLib
Debug
MetaphoneLib.bakvpj
MetaphoneLib.vpj
Release
DoubleMetaphone.obj
MetaphoneLib.lib
stdafx.obj
XPMetaphone
Debug
Release
XPMetaphone.dll
XPMetaphone.exp
XPMetaphone.lib
XPMetaphone.obj
XPMetaphone.res
XPMetaphone.bakvpj
XPMetaphone.vpj
Metaphone.NET
bin
Debug
Release
Metaphone.NET.dll
Metaphone.NET.bakvpj
Metaphone.NET.csproj.user
Metaphone.NET.vpj
obj
Debug
temp
TempPE
Release
temp
TempPE
dmetaphone_src1.zip
Metaphone.NET.bakvpj
Metaphone.NET.csproj.user
Metaphone.NET.vpj
MetaphoneCOM.aps
MetaphoneCOM.bakvpj
MetaphoneCOM.rgs
MetaphoneCOM.vpj
MetaphoneCOMps.def
MetaphoneCOMPS.vpj
Test.vbs.braindeademailclient
MetaphoneLib.bakvpj
MetaphoneLib.vpj
XPMetaphone.bakvpj
XPMetaphone.vpj
Option Explicit

dim wordMap

 'Create the dictionary which will contain the phonetic key->word map
 Set wordMap = CreateObject("Scripting.Dictionary")
 
 'Read the namelist file
 Dim oFile
 Dim oStream
 Dim word
 Dim words
 Dim mphone
 Dim primaryKey
 Dim alternateKey
 
 Set oFile = CreateObject("Scripting.FileSystemObject")
 Set oStream = oFile.OpenTextFile("..\namelist.txt")
 Set mphone = CreateObject("MetaphoneCOM.DoubleMetaphoneString")
 
 WScript.Echo "Loading name data; this will take several seconds..."
 
 While Not oStream.AtEndOfStream
     word = oStream.ReadLine
     mphone.ComputeMetaphoneKeysScr word, primaryKey, alternateKey
     
     'Add an entry to the dictionary for each key
     If Not wordMap.Exists(primaryKey) Then
         'No words associated with this key, so create an empty entry for it
         wordMap.Add primaryKey, Array()
     End If
     
     'Get the array of words for the key, then grow it by one and add
     'the word we just read
     words = wordMap.Item(primaryKey)
     ReDim Preserve words(UBound(words) + 1)
     words(UBound(words)) = word
     wordMap.Item(primaryKey) = words
     
     If Len(alternateKey) > 0 Then
         'Alternate key also computed
         If Not wordMap.Exists(alternateKey) Then
             'No words associated with this key, so create an empty entry for it
             wordMap.Add alternateKey, Array()
         End If
         
         'Get the array of words for the key, then grow it by one and add
         'the word we just read
         words = wordMap.Item(alternateKey)
         ReDim Preserve words(UBound(words) + 1)
         words(UBound(words)) = word
         wordMap.Item(alternateKey) = words
     End If
 Wend
 oStream.Close
 
 'Begin the search
dim searchWord
dim results
Dim wordIdx
Dim listIdx
dim resultsString

'Hack the dictionary object for use a a Set, which does not allow duplicate entries, to
'de-dupe the list of results
set results = CreateObject("Scripting.Dictionary")

while true
   searchWord = InputBox("Enter name to search for", "VBScript Word Lookup", "Nelson")
   if searchWord = "" then
      WScript.Quit()
   end if
   searchWord = Trim(searchWord)
   If Len(searchWord) = 0 Then
      MsgBox "You must enter a search word"
      WScript.Quit()
   End If
   
   mphone.ComputeMetaphoneKeysScr searchWord, primaryKey, alternateKey
   results.RemoveAll
   
   If wordMap.Exists(primaryKey) Then
      words = wordMap.Item(primaryKey)
      
      For wordIdx = 0 To UBound(words)
         Results(words(wordIdx)) = true
      Next
   End If
   
   If Len(alternateKey) > 0 Then
     'Also an alternate key.  Search with that
     If wordMap.Exists(alternateKey) Then
         words = wordMap.Item(alternateKey)
         
         For wordIdx = 0 To UBound(words)
            Results(words(wordIdx)) = true
         Next
     End If
   End If
   
   'The Keys property of the results dictionary contains a list of unique words from
   'the results
   resultsString = "Found " & results.Count & " matches:" & vbCrLf
   
   for each word in results.Keys
      resultsString = resultsString & vbTab & word & vbCrLf
   next
   
   MsgBox resultsString, , "VBScript Word Lookup"
wend

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Adam Nelson
Web Developer
United States United States
My name is Adam Nelson. I've been a professional programmer since 1996, working on everything from database development, early first-generation web applications, modern n-tier distributed apps, high-performance wireless security tools, to my last job as a Senior Consultant at BearingPoint posted in Baghdad, Iraq training Iraqi developers in the wonders of C# and ASP.NET. I am currently an Engineering Director at Dell.
 
I have a wide range of skills and interests, including cryptography, image processing, computational linguistics, military history, 3D graphics, database optimization, and mathematics, to name a few.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 19 Mar 2007
Article Copyright 2003 by Adam Nelson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid