I have a problem finding similar names of food in my database (there are about 100k products names). I've decided to use fuzz.token_sort_ratio from lib fuzzywuzzy to find similar product names. This is how it is works:
s1 = 'Pepsi Light'
s2 = 'Light Pepsi'
fuzz.token_sort_ratio(s1, s2)
100
Now I want to find all the names of products with similar words, which have the result of fuzz.token_sort_ratio >= 90
Here my code:
#Find similar
start=datetime.now()
l = list(v_foods.name[0:20000])
i=0
df = pd.DataFrame(columns=['name1', 'name2', 'probab_same'])
for k in range(len(l)):
for s in range(k+1,len(l)):
probability = fuzz.token_sort_ratio(l[k], l[s])
if probability >= 90:
df.loc[i] = [l[k], l[s], probability]
i +=1
print('Spent time: {}' .format(datetime.now() - start))
df.head(5)
It takes a lot of time. The more products I have, the more time it takes
l = list(v_foods.name[0:5000])Spent time: ~3 minutesl = list(v_foods.name[0:10000])Spent time: ~13 minutesl = list(v_foods.name[0:20000])Spent time: ~53 minutes
As I said above, my base has 100k names and it will work very slow. Are there any methods to optimize my algorithm?