Skip to content

Distance

Distance class#

Contains functions for calculating the levenshtein distance between strings.

distance(string1: str, string2: str) -> Optional[int] #

Returns the levenshtein distance between two strings.

Parameters:

Name Type Description Default
string1 str

The first string to compare.

required
string2 str

The second string to compare.

required

Returns:

Type Description
Optional[int]

The levenshtein distance between the two strings.

Examples:

>>> distance("stringmatch", "strmatch")
3
>>> distance("stringmatch", "something different")
14
Source code in stringmatch/distance.py
def distance(self, string1: str, string2: str) -> Optional[int]:
    """Returns the levenshtein distance between two strings.

    Parameters
    ----------
    string1 : str
        The first string to compare.
    string2 : str
        The second string to compare.

    Returns
    -------
    Optional[int]
        The levenshtein distance between the two strings.

    Examples
    --------
    >>> distance("stringmatch", "strmatch")
    3
    >>> distance("stringmatch", "something different")
    14
    """
    return distance(string1, string2) if string1 and string2 else None

distance_list(string: str, string_list: List[str]) -> List[Optional[int]] #

Returns the levenshtein distance for a string and a list of strings.

Parameters:

Name Type Description Default
string str

The string to compare.

required
string_list List[str]

The List of strings to compare to.

required

Returns:

Type Description
List[Optional[int]]

The levenshtein distances between the two strings.

Examples:

>>> distance_list("stringmatch", ["strmatch", "something different"])
[3, 14]
Source code in stringmatch/distance.py
def distance_list(self, string: str, string_list: List[str]) -> List[Optional[int]]:
    """Returns the levenshtein distance for a string and a list of strings.

    Parameters
    ----------
    string : str
        The string to compare.
    string_list : List[str]
        The List of strings to compare to.

    Returns
    -------
    List[Optional[int]]
        The levenshtein distances between the two strings.

    Examples
    --------
    >>> distance_list("stringmatch", ["strmatch", "something different"])
    [3, 14]
    """
    return [self.distance(string, s) for s in string_list]