def levenshtein(str1, str2): """ Measure the similarity between two strings using Levenshtein distance, which gives the minimum number of character insertions, deletions, and substitutions needed to change one string into the other. Args: str1 (str) str2 (str) normalize (bool): if True, divide Levenshtein distance by the total number of characters in the longest string; otherwise leave the distance as-is Returns: float: similarity between `str1` and `str2` in the interval [0.0, 1.0], where larger values correspond to more similar strings """ distance = _levenshtein(str1, str2) distance /= max(len(str1), len(str2)) return 1.0 - distance
def levenshtein(str1, str2, normalize=False): """ Measure the distance between two strings using Levenshtein distance, which gives the minimum number of character insertions, deletions, and substitutions needed to change one string into the other. Args: str1 (str) str2 (str) normalize (bool): if True, divide Levenshtein distance by the total number of characters in the longest string; otherwise leave the distance as-is Returns: int or float: if `normalize` is False, return an int, otherwise return a float in the interval [0.0, 1.0], where smaller values correspond to more similar strings """ distance = _levenshtein(str1, str2) if normalize is True: distance /= max(len(str1), len(str2)) return distance
def levenshtein(a, b): if (a is None) or (b is None): return 999999 return _levenshtein(a, b)