'How to find similarity between two strings with function using python
These are two strings
string1 = 'SUTURE SILK 30 INCHES(75CM) 3-0 BLK'
string2 = 'SUTURE NON-ABSORBABLE PERMA-HAND SIZE 3-0 L'
I want output like
0.801452
how I can do this by using function
Solution 1:[1]
Considering that you want to get the Levenshtein Distance, without any aditional dependency you can do something like this:
def levenshteinDistance(s1, s2):
if len(s1) > len(s2):
s1, s2 = s2, s1
distances = range(len(s1) + 1)
for i2, c2 in enumerate(s2):
dist = [i2+1]
for i1, c1 in enumerate(s1):
if c1 == c2:
dist.append(distances[i1])
else:
dist.append(1 + min((distances[i1], distances[i1 + 1], dist[-1])))
distances = dist
return distances[-1]
You can get more informations about this distance on wikipedia.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | George Victor |
