SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Roll Your Own Fuzzy Match / Grouping (Jaro Winkler) - T-SQL


Roll Your Own Fuzzy Match / Grouping (Jaro Winkler) - T-SQL

Author
Message
Ira Warren Whiteside
Ira Warren Whiteside
SSC-Addicted
SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)

Group: General Forum Members
Points: 468 Visits: 296
Comments posted to this topic are about the item Roll Your Own Fuzzy Match / Grouping (Jaro Winkler) - T-SQL

Ira Warren Whiteside
ezahurak
ezahurak
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 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
Ira Warren Whiteside
Ira Warren Whiteside
SSC-Addicted
SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)

Group: General Forum Members
Points: 468 Visits: 296
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
ezahurak
ezahurak
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 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. Smile
pallu
pallu
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 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 :-)



rawliquid
rawliquid
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 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'
Nadrek
Nadrek
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4416 Visits: 2741
[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.
Ira Warren Whiteside
Ira Warren Whiteside
SSC-Addicted
SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)

Group: General Forum Members
Points: 468 Visits: 296
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
Ira Warren Whiteside
Ira Warren Whiteside
SSC-Addicted
SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)

Group: General Forum Members
Points: 468 Visits: 296
Thanks for the suggestions, I try them out.

Ira Warren Whiteside
pallu
pallu
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 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. Smile



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search