-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Levenshtein distance functions to string lib #3648
base: master
Are you sure you want to change the base?
Conversation
Beherith
commented
Aug 29, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the use case for this? Probably worth mentioning in the PR description.
@@ -182,3 +182,66 @@ if not string.formatSI then | |||
return str .. siPrefix | |||
end | |||
end | |||
|
|||
if not string.Levenshtein then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to match other functions, and to make it a little more clear what it does
if not string.Levenshtein then | |
if not string.levenshteinDistance then |
(and changes on related lines for this and FindClosest
)
function string.Levenshtein(a,b) | ||
local lena = string.len(a) | ||
local lenb = string.len(b) | ||
local ssub = string.sub |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
already defined above
local ssub = string.sub |
|
||
-- print(string.Levenshtein(string.rep("asdfasdfasdfasdf", 1000), string.rep("asdfasdfasdfasda", 1000))) -- 5 seconds | ||
-- print(string.FindClosest("apple", {"pear", "popple","bear","ple"})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-- print(string.Levenshtein(string.rep("asdfasdfasdfasdf", 1000), string.rep("asdfasdfasdfasda", 1000))) -- 5 seconds | |
-- print(string.FindClosest("apple", {"pear", "popple","bear","ple"})) |
Levenshtein0[c+1] = c | ||
end | ||
for r = 1, lena do | ||
--print(table.unpack(Levenshtein0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--print(table.unpack(Levenshtein0)) |
end | ||
Levenshtein0, Levenshtein1 = Levenshtein1, Levenshtein0 -- swap rows | ||
end | ||
return Levenshtein1[lenb] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function gives incorrect output for at least these test cases:
{
name = "returns the length of the non-empty string for one empty string",
args = { "", "apple" },
expected = 5,
},
{
name = "returns the length of the non-empty string for one empty string (reverse case)",
args = { "apple", "" },
expected = 5,
},
{
name = "returns 1 for strings differing by one character",
args = { "apple", "appla" },
expected = 1,
},
{
name = "returns the length of the longer string for completely different strings",
args = { "cat", "dogs" },
expected = 4,
},
This change fixes those tests:
return Levenshtein1[lenb] | |
return Levenshtein0[lenb + 1] |
@Beherith What is the status of this? |