There are many methods of comparing string in python. Some of the main methods are:
- Using regex
- Simple compare
- Using difflib
But one of the very easy method is by using fuzzywuzzy library where we can have a score out of 100, that denotes two string are equal by giving similarity index. This article talks about how we start using fuzzywuzzy library.
FuzzyWuzzy is a library of Python which is used for string matching. Fuzzy string matching is the process of finding strings that match a given pattern. Basically it uses Levenshtein Distance to calculate the differences between sequences.
FuzzyWuzzy has been developed and open-sourced by SeatGeek, a service to find sport and concert tickets. Their original use case, as discussed in their blog.
- Requirements of fuzzywuzzy
- Python 2.4 or higher
- python-Levenshtein
Install via pip :
pip install fuzzywuzzy pip install python-Levenshtein
How to use this library ?
First of import these modules,
from fuzzywuzzy import fuzz from fuzzywuzzy import process |
Simple ratio usage :
fuzz.ratio( 'neveropen' , 'LazyroarLazyroar' ) 87 # Exact match fuzz.ratio( 'Lazyroar' , 'Lazyroar' ) 100 fuzz.ratio( 'Lazyroar for Lazyroar' , 'Geeks For Geeks ' ) 80 |
fuzz.partial_ratio( "Lazyroar for Lazyroar" , "Lazyroar for Lazyroar!" ) 100 # Exclamation mark in second string, but still partially words are same so score comes 100 fuzz.partial_ratio( "Lazyroar for Lazyroar" , "Lazyroar Lazyroar" ) 64 # score is less because there is a extra token in the middle middle of the string. |
Now, token set ratio an token sort ratio:
# Token Sort Ratio fuzz.token_sort_ratio( "Lazyroar for Lazyroar" , "for Lazyroar Lazyroar" ) 100 # This gives 100 as every word is same, irrespective of the position # Token Set Ratio fuzz.token_sort_ratio( "Lazyroar for Lazyroar" , "Lazyroar for for Lazyroar" ) 88 fuzz.token_set_ratio( "Lazyroar for Lazyroar" , "Lazyroar for for Lazyroar" ) 100 # Score comes 100 in second case because token_set_ratio considers duplicate words as a single word. |
Now suppose if we have list of list of options and we want to find the closest match(es), we can use the process module
query = 'Lazyroar for Lazyroar' choices = [ 'geek for geek' , 'geek geek' , 'g. for Lazyroar' ] # Get a list of matches ordered by score, default limit to 5 process.extract(query, choices) [( 'Lazyroar Lazyroar' , 95 ), ( 'g. for Lazyroar' , 95 ), ( 'geek for geek' , 93 )] # If we want only the top one process.extractOne(query, choices) ( 'Lazyroar Lazyroar' , 95 ) |
There is also one more ratio which is used often called WRatio, sometimes its better to use WRatio instead of simple ratio as WRatio handles lower and upper cases and some other parameters too.
fuzz.WRatio( 'Lazyroar for Lazyroar' , 'Geeks For Geeks' ) 100 fuzz.WRatio( 'Lazyroar for Lazyroar!!!' , 'Lazyroar for Lazyroar' ) 100 # whereas simple ratio will give for above case fuzz.ratio( 'Lazyroar for Lazyroar!!!' , 'Lazyroar for Lazyroar' ) 91 |
Full Code
# Python code showing all the ratios together, # make sure you have installed fuzzywuzzy module from fuzzywuzzy import fuzz from fuzzywuzzy import process s1 = "I love Lazyroar" s2 = "I am loving Lazyroar" print "FuzzyWuzzy Ratio: " , fuzz.ratio(s1, s2) print "FuzzyWuzzy PartialRatio: " , fuzz.partial_ratio(s1, s2) print "FuzzyWuzzy TokenSortRatio: " , fuzz.token_sort_ratio(s1, s2) print "FuzzyWuzzy TokenSetRatio: " , fuzz.token_set_ratio(s1, s2) print "FuzzyWuzzy WRatio: " , fuzz.WRatio(s1, s2), '\n\n' # for process library, query = 'Lazyroar for Lazyroar' choices = [ 'geek for geek' , 'geek geek' , 'g. for Lazyroar' ] print "List of ratios: " print process.extract(query, choices), '\n' print "Best among the above list: " ,process.extractOne(query, choices) |
Output:
FuzzyWuzzy Ratio: 84 FuzzyWuzzy PartialRatio: 85 FuzzyWuzzy TokenSortRatio: 84 FuzzyWuzzy TokenSetRatio: 86 FuzzyWuzzy WRatio: 84 List of ratios: [('g. for Lazyroar', 95), ('geek for geek', 93), ('geek geek', 86)] Best among the above list: ('g. for Lazyroar', 95)
The FuzzyWuzzy library is built on top of difflib library, python-Levenshtein is used for speed. So it is one of the best way for string matching in python.