I have found some Python
implementations of the Levenshtein distance.
I am wondering though how these algorithms can be efficiently modified so that they break if the Levenshtein distance is greater than n
(e.g. 3) instead of running until the end?
So essentially I do not want to let the algorithm run for too long to calculate the final distance if I simply want to know if the distance is greater than a threshold or not.
I have found some relevant posts here:
- Modifying Levenshtein Distance algorithm to not calculate all distances
- Levenstein distance limit
- Most efficient way to calculate Levenshtein distance
- Levenshtein Distance Algorithm better than O(n*m)?
but still, I do not see any Python code which does what I describe above (which is more or less what these posts describe too).
P.S.: The solution provided by @amirouche below is based on the fastest implementation that I have tested with some benchmarking (from here: https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python, https://mcmap.net/q/40678/-edit-distance-in-python) and its bounded version is the fastest one of its kind from my tests (without excluding that there may be even faster ones).