Click here to Skip to main content
15,891,423 members
Articles / Programming Languages / C++

Implement Phonetic ("Sounds-like") Name Searches with Double Metaphone Part I: Introduction & C++ Implementation

Rate me:
Please Sign up or sign in to vote.
4.91/5 (21 votes)
19 Mar 2007CPOL15 min read 148.6K   2.8K   60  
Introduces the Double Metaphone algorithm for phonetic comparison of proper names, and provides a practical C++ implementation for use in the reader's projects.
VERSION 5.00
Begin VB.Form frmMain 
   Caption         =   "VB Word DB Lookup - Double Metaphone Sample"
   ClientHeight    =   3975
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   5070
   LinkTopic       =   "Form1"
   ScaleHeight     =   3975
   ScaleWidth      =   5070
   StartUpPosition =   3  'Windows Default
   Begin VB.ListBox lstResults 
      Height          =   2400
      Left            =   240
      TabIndex        =   4
      Top             =   1200
      Width           =   2175
   End
   Begin VB.CommandButton btnFind 
      Caption         =   "Search"
      Height          =   375
      Left            =   3360
      TabIndex        =   2
      Top             =   120
      Width           =   1575
   End
   Begin VB.TextBox txtSearchWord 
      Height          =   285
      Left            =   1200
      TabIndex        =   1
      Top             =   120
      Width           =   1935
   End
   Begin VB.Label Label2 
      Caption         =   "Results:"
      Height          =   255
      Left            =   120
      TabIndex        =   3
      Top             =   600
      Width           =   1335
   End
   Begin VB.Label Label1 
      Caption         =   "Search For:"
      Height          =   255
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   975
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub btnFind_Click()
    'Perform the search
    Dim searchWord As String
    
    searchWord = Trim(Me.txtSearchWord.Text)
    If Len(searchWord) = 0 Then
        MsgBox "You must enter a search word"
        Exit Sub
    End If
    
    Me.lstResults.Clear
    
    'Compute the double metaphone keys for the search word
    Dim mphone As New MetaphoneCOM.DoubleMetaphoneShort
    Dim primaryKey As Integer
    Dim alternateKey As Integer
    
    mphone.ComputeMetaphoneKeys searchWord, primaryKey, alternateKey
    
    'Search the database
    Dim oConn As New ADODB.Connection
    
    'Use the namelist.mdb Access database in the parent directory
    oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\..\namelist.mdb"
    oConn.Open
    
    Dim oRS As ADODB.Recordset
    Dim strSql As String
    
    'Select from the Words table, which contains a list of names, and the
    'unsigned short representation of their Double Metaphone keys
    strSql = "select word from Words where"
    strSql = strSql & "(key1 = " & primaryKey & ")"
    strSql = strSql & " or (key2 = " & primaryKey & ")"
    If alternateKey <> MetaphoneKey.Invalid Then
        strSql = strSql & " or (key1 = " & alternateKey & ")"
        strSql = strSql & " or (key2 = " & alternateKey & ")"
    End If
    
    Set oRS = oConn.Execute(strSql)
    
    While Not oRS.EOF
        Me.lstResults.AddItem oRS("word")
        oRS.MoveNext
    Wend
    
    oConn.Close
End Sub

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
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.

Comments and Discussions