Find a needle in a haystack based on string similarity and regular expression rules.
Replaces loose_tight_dictionary
because that was a confusing name.
>> require 'fuzzy_match'
=> true
>> matcher = FuzzyMatch.new(['seamus', 'andy', 'ben'])
=> #<FuzzyMatch: [...]>
>> matcher.find('Shamus')
=> "seamus"
At the core, and even if you configure nothing else, string similarity (calculated by "pair distance" aka Dice's) is used to compare records.
You can tell FuzzyMatch
what field or method to use via the :read
option... for example, let's say you want to match a Country
object like #<Country name:"Uruguay" iso_3166_code:"UY">
>> matcher = FuzzyMatch.new(Country.all, :read => :name) # Country#name will be called when comparing
=> #<FuzzyMatch: [...]>
>> matcher.find('youruguay')
=> #<Country name:"Uruguay" iso_3166_code:"UY"> # the matcher returns a Country object
You can improve the default matchings with rules. There are 4 different kinds of rules. Each rule is a regular expression. Depending on the kind of rule, the results of running the regular expression are used for a particular purpose.
We suggest that you first try without any rules and only define them to improve matching, prevent false positives, etc.
>> matcher = FuzzyMatch.new(['Ford F-150', 'Ford F-250', 'GMC 1500', 'GMC 2500'], :blockings => [ /ford/i, /gmc/i ], :normalizers => [ /K(\d500)/i ], :identities => [ /(f)-?(\d50)/i ])
=> #<FuzzyMatch: [...]>
>> matcher.find('fordf250')
=> "Ford F-250"
>> matcher.find('gmc truck k1500')
=> "GMC 1500"
For identities and normalizers (see below), only the captures are used. For example, /(f)-?(\d50)/i
captures the "F" and the "250" but ignores the dash. So place your parentheses carefully! Blockings work the same way, except that if you don't have any captures, a simple match will pass.
Group records together.
Setting a blocking of /Airbus/
ensures that strings containing "Airbus" will only be scored against to other strings containing "Airbus". A better blocking in this case would probably be /airbus/i
.
Strip strings down to the essentials.
Adding a normalizer like /(boeing).*(7\d\d)/i
will cause "BOEING COMPANY 747" and "boeing747" to be normalized to "BOEING 747" and "boeing 747", respectively. Since things are generally downcased before they are compared, these would be an exact match.
Prevent impossible matches.
Adding an identity like /(f)-?(\d50)/i
ensures that "Ford F-150" and "Ford F-250" never match.
Ignore common and/or meaningless words.
Adding a stop word like THE
ensures that it is not taken into account when comparing "THE CAT", "THE DAT", and "THE CATT"
read
: how to interpret each record in the 'haystack', either a Proc or a symbolmust_match_blocking
: don't return a match unless the needle fits into one of the blockings you specifiedmust_match_at_least_one_word
: don't return a match unless the needle shares at least one word with the matchfirst_blocking_decides
: force records into the first blocking they match, rather than choosing a blocking that will give them a higher scoregather_last_result
: enablelast_result
String similarity is case-insensitive. Everything is downcased before scoring. This is a change from previous versions.
Be careful when trying to use case-sensitivity in your rules; in general, things are downcased before comparing.
The algorithm is Dice's Coefficient (aka Pair Distance) because it seemed to work better than Longest Substring, Hamming, Jaro Winkler, Levenshtein (although see edge case below) etc.
Here's a great explanation copied from the wikipedia entry:
to calculate the similarity between:
night
nacht
We would find the set of bigrams in each word:
{ni,ig,gh,ht}
{na,ac,ch,ht}
Each set has four elements, and the intersection of these two sets has only one element: ht.
Inserting these numbers into the formula, we calculate, s = (2 · 1) / (4 + 4) = 0.25.
In edge cases where Dice's finds that two strings are equally similar to a third string, then Levenshtein distance is used. For example, pair distance considers "RATZ" and "CATZ" to be equally similar to "RITZ" so we invoke Levenshtein.
>> 'RITZ'.pair_distance_similar 'RATZ'
=> 0.3333333333333333
>> 'RITZ'.pair_distance_similar 'CATZ'
=> 0.3333333333333333 # pair distance can't tell the difference, so we fall back to levenshtein...
>> 'RITZ'.levenshtein_similar 'RATZ'
=> 0.75
>> 'RITZ'.levenshtein_similar 'CATZ'
=> 0.5 # which properly shows that RATZ should win
Over 2 years in Brighter Planet's environmental impact API and reference data service.
We often combine fuzzy_match
with remote_table
and errata
:
- download table with
remote_table
- correct serious or repeated errors with
errata
fuzzy_match
the rest
The admittedly imperfect metaphor is "look for a needle in a haystack"
- needle: the search term
- haystack: the records you are searching (your result will be an object from here)
If you add the amatch
gem to your Gemfile, it will use that, which is much faster (but segfaults have been seen in the wild). Thanks Flori!
Otherwise, pure ruby versions of the string similarity algorithms derived from the answer to a StackOverflow question and the text gem are used. Thanks marzagao and threedaymonk!
- Seamus Abshere [email protected]
- Ian Hough [email protected]
- Andy Rossmeissl [email protected]
Copyright 2012 Brighter Planet, Inc.