Skip to content

Instantly share code, notes, and snippets.

@Syncrossus
Last active September 18, 2019 13:03
Show Gist options
  • Save Syncrossus/b7dbb063fa227b410563be123cb93549 to your computer and use it in GitHub Desktop.
Save Syncrossus/b7dbb063fa227b410563be123cb93549 to your computer and use it in GitHub Desktop.
Levenshtein distance in Python for lists of objects comparable with the `!=` operator. Uses the Wagner-Fischer algorithm.
import numpy as np
def levenshtein(l1, l2):
""" Computes Levenshtein distance between two list-like objects
Args:
l1, l2: subscriptable objects compatible with len()
containing objects comparable with the `!=` operator.
"""
# initializing the distance matrix
m = np.zeros(shape=(len(l1), len(l2)))
m[0, :] = range(0, len(l2))
m[:, 0] = range(0, len(l1))
for i in range(1, len(l1)):
for j in range(1, len(l2)):
cost = 0
if l1[i] != l2[j]:
cost = 1
# distance at i,j is the minimal distance
# of the previous steps + current cost
m[i, j] = min(m[i - 1, j], m[i, j - 1], m[i - 1, j - 1]) + cost
return m[len(l1) - 1, len(l2) - 1]
@Syncrossus
Copy link
Author

Syncrossus commented Jun 8, 2018

This code is released under the WTFPL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment