I got a function that has as input 2 strings, computes the string similarity of all the combinations and give back and output the highest similarity. For example, "you are" and "are you" will have 1 as similarity value.
import itertools
from difflib import SequenceMatcher
import numpy as np
def _compare_names(a, b):
return SequenceMatcher(None, a, b).ratio()
def _compare_names_with_combinations(name_1,name_2):
name_1 = name_1.lower().split()
name_2 = name_2.lower().split()
combinations_name_1 = list(itertools.permutations(name_1))
combinations_name_2 = list(itertools.permutations(name_2))
combinations_name_joined_1 = [' '.join(list(name)) for name in combinations_name_1]
combinations_name_joined_2 = [' '.join(list(name)) for name in combinations_name_2]
distances = []
for name1 in combinations_name_joined_1:
for name2 in combinations_name_joined_2:
distance = _compare_names(name1,name2)
distances.append(distance)
return max(distances)
examples:
_compare_names_with_combinations('you are','are you')
>> 1
_compare_names_with_combinations('you are','are yos')
>> 0.85714285
My concerns come when I have to compare a lot of texts and it seems that there should be around a more efficient way of computing this value. Do you think there is space in this function to decrease the computational time?
john smith
,billy bob thornton
.. . \$\endgroup\$_compare_names
look? Also, no need fordistances
, just store the max value. \$\endgroup\$