pylev
pylev is a pure Python implementation for calculating the Levenshtein distance between two strings, notable for its New BSD license unlike many other GPL-licensed alternatives. The current version is 1.4.0. It has an irregular release cadence, with updates addressing performance improvements and minor fixes.
Warnings
- deprecated In v1.1.0, a much faster Levenshtein variant was introduced and became the default `pylev.levenshtein()`. The older, naive implementation was renamed to `pylev.classic_levenschtein()`. While aliases for backward-compatibility were added in v1.2.0, relying on the older, slower method or its original name is deprecated and should be avoided in new code.
- deprecated In v1.2.0, all incorrect spellings of 'Levenshtein' in method names were corrected. While older, misspelled method names were aliased for backward-compatibility, new code should exclusively use the correctly spelled versions to prevent potential future breakage or confusion.
- gotcha `pylev.levenshtein()` computes the character-level Levenshtein distance between strings. If you intend to calculate word-level distance between sentences or phrases, you must first pre-process the strings into lists of words (e.g., using `str.split()`). Passing raw sentences will treat each character as an individual item.
Install
-
pip install pylev
Imports
- levenshtein
import pylev distance = pylev.levenshtein('string1', 'string2')
Quickstart
import pylev
str1 = 'kitten'
str2 = 'sitting'
distance = pylev.levenshtein(str1, str2)
print(f"Levenshtein distance between '{str1}' and '{str2}': {distance}")