pythonstringlevenshtein-distance

String edit distance in python


I need to check if the string distance (Measure the minimal number of changes - character removal, addition, and transposition) between two strings in python is greater than 1.

I can implement it on my own, but I bet there are existing packages that would save me from implementing that on my own. I wasn't able to find any such package I could identify as commonly used. Are there any?


Solution

  • There is a NLTK package which you can use, it uses the Levenshtein edit-distance which should be what you're looking for.

    Example:

    import nltk
    s1 = "abc"
    s2 = "ebcd"
    nltk.edit_distance(s1, s2) # output: 2
    

    Reference: https://tedboy.github.io/nlps/generated/generated/nltk.edit_distance.html