Last active
March 16, 2019 22:25
-
-
Save toomasv/40caae5e5b31d21612621e7d16aa9f2f to your computer and use it in GitHub Desktop.
Implementation of Levenshtein distance
This file contains hidden or 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
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