Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active March 16, 2019 22:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toomasv/40caae5e5b31d21612621e7d16aa9f2f to your computer and use it in GitHub Desktop.
Save toomasv/40caae5e5b31d21612621e7d16aa9f2f to your computer and use it in GitHub Desktop.
Implementation of Levenshtein distance
Red [
Date: 6-Jan-2019
File: %levenshtein.red
]
lev: function [s t][
unless string? s [s: mold s]
unless string? t [t: mold t]
case [
s = t [return 0]
empty? s [return (length? t)]
empty? t [return (length? s)]
]
v0: append/dup make block! 1 + length? t none 1 + length? t
v1: copy v0
repeat i length? v0 [v0/:i: i - 1]
repeat i length? s [
v1/1: i
repeat j length? t [
cost: pick [0 1] s/:i = t/:j
v1/(j + 1): min min v1/:j + 1 v0/(j + 1) + 1 v0/:j + cost
]
repeat j length? v0 [v0/:j: v1/:j]
]
v1/(1 + length? t)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment