-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Update levenshtein_distance.py #11171
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
cclauss
merged 11 commits into
TheAlgorithms:master
from
pedram-mohajer:pedram-mohajer-patch-2
Nov 26, 2023
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f6bf88b
Update levenshtein_distance.py
pedram-mohajer 0f5c583
Update levenshtein_distance.py
pedram-mohajer 994f7a4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7ed8364
Update levenshtein_distance.py
pedram-mohajer 1b98019
Update levenshtein_distance.py
pedram-mohajer b01dc7e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4d0fb18
Update levenshtein_distance.py
pedram-mohajer ab2e321
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6952b48
Update levenshtein_distance.py
pedram-mohajer 54bb20e
Update levenshtein_distance.py
cclauss 9a64f33
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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 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 |
---|---|---|
@@ -1,70 +1,71 @@ | ||
""" | ||
This is a Python implementation of the levenshtein distance. | ||
Levenshtein distance is a string metric for measuring the | ||
difference between two sequences. | ||
This is an optimized Python implementation of the Levenshtein distance algorithm. | ||
Levenshtein distance is a metric for measuring differences between sequences. | ||
|
||
For doctests run following command: | ||
python -m doctest -v levenshtein-distance.py | ||
For doctests, run the following command: | ||
python -m doctest -v levenshtein_distance.py | ||
or | ||
python3 -m doctest -v levenshtein-distance.py | ||
python3 -m doctest -v levenshtein_distance.py | ||
|
||
For manual testing run: | ||
python levenshtein-distance.py | ||
For manual testing, run: | ||
python levenshtein_distance.py | ||
""" | ||
|
||
|
||
def levenshtein_distance(first_word: str, second_word: str) -> int: | ||
"""Implementation of the levenshtein distance in Python. | ||
:param first_word: the first word to measure the difference. | ||
:param second_word: the second word to measure the difference. | ||
:return: the levenshtein distance between the two words. | ||
def levenshtein_distance_optimized(first_word: str, second_word: str) -> int: | ||
""" | ||
Compute the Levenshtein distance between two words (strings). | ||
|
||
The function is optimized for efficiency by modifying rows in place. | ||
|
||
Parameters: | ||
first_word (str): The first word to measure the difference. | ||
second_word (str): The second word to measure the difference. | ||
|
||
Returns: | ||
int: The Levenshtein distance between the two words. | ||
|
||
Examples: | ||
>>> levenshtein_distance("planet", "planetary") | ||
>>> levenshtein_distance_optimized("planet", "planetary") | ||
3 | ||
>>> levenshtein_distance("", "test") | ||
>>> levenshtein_distance_optimized("", "test") | ||
4 | ||
>>> levenshtein_distance("book", "back") | ||
>>> levenshtein_distance_optimized("book", "back") | ||
2 | ||
>>> levenshtein_distance("book", "book") | ||
>>> levenshtein_distance_optimized("book", "book") | ||
0 | ||
>>> levenshtein_distance("test", "") | ||
>>> levenshtein_distance_optimized("test", "") | ||
4 | ||
>>> levenshtein_distance("", "") | ||
>>> levenshtein_distance_optimized("", "") | ||
0 | ||
>>> levenshtein_distance("orchestration", "container") | ||
>>> levenshtein_distance_optimized("orchestration", "container") | ||
10 | ||
""" | ||
# The longer word should come first | ||
if len(first_word) < len(second_word): | ||
return levenshtein_distance(second_word, first_word) | ||
return levenshtein_distance_optimized(second_word, first_word) | ||
|
||
if len(second_word) == 0: | ||
return len(first_word) | ||
|
||
previous_row = list(range(len(second_word) + 1)) | ||
|
||
for i, c1 in enumerate(first_word): | ||
current_row = [i + 1] | ||
current_row = [i + 1] + [0] * len(second_word) | ||
|
||
for j, c2 in enumerate(second_word): | ||
# Calculate insertions, deletions and substitutions | ||
insertions = previous_row[j + 1] + 1 | ||
deletions = current_row[j] + 1 | ||
substitutions = previous_row[j] + (c1 != c2) | ||
current_row[j + 1] = min(insertions, deletions, substitutions) | ||
|
||
# Get the minimum to append to the current row | ||
current_row.append(min(insertions, deletions, substitutions)) | ||
|
||
# Store the previous row | ||
previous_row = current_row | ||
|
||
# Returns the last element (distance) | ||
return previous_row[-1] | ||
|
||
|
||
if __name__ == "__main__": | ||
first_word = input("Enter the first word:\n").strip() | ||
second_word = input("Enter the second word:\n").strip() | ||
|
||
result = levenshtein_distance(first_word, second_word) | ||
result = levenshtein_distance_optimized(first_word, second_word) | ||
print(f"Levenshtein distance between {first_word} and {second_word} is {result}") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a standard way to document parameters and return values. Please do not repeat the datatypes that are in the function signature because mypy validates them and readers will be confused if one is change and the other is not.