SequenceMatcher is a class in Python available in the difflib module, which provides functions for comparing sequences in two different pieces of text. So whenever you want to compare two text files, you can explore the difflib module in Python. If you have never used the SequenceMatcher class in Python, this article is for you. In this article, I will take you through a tutorial on SequenceMatcher in Python.
SequenceMatcher in Python
The SequenceMatcher class is available in the difflib module in Python, which is available in the Python standard library. You do not have to install it before using it. There are many classes in the difflib module to compare texts. One of those classes is SequenceMatcher which calculates how well the sequence of two texts matches each other. In simple words, it finds similarities in the sequence of two different texts.
Let’s see how to use this class to find similarities in the sequence of two texts. I will first input two very similar texts into this class:
from difflib import SequenceMatcher text1 = "My Name is Aman Kharwal" text2 = "Hi, My Name is Aman Kharwal" sequenceScore = SequenceMatcher(None, text1, text2).ratio() print(f"Both are {sequenceScore * 100} % similar")
Both are 92.0 % similar
So, according to the score above, it shows that both the text inputs have very similar sequences. Now let’s try it with text inputs that are dissimilar from each other:
text1 = "My Name is Aman Kharwal" text2 = "I am the founder of thecleverprogrammer.com" sequenceScore = SequenceMatcher(None, text1, text2).ratio() print(f"Both are {sequenceScore * 100} % similar")
Both are 24.242424242424242 % similar
So, according to the score above, it shows that both the text inputs have less similar sequences. This is how you can use this class in Python available in the difflib module.
Summary
The SequenceMatcher class is available in the difflib module in Python, which is available in the Python standard library. You do not have to install it before using it. I hope you liked this article on a tutorial on SequenceMatcher in Python. Feel free to ask valuable questions in the comments section below.