-
Notifications
You must be signed in to change notification settings - Fork 0
/
jaro.py
65 lines (50 loc) · 1.46 KB
/
jaro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from math import floor, ceil
def jaro_distance(s1, s2):
# If the s are equal
if (s1 == s2):
return 1.0
# Length of two s
len1 = len(s1)
len2 = len(s2)
# Maximum distance upto which matching
# is allowed
max_dist = floor(max(len1, len2) / 2) - 1
# Count of matches
match = 0
# Hash for matches
hash_s1 = [0] * len(s1)
hash_s2 = [0] * len(s2)
# Traverse through the first
for i in range(len1):
# Check if there is any matches
for j in range(max(0, i - max_dist),
min(len2, i + max_dist + 1)):
# If there is a match
if (s1[i] == s2[j] and hash_s2[j] == 0):
hash_s1[i] = 1
hash_s2[j] = 1
match += 1
break
# If there is no match
if (match == 0):
return 0.0
# Number of transpositions
t = 0
point = 0
# Count number of occurrences
# where two characters match but
# there is a third matched character
# in between the indices
for i in range(len1):
if (hash_s1[i]):
# Find the next matched character
# in second
while (hash_s2[point] == 0):
point += 1
if (s1[i] != s2[point]):
t += 1
point += 1
t = t//2
# Return the Jaro Similarity
return (match/ len1 + match / len2 +
(match - t) / match)/ 3.0