FuzzyWuzzy Python library

In this article, we see the FuzzyWuzzy library. The name of this library something weird and funny, but it is advantageous. It has a unique way to compare both strings and returns the score out of 100 of how much string is matched. 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. 

There are many methods of comparing strings in python. Some of the main methods are:

  1. Using regex
  2. Simple compare
  3. Using difflib
To work with this library, we need to install it in our Python environment.
pip install fuzzywuzzy
pip install python-Levenshtein 

First, understand the following methods of fuzzywuzzy library:

Import these modules:-
from fuzzywuzzy import fuzz 
from fuzzywuzzy import process 

Fuzz Module:  

The fuzz module is used to compare the two given strings at a time. It returns a score out of 100 after comparison using the different methods.  

Fuzz.ratio():

It is one of the important methods of the fuzz module. It compares the string and score on the basis of how much the given string is matched.

Fuzz.partial_ratio():

The fuzzywuzzy library provides another powerful method partial_ratio(). It is used to handle the complex sting comparison such as substring matching. The partial_ratio() method can detect the substring. It follows the optimal partial logic where the short length string k and longer string m, the algorithm finds the best matching length k-string.

Fuzz.token_sort_ratio: 

This method does not guarantee to get an accurate result because if we make the changes in the order of string. It may not give an accurate result.

Fuzz.token_set_ratio():

We used another method fuzz.token_set_ratio() that performs a set operation and takes out the common token and then makes a ratio() pairwise comparison.


Example:

# Importing the module from the fuzzywuzzy library 
from fuzzywuzzy import fuzz  
# Strings 
str1 = "Welcome to Python programming"  
str2 = "python"  
#  Exact match using ratio()
Ratio = fuzz.ratio(str1.lower(),str2.lower())  
# Partial match using partial_ratio()
Ratio_partial = fuzz.partial_ratio(str1.lower(),str2.lower())
# Token Sort Ratio
Ratio_Token = fuzz.token_sort_ratio(str1,str2)     
print(Ratio)  
print(Ratio_partial)  
print(Ratio_Token)



Process Module: 

The fuzzywuzzy package provides the processing module that allows us to calculate the string with the highest similarity.

Fuzz.WRatio:

The processing module also provides the WRatio, which gives a better result than the simple ratio. It handles lower and upper cases and some other parameters too.

Example:

from fuzzywuzzy import fuzz   
from fuzzywuzzy import process   
print(fuzz.WRatio('good morning', 'Good Morning')) 
print(fuzz.WRatio('good morning!!!','good Morning')) 



Example:

from fuzzywuzzy import process  
strToMatch = "Hello Good Morning"  
givenOpt = ["hello","Hello Good","Morning","Good Evenining"]  
ratios = process.extract(strToMatch,givenOpt)  
print(ratios)  
# We can choose the string that has highest matching percentage  
high = process.extractOne(strToMatch,givenOpt)  
print(high)





Thank you 😊 for reading. Please read other blogs. And also share with your friends and 
family.


 ꜰᴏʀ ᴇxᴘʟᴏʀɪɴɢ ᴛʜᴇ ᴡᴏʀʟᴅ ᴘʟᴇᴀꜱᴇ ʜᴀᴠᴇ ʟᴏᴏᴋ ᴀɴᴅ ꜰᴏʟʟᴏᴡ.
https://maps.app.goo.gl/jnKyzdDpKMFutUqR7


ʟᴇᴛ ᴍᴇ ᴋɴᴏᴡ ɪꜰ ʏᴏᴜ ʜᴀᴠᴇ ᴀɴʏ Qᴜᴇʀɪᴇꜱ ᴏʀ Qᴜᴇꜱᴛɪᴏɴꜱ.
pratikshagarkar871999@gmail.com 
             

 




 



 









 



Comments

Popular posts from this blog

How to convert PDF file into audio file?

Pillow Libary in Python.

How to perform operations on emails and folders using imap_tools?