DevWebPro Newsletter:


Get Your Site Submitted for Free in the World's Largest B2B Directory!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

Coldfusion: Flexible String Comparisons

By Raymond Camden
Expert Author
Article Date: 2008-01-04

Martin asks a pretty interesting question about comparing strings:

Is there a way to compare the similarities of variable strings in ColdFusion?

For instance, lets say variable.foo = "15 St Mungo Place" and variable.coo = "15 St. Mungo Place"

Sql will not return a match but you and I both know that these two strings mean the same thing AND that there is only a one character difference.

My question is - Is there a way to get Coldfusion to recognise that there is only a one character difference (similarities) between the two strings.

Obviously the potential applications for search queries etc.. could be enormous here so I'm VERY curious to see if this is possible.


Right off the bat, there is nothing baked into ColdFusion that will do this automatically, so we have to look at other possible solutions.

For some cases, you could use straight substitutions.

You could have a list of common alternative spellings ("st. for st") and loop over them and do replacements.

That will work for simple things but probably isn't going to scale up.

You could also compare strings and see how close they are. I did some Googling on that, and kept coming across the Levenshtein distance formula.

I was actually working to rebuild the pseudo-code on Wikipedia when I remembered CFLib - turns out it has a udf, levDistance, that can use this logic.

This is still somewhat of a slow process as you would need to compare your search against all the words in the database.

This is probably a case where something like Verity, or the full text searching feature of your database, may be more handy.

Anyone done anything like this in ColdFusion?

Comments

About the Author:
Raymond Camden, ray@camdenfamily.com
http://ray.camdenfamily.com

Raymond Camden is Vice President of Technology for roundpeg, Inc. A long time ColdFusion user, Raymond has worked on numerous ColdFusion books and is the creator of many of the most popular ColdFusion community web sites. He is an Adobe Community Expert, user group manager, and the proud father of three little bundles of joy.


Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact | Site Map

Dev Web Pro is an iEntry, Inc.® publication - 1998-2008 All Rights Reserved Privacy Policy and Legal