Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Roll Your Own Fuzzy Match / Grouping (Jaro Winkler) - T-SQL Expand / Collapse
Author
Message
Posted Tuesday, June 9, 2009 11:59 PM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, December 1, 2014 10:40 PM
Points: 42, Visits: 294
Comments posted to this topic are about the item Roll Your Own Fuzzy Match / Grouping (Jaro Winkler) - T-SQL

Ira Warren Whiteside
Post #732037
Posted Wednesday, June 10, 2009 7:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, June 10, 2009 9:51 AM
Points: 2, Visits: 3
Ira, interesting article. Timely, too, as I've been working on a project to create an index of people listed in multiple data sources, locating the same people in each database by means of matching on various fields.

I'm currently using a similar approach, but using a Levenshtien distance algoritm instead of Jaro-Winkler. Would Jaro-Winkler be more efficient?

Thanks,
Ed Z
Post #732249
Posted Wednesday, June 10, 2009 7:31 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, December 1, 2014 10:40 PM
Points: 42, Visits: 294
Yes, Jaro-Winkler will give more weight to the length of the string match from the beginning of the string. It should be more accurate the Levi, however the real challenge is to implement a blocking/partioning strategy to limits the num of records sent to Jaro.

Ira Warren Whiteside
Post #732266
Posted Wednesday, June 10, 2009 7:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, June 10, 2009 9:51 AM
Points: 2, Visits: 3
Thanks, I'll give Jaro-Winkler a try.

I've already a strategy in place to filter the number of rows through to the fuzzy match query, including pre-weeding out exact matches and matches that don't start with the same first letter as the match target. (In my experience here, we tend to at least get the first letter of the name right!) Those filters alone are enough to bring 2+ minute queries down to sub-second queries, which is good enough for me.

Now if I could just get my nickname-based matching working as fast, I'd be a truly happy camper. :)
Post #732295
Posted Wednesday, June 10, 2009 8:04 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, May 14, 2014 1:06 PM
Points: 30, Visits: 95
Good stuff Ira. I had been using the built in Fuzzy Matching Package, but there's nothing like being able to use a regular query.

A small thing - a slightly more accurate translation of the Hindi saying at the bottom is - Whether you do or do not do [somethin], there is nothing like trying.

Cheers



Post #732313
Posted Wednesday, June 10, 2009 8:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 14, 2012 9:51 AM
Points: 2, Visits: 5
There are a few misspellings in the posted content, in the jaro procedure the string cleansing function called is 'Clean_sting' yet below the function created is 'Cleansing'
Post #732354
Posted Wednesday, June 10, 2009 8:47 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 8:46 AM
Points: 893, Visits: 2,475
[Edited after an accidental early post]
First and foremost, thank you very much for writing Jaro-Winkler for T-SQL; that was an algorithm in a language I was looking for just a few weeks ago for name and address matching.

Second, thank you for taking the time to post a comprehensive article, including test cases, on a very important topic.

Third, I have a few suggestions for what I would consider improvements:
1) Descriptive variable names. @max_len, for instance, is much better than @m, @z, and so on.

2) Typo?
2a) You call [dbo].[clean_sting] and give source code to a function [dbo].[Cleansing]
2b) In [dbo].[Cleansing], there appears to be an extraneous ') just before the RETURN

3) Efficiency improvements
3a) In the cleansing function, instead of
            Declare @ret_value varchar(max)
set @ret_value = @p_str1
set @ret_value = REPLACE(@ret_value, '.', '')
set @ret_value = REPLACE(@ret_value, ',', '')
set @ret_value = REPLACE(@ret_value, '-', '')
set @ret_value = REPLACE(@ret_value, ';', '')
set @ret_value = REPLACE(@ret_value, ':', '') ')
RETURN @ret_value

I would suggest
            Declare @ret_value varchar(max)
set @ret_value = @p_str1
set @ret_value = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@ret_value, '.', ''), ',', ''), '-', '') , ';', ''), ':', '')
RETURN @ret_value

On a sample size of 98,304 rows (and a pretty pathetic SQL Server), CPU on SQL Server profiler dropped from 4422 to 3906 with this change (i.e. this uses only 88% the CPU time of the original).

4) Suggested higher cost, higher sanitization REPLACE() for dirtier strings in general when one wants to compare alphanumerics, presented in bite size pieces (combine the REPLACE() sets for higher efficiency:
-- HTML character entity references
-- An authoritative list for HTML 4.0 is at http://www.w3.org/TR/REC-html40/sgml/entities.html
SET @String = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@String, '&',' '), '&',' '),'',' '), ' ',' '),'"',' '), '"',' ')

-- (U.S. centric) Punctuation merely gets in the way of matching; replace with spaces to keep our word separation; we won't do a fully complete replacement, but we'll do what we do relatively quickly.
-- EXCEPT FOR THE # SIGN (which merely gets turned to ' # ', and is used for further address splitting, and is cleaned at the end of that), the bottom three rows of punctuation on the right hand side of a U.S. keyboard, with and without shift by row, then the top row's symbols the same way.
-- Further address splitting: pull the number off the front, then pull the Suite/Apartment/#/Unit/etc. off the end, leaving the middle by itself
SET @String = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@String, '.', ' '), ',', ' '), '/', ' '), '<', ' '), '>', ' '), '?', ' '), ';',' '), '''',' '), ':',' '), '"',' '), '[',' '), ']',' '), '\',' '), '{',' '), '}',' '), '|',' '), '`', ' '), '-', ' '), '=', ' '), '~', ' '), '!', ' '), '@', ' '), '#', ' # '), '$',' '), '%',' '), '^',' '), '&',' '), '*',' '), '(',' '), ')',' '), '_',' '), '+',' ')


-- (U.S. centric) Special characters merely gets in the way of matching, and some may still be in use in old systems and data, particularly mainframes and mainframe derived code. Here's a list of codes, from more common, to quite rare
-- NOTE: Be careful with UNICODE, though these are all from the first 7 bits of ASCII encoding
SET @String = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@String, CHAR(9),' '), CHAR(11),' '), CHAR(13),' '), CHAR(10),' '), CHAR(0),' '), CHAR(27),' '), CHAR(12),' '), CHAR(1),' '), CHAR(2),' '), CHAR(3),' '), CHAR(4),' '), CHAR(8),' '), CHAR(23),' '), CHAR(24),' '), CHAR(25),' '), CHAR(26),' '), CHAR(28),' '), CHAR(29),' '), CHAR(30),' '), CHAR(31),' ')

5) Suggested higher cost, higher sanitization address cleaning):

-- Get rid of any combination of leading 0's and spaces in a moderately performant way:
SET @StringToStripLeadingZerosSpacesFrom = COALESCE(SUBSTRING(@StringToStripLeadingZerosSpacesFrom,PATINDEX('%[^0 ]%',@StringToStripLeadingZerosSpacesFrom),DATALENGTH(@StringToStripLeadingZerosSpacesFrom)),'')

-- Directions can be changed to standard abbreviations
-- This must be done before punctuations is replaced by spaces.
SET @String = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@String, ' north-east ',' ne '),' northeast ',' ne '), ' north-west ',' nw '),' northwest ',' nw '), ' south-east ',' se '),' southeast ',' se '), ' south-west ',' sw '),' southwest ',' sw '), ' north ',' n '), ' east ',' e '), ' south ',' s '), ' west ',' w ')

6) Suggested higher cost, higher sanitization name cleaning for highly fuzzy matches, again, consolidate for higher efficiency:
-- Dental/Medical centric abbreviation cleaning
-- A select few sourced from http://www.tabers.com/tabersonline/ub/view/Tabers/144251/0/Professional_Designations_and_Titles_in_the_Health_Sciences)
-- Precondition: Punctuation replaced by spaces, and then all consecutive spaces consolidated into single spaces
SET @String = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@String, ' DDS ',' '),' D D S ',' '), ' DMD ',' '),' D M D ',' '), ' PC ',' '),' P C ',' '), ' MS ',' '),' M S ',' '),' PA ',' '),' P A ',' '),' FACD ',' '),' F A C D ',' '),' RDH ',' '),' R D H ',' '),' RDA ',' '),' R D A ',' '),' CDA ',' '),' C D A ',' '),' APC ',' '),' A P C ',' '),' MSD ',' '),' M S D ',' '),' INC ',' '),' I N C ',' '),' MD ',' '),' M D ',' '),' LTD ',' '),' L T D ',' ')


-- Now we get rid of common suffixes
-- This definitely is going to generate false positive matches on parents and children!
-- Use at a fairly fuzzy level of matching, after more precise matches have been handled
SET @String = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@String, ' I ',' '),' II ',' '), ' III ',' '),' IV ',' '), ' V ',' '),' JR ',' '), ' SR ',' ')


7) As always, for bulk use, generate and save the "clean" string separately in its own columns using REPLACE() on sets.

Fourth, nothing to do with this article, but I would suggest adding Double Metaphone to your list of fuzzy logic functions. It's computationally expensive, but can be done in bulk, and is really quite reasonable when it comes to matching strings that sound alike. I found source at http://www.sqlservercentral.com/scripts/Miscellaneous/30219/ and updated it to generate 35 character strings instead of 5 character strings for less fuzzy matching on longer names.
Post #732357
Posted Wednesday, June 10, 2009 9:24 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, December 1, 2014 10:40 PM
Points: 42, Visits: 294
Thank you, the translation came from some friends in New Delhi, what would be your translation for "Do, or do not. There is no try".

Ira Warren Whiteside
Post #732386
Posted Wednesday, June 10, 2009 9:25 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, December 1, 2014 10:40 PM
Points: 42, Visits: 294
Thanks for the suggestions, I try them out.

Ira Warren Whiteside
Post #732388
Posted Wednesday, June 10, 2009 9:40 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, May 14, 2014 1:06 PM
Points: 30, Visits: 95
Hi Ira,
The Hindi saying: "karo yaa na karo, koshish jaisa kuch nahi hai" literally translates to "Do or not do, there is nothing like try". However, the implied translation is "[Whether you] do or not do [something], there is nothing like try[ing]" with the implied portions in square brackets. That is because of the structure of the Hindi language.

Cheers. :)



Post #732409
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse