Skip to content

Jaro winkler #2041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 30, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions strings/jaro_winkler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import math


def jaro_winkler(first_string: str, second_string: str) -> float:
def jaro_winkler(str1: str, str2: str) -> float:
"""
Jaro–Winkler distance is a string metric measuring an edit distance between two sequences.
Output value is between 0.0 and 1.0.
Expand All @@ -11,7 +11,7 @@ def jaro_winkler(first_string: str, second_string: str) -> float:
0.9611111111111111

>>> jaro_winkler("CRATE", "TRACE")
0.6222222222222222
0.7333333333333334

>>> jaro_winkler("test", "dbdbdbdb")
0.0
Expand All @@ -20,36 +20,50 @@ def jaro_winkler(first_string: str, second_string: str) -> float:
1.0

>>> jaro_winkler("hello world", "HeLLo W0rlD")
0.5303030303030303
0.6363636363636364

>>> jaro_winkler("test", "")
0.0

>>> jaro_winkler("hello", "world")
0.4666666666666666

>>> jaro_winkler("hell**o", "*world")
0.4365079365079365
"""
max_distance = math.floor(max(len(first_string), len(second_string)) / 2) - 1

def get_matched_characters(_str1, _str2):
matched = []
limit = math.floor(min(len(_str1), len(_str2)) / 2)
for i, l in enumerate(_str1):
left, right = int(max(0, i - limit)), int(min(i + limit + 1, len(_str2)))
if l in _str2[left:right]:
matched.append(l)
_str2 = _str2[0:_str2.index(l)] + " " + _str2[_str2.index(l) + 1:]

return ''.join(matched)

# matching characters
match_count = 0
for i, c1 in enumerate(first_string):
for j, c2 in enumerate(second_string):
if c1 == c2 and abs(i - j) < max_distance:
match_count += 1
matching_1 = get_matched_characters(str1, str2)
matching_2 = get_matched_characters(str2, str1)
match_count = len(matching_1)

# transposition
not_match = len(
[(c1, c2) for c1, c2 in zip(first_string, second_string) if c1 != c2]
)
transpositions = math.floor(len(
[(c1, c2) for c1, c2 in zip(matching_1, matching_2) if c1 != c2]
)) / 2

if not match_count:
jaro = 0.0
else:
jaro = 1 / 3 * (
match_count / len(first_string)
+ match_count / len(second_string)
+ (match_count - not_match / 2) / match_count)
match_count / len(str1)
+ match_count / len(str2)
+ (match_count - transpositions) / match_count)

# common prefix up to 4 characters
prefix_len = 0
for c1, c2 in zip(first_string[:4], second_string[:4]):
for c1, c2 in zip(str1[:4], str2[:4]):
if c1 == c2:
prefix_len += 1
else:
Expand Down