Click here to Skip to main content
15,879,022 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 147.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.
/**
 * DoubleMetaphoneShort.cpp
 * 
 * Implementation of the Double Metaphone phonetic matching algorithm, wrapped as a dual-interface
 * COM component callable from Visual Basic and scripting clients.  This implementation exposes
 * the metaphone keys as shorts instead of strings, for even more efficient storage and retrieval.
 * 
 * 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/
 * 
 * Current Version: 1.0.0
 * Revision History:
 * 	1.0.0 - ajn - First release
 * 
 * Implementation by Adam J. Nelson (anelson@nullpointer.net)
 * Copyright (C) 2003 Adam J. Nelson, All Rights Reserved
 * 
 * The Double Metaphone algorithm was written by Lawrence Phillips, and is 
 * Copyright (c) 1998, 1999 by Lawrence Philips.
 **/

#include "stdafx.h"
#include "DoubleMetaphoneShort.h"

STDMETHODIMP DoubleMetaphoneShort::ComputeMetaphoneKeys(/*[in]*/ BSTR Word, /*[out]*/ short* PrimaryKey, /*[out]*/ short* AlternateKey) {
	//Simply pass the call along to a ShortDoubleMetaphone instance
	ShortDoubleMetaphone mphone(Word);
	
	*PrimaryKey = mphone.getPrimaryShortKey();
	if (mphone.getAlternateKey()) {
		*AlternateKey = mphone.getAlternateShortKey();
	} else {
		*AlternateKey = static_cast<short>(METAPHONE_INVALID_KEY);
	}
	
	return S_OK;
}

STDMETHODIMP DoubleMetaphoneShort::CompareMetaphoneKeys(/*[in]*/ short PrimaryKey1, /*[in]*/ short AlternateKey1, /*[in]*/ short PrimaryKey2, /*[in]*/ short AlternateKey2, /*[out, retval]*/ VARIANT_BOOL* Result) {
	if ( 
			(PrimaryKey1 == PrimaryKey2) ||
			(AlternateKey2 != METAPHONE_INVALID_KEY && PrimaryKey1 == AlternateKey2) ||
			(AlternateKey1 != METAPHONE_INVALID_KEY && AlternateKey1 == PrimaryKey2) ||
			(AlternateKey1 != METAPHONE_INVALID_KEY && AlternateKey2 != METAPHONE_INVALID_KEY && AlternateKey1 == AlternateKey2)
		) {
		*Result = VARIANT_TRUE;
	} else {
		*Result = VARIANT_FALSE;
	}
	
	return S_OK;
} 

/** ComputeMetaphoneKeys, but using only IDispatch-compatible types for use with VBScript/JScript/etc */
STDMETHODIMP DoubleMetaphoneShort::ComputeMetaphoneKeysScr(/*[in]*/ BSTR Word, /*[out]*/ VARIANT* PrimaryKey, /*[out]*/ VARIANT* AlternateKey) {
	short prime, alt;
	
	ComputeMetaphoneKeys(Word, &prime, &alt);
	
	::VariantInit(PrimaryKey);
	::VariantInit(AlternateKey);
	
	V_VT(PrimaryKey) = VT_I2;
	PrimaryKey->iVal = prime;
	
	if (alt != METAPHONE_INVALID_KEY) {
		V_VT(AlternateKey) = VT_I2;
		AlternateKey->iVal = 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)


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