levenshtein
levenshtein, function
def pure levenshtein(a: text, b: text): number
Returns the case-sensitive Levenshtein distance between its arguments.
That distance is defined as the smallest number of edits (to remove a character, to add a character, or to replace a character with another) needed to transform the first argument into the second argument. The distance is 0 if the two arguments are equal, and is always smaller than the length of the longest argument.
Example
table Words = with
[| as From, as To |]
[| "kitten", "sitting" |]
[| "Saturday", "Sunday" |]
show table "Distance" with
Words.From
Words.To
levenshtein(Words.From, Words.To)
This displays the following table:
From | To | levenshtein() | |
---|---|---|---|
kitten | sitting | 3 | k to s, e to i, append g |
Saturday | Sunday | 3 | a to u, remove t, remove r |
Recipes and Best Practices
You can combine argmin
and levenshtein
to find the value closest to a given text.
read form with
query : text
read "Words.csv" as Words with
Word : text
show scalar "Closest word" with
argmin(levenshtein(Words.Word, query), Words.Word)