Created
August 30, 2021 17:11
-
-
Save rgiaviti/22bbd04f78383b4679eafc4d9d0dae49 to your computer and use it in GitHub Desktop.
get string similarity in Python with SequenceMatcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from difflib import SequenceMatcher | |
def similarity(string1: str, string2: str) -> float: | |
return SequenceMatcher(a=string1, b=string2).ratio() | |
def similarity_in_percent(string1: str, string2: str) -> float: | |
value = similarity(string1, string2) * 100 | |
return round(value, 2) | |
print(similarity("João", "João")) # > 1.0 String Iguais | |
print(similarity("João", "JOÃO")) # > 0.25 Maiúsculas e Minúsculas contam | |
print(similarity("João", "Pedro")) # > 0.2222222222222222 | |
# Convertendo em percentual e cortando as casas decimais | |
print(similarity_in_percent("João", "João")) # > 100.0% Iguais | |
print(similarity_in_percent("João", "JOÃO")) # > 25% Iguais | |
print(similarity_in_percent("João", "Pedro")) # > 22.22% Iguais |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment