-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Jaro winkler #2041
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f52609d
Added jaro_winkler first version
l3str4nge ee741ad
Merge branch 'master' into jaro_winkler
l3str4nge bc32a9f
Added doctests
l3str4nge a07914f
Fix flake warnings
l3str4nge f5d1500
Refactor
l3str4nge c39c9de
Fixes bug in jaro winkler implementation
l3str4nge fb505bc
Commit suggestions
l3str4nge 23b0fac
Missing comming suggestions
l3str4nge d1f65b4
Remove unused math module
l3str4nge 8d8d68c
Import doctest
poyea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance """ | ||
import math | ||
|
||
|
||
def jaro_winkler(first_string: str, second_string: 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. | ||
|
||
>>> jaro_winkler("martha", "marhta") | ||
0.9611111111111111 | ||
|
||
>>> jaro_winkler("CRATE", "TRACE") | ||
0.6222222222222222 | ||
|
||
>>> jaro_winkler("test", "dbdbdbdb") | ||
0.0 | ||
|
||
>>> jaro_winkler("test", "test") | ||
1.0 | ||
|
||
>>> jaro_winkler("hello world", "HeLLo W0rlD") | ||
0.5303030303030303 | ||
|
||
>>> jaro_winkler("test", "") | ||
0.0 | ||
""" | ||
max_distance = math.floor(max(len(first_string), len(second_string)) / 2) - 1 | ||
|
||
# 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 | ||
|
||
# transposition | ||
not_match = len( | ||
[(c1, c2) for c1, c2 in zip(first_string, second_string) if c1 != c2] | ||
) | ||
|
||
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) | ||
|
||
# common prefix up to 4 characters | ||
prefix_len = 0 | ||
for c1, c2 in zip(first_string[:4], second_string[:4]): | ||
if c1 == c2: | ||
prefix_len += 1 | ||
else: | ||
break | ||
|
||
return jaro + 0.1 * prefix_len * (1 - jaro) | ||
|
||
|
||
if __name__ == '__main__': | ||
print(jaro_winkler("hello", "world")) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.