@@ -34,25 +34,30 @@ def get_matched_characters(_str1: str, _str2: str) -> str:
34
34
matched .append (l )
35
35
_str2 = f"{ _str2 [0 :_str2 .index (l )]} { _str2 [_str2 .index (l ) + 1 :]} "
36
36
37
- return '' .join (matched )
37
+ return "" .join (matched )
38
38
39
39
# matching characters
40
40
matching_1 = get_matched_characters (str1 , str2 )
41
41
matching_2 = get_matched_characters (str2 , str1 )
42
42
match_count = len (matching_1 )
43
43
44
44
# transposition
45
- transpositions = len (
46
- [(c1 , c2 ) for c1 , c2 in zip (matching_1 , matching_2 ) if c1 != c2 ]
47
- ) // 2
45
+ transpositions = (
46
+ len ( [(c1 , c2 ) for c1 , c2 in zip (matching_1 , matching_2 ) if c1 != c2 ]) // 2
47
+ )
48
48
49
49
if not match_count :
50
50
jaro = 0.0
51
51
else :
52
- jaro = 1 / 3 * (
53
- match_count / len (str1 )
54
- + match_count / len (str2 )
55
- + (match_count - transpositions ) / match_count )
52
+ jaro = (
53
+ 1
54
+ / 3
55
+ * (
56
+ match_count / len (str1 )
57
+ + match_count / len (str2 )
58
+ + (match_count - transpositions ) / match_count
59
+ )
60
+ )
56
61
57
62
# common prefix up to 4 characters
58
63
prefix_len = 0
@@ -65,7 +70,8 @@ def get_matched_characters(_str1: str, _str2: str) -> str:
65
70
return jaro + 0.1 * prefix_len * (1 - jaro )
66
71
67
72
68
- if __name__ == ' __main__' :
73
+ if __name__ == " __main__" :
69
74
import doctest
75
+
70
76
doctest .testmod ()
71
77
print (jaro_winkler ("hello" , "world" ))
0 commit comments