def levenshtein_distance_words()

in src/Backend/src/api/routers/processor/endpoints.py [0:0]


def levenshtein_distance_words(list_one: list[str], list_two: list[str]) -> int:
    """
    Checks the levenshtein distance between two lists of strings
    eg.: ["hello", "wonderful", "world"]  and ["hello", "darling"]
    returns 2 points (1 substitution and 1 deletion)
    """
    distances = [
        [0 for _ in range(len(list_two) + 1)] for _ in range(len(list_one) + 1)
    ]

    for i in range(len(list_one) + 1):
        distances[i][0] = i
    for j in range(len(list_two) + 1):
        distances[0][j] = j

    for i in range(1, len(list_one) + 1):
        for j in range(1, len(list_two) + 1):
            cost = 0 if list_one[i - 1] == list_two[j - 1] else 1
            distances[i][j] = min(
                distances[i - 1][j] + 1,
                distances[i][j - 1] + 1,
                distances[i - 1][j - 1] + cost,
            )

    return distances[-1][-1]