Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C++

Implement Phonetic ("Sounds-like") Name Searches with Double Metaphone Part VI: Other Methods & Additional Resources

Rate me:
Please Sign up or sign in to vote.
5.00/5 (27 votes)
19 Mar 20079 min read 253.1K   5.5K   71  
Surveys other phonetic matching techniques, and presents additional resources on the subject.
/**
 * 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 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


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