nltk.edit_distance()

nltk.edit_distance(s1, s2, transpositions=False)[source]

Calculate the Levenshtein edit-distance between two strings. The edit distance is the number of characters that need to be substituted, inserted, or deleted, to transform s1 into s2. For example, transforming “rain” to “shine” requires three steps, consisting of two substitutions and one insertion: “rain” -> “sain” -> “shin” -> “shine”. These operations could have been done in other orders, but at least three steps are needed.

This also optionally allows transposition edits (e.g., “ab” -> “ba”), though this is disabled by default.

Parameters:
  • s2 (str) – The strings to be analysed
  • transpositions (bool) – Whether to allow transposition edits

:rtype int