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 I: Introduction & C++ Implementation

, 19 Mar 2007
Introduces the Double Metaphone algorithm for phonetic comparison of proper names, and provides a practical C++ implementation for use in the reader's projects.
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
dmetaphone_demo1.zip
App.ico
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.aps
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
/**
 * DoubleMetaphoneString.cpp
 * 
 * Implementation of the Double Metaphone phonetic matching algorithm, wrapped as a dual-interface
 * COM component callable from Visual Basic and scripting clients.
 * 
 * For the latest version, implemenatations for other languages, and links to articles
 * I've written on how to use this and all my other Double Metaphone implementations, go to
 * http://www.nullpointer.net/anelson/
 * 
 * Implementation by Adam J. Nelson (anelson@nullpointer.net)
 * Copyright (C) 2003 Adam J. Nelson, All Rights Reserved
 * 
 * Current Version: 1.0.0
 * Revision History:
 * 	1.0.0 - ajn - First release
 * 
 * The Double Metaphone algorithm was written by Lawrence Phillips, and is 
 * Copyright (c) 1998, 1999 by Lawrence Philips.
 **/

#include "stdafx.h"
#include "DoubleMetaphoneString.h"

#define		METAPHONE_KEY_LENGTH	4	//Phillips says 4 seems just right, and my tests do as well


/** Identical in semantics to DoubleMetaphone::doubleMetaphone */
STDMETHODIMP DoubleMetaphoneString::ComputeMetaphoneKeys(/*[in]*/ BSTR Word, /*[out]*/ BSTR* PrimaryKey, /*[out]*/ BSTR* AlternateKey) {
	//Simply pass the call along to a DoubleMetaphone instance
	DoubleMetaphone<METAPHONE_KEY_LENGTH> mphone(Word);
	
	*PrimaryKey = ::SysAllocString(mphone.getPrimaryKey());
	if (mphone.getAlternateKey()) {
		*AlternateKey = ::SysAllocString(mphone.getAlternateKey());
	} else {
		*AlternateKey = NULL;
	}
	
	return S_OK;
}

/** Compares the metaphone keys from two words, 1 and 2, testing all 4 possible comparisons for a match.
 * Returns TRUE if any pair of keys from word 1 and word 2 match, else false */
STDMETHODIMP DoubleMetaphoneString::CompareMetaphoneKeys(/*[in]*/ BSTR PrimaryKey1, /*[in]*/ BSTR AlternateKey1, /*[in]*/ BSTR PrimaryKey2, /*[in]*/ BSTR AlternateKey2, /*[out, retval]*/ VARIANT_BOOL* Result) {
	if ( 
			(wcscmp(PrimaryKey1, PrimaryKey2)==0) ||
			(AlternateKey2 != NULL && wcscmp(PrimaryKey1, AlternateKey2)==0) ||
			(AlternateKey1 != NULL && wcscmp(AlternateKey1, PrimaryKey2)==0) ||
			(AlternateKey1 != NULL && AlternateKey2 != NULL && wcscmp(AlternateKey1, AlternateKey2)==0)
		) {
		*Result = VARIANT_TRUE;
	} else {
		*Result = VARIANT_FALSE;
	}
	
	return S_OK;
}

/** ComputeMetaphoneKeys, but using only IDispatch-compatible types for use with VBScript/JScript/etc */
STDMETHODIMP DoubleMetaphoneString::ComputeMetaphoneKeysScr(/*[in]*/ BSTR Word, /*[out]*/ VARIANT* PrimaryKey, /*[out]*/ VARIANT* AlternateKey) {
	BSTR prime = NULL, alt = NULL;
	
	ComputeMetaphoneKeys(Word, &prime, &alt);
	
	::VariantInit(PrimaryKey);
	::VariantInit(AlternateKey);
	
	V_VT(PrimaryKey) = VT_BSTR;
	PrimaryKey->bstrVal = prime;
	
	if (alt != NULL) {
		V_VT(AlternateKey) = VT_BSTR;
		AlternateKey->bstrVal = alt;
	} else {
		V_VT(AlternateKey) = VT_NULL;
	}
	
	return S_OK;
}


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)

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
Web04 | 2.8.140821.2 | Last Updated 19 Mar 2007
Article Copyright 2003 by Adam Nelson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid