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
Install via pip :
pip install fuzzywuzzy pip install python-Levenshtein
How to use this library ?
First of import these modules,
Simple ratio usage :
Now, token set ratio an token sort ratio:
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
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.
FuzzyWuzzy Ratio: 84 FuzzyWuzzy PartialRatio: 85 FuzzyWuzzy TokenSortRatio: 84 FuzzyWuzzy TokenSetRatio: 86 FuzzyWuzzy WRatio: 84 List of ratios: [('g. for geeks', 95), ('geek for geek', 93), ('geek geek', 86)] Best among the above list: ('g. for geeks', 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.
- Python Faker Library
- Python | Holidays library
- Python | Schedule Library
- Python Pyforest Library
- Python Numerizer Library
- Python math library | exp() method
- How to install Librosa Library in Python
- Python math library | gamma() function
- Python math library | isclose() method
- Python math library | isnan() method
- Python math library | expm1() method
- Introduction to pyglet library for game development in Python
- Pytube | Python library to download youtube videos
- Python math library | isfinite() and remainder() method
- Learning Model Building in Scikit-learn : A Python Machine Learning Library
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.