Created
February 27, 2018 09:05
-
-
Save DarkWiiPlayer/f00633a8333f11b3ec1d97edbab2481c to your computer and use it in GitHub Desktop.
Levenshtein distance in moonscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import min from math | |
levenshtein = (a, b) -> | |
c = {nil, nil, nil, nil, nil} | |
p = {nil, nil, nil, nil, nil} | |
for i=1,#a+1 | |
c[i]=i-1 -- Distance to "" | |
for i=1,#b | |
p,c = c,p | |
c[1] = i -- Distance from "" | |
for j=1,#a | |
deletioncost = p[j+1]+1 | |
insertioncost = c[j]+1 | |
local substitutioncost | |
if a\byte(j) == b\byte(i) | |
substitutioncost = p[j] | |
else | |
substitutioncost = p[j] + 1 | |
c[j+1] = min(deletioncost, insertioncost, substitutioncost) | |
c[#a+1] | |
{:levenshtein} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment