Fuzzy matching with python
Fuzzy matching is great. In fact, it is better than regular expression matching in certain cases. Come to think of it, our entire human brain works on fuzzy matching.
Take handwriting, for example, everyone can read each other’s handwriting as long as it’s legible. That’s fuzzy matching on an organic level.
I had a problem where I have a string of text, and I wanted to match the substring with other string but the issue is that the substring varies from string to string and I cant predict the proper substring to do a regex matching. So suddenly out of the blue I got an idea to do a fuzzy matching.
So I asked ChatGPT like I usually do these days and seems that Python has a built in library for fuzzy matching, this is how to use it.
from difflib import SequenceMatcher
string1 = "apple pie"
string2 = "apple pi"
# Create a SequenceMatcher object
seq_matcher = SequenceMatcher(None, string1, string2)
# Get the ratio of similarity between the two strings
similarity_ratio = seq_matcher.ratio()
print(similarity_ratio)
Now I have another weapon in my arsenal to defeat my enemies.