View Full Version : LUA levenshteihn distance
gvanassche
02-28-2010, 03:18 AM
Hi all, I found the Levenshtein distance calculation and I tried to embed this in an app, but when I use it, I get an error.
But I don't understand where it comes from.
Can anyone take a look at this?
thanks
gert
T3STY
02-28-2010, 08:08 AM
I don't know where did you take this code from or what programming language were going to use but I didn't saw anything like this before, in AMS:
function levenshtein(string1, string2)
local str1, str2, distance = {}, {}, {};
str1.len, str2.len = string.len(string1), string.len(string2);
string.gsub(string1, "(.)", function(s) table.insert(str1, s); end);
string.gsub(string2, "(.)", function(s) table.insert(str2, s); end);
for i = 0, str1.len do distance[i][0] = i; end
for i = 0, str2.len do distance[0][i] = i; end
for i = 1, str1.len do
for j = 1, str2.len do
local tmpdist = 1;
if(str1[i-1] == str2[j-1]) then tmpdist = 0; end
distance[i][j] = math.min(
distance[i-1][j] + 1, distance[i][j-1]+1, distance[i-1][j-1] + tmpdist);
end
end
return distance[str1.len][str2.len];
end
By watching the for loop syntax, it seems to be something like C/C++ (or else, I don't know). Give us the original code and we may help you porting the code in AMS. As actually is your function, I can't understand almost anything..
gvanassche
02-28-2010, 09:03 AM
He, thanks for reacting.
An example of an implementation is on
http://www.miislita.com/searchito/levenshtein-edit-distance.html
Take a look at this to see what the code should do.
There is a lot of examples, code written in different languages on
http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Levenshtein_distance
This code however came from:
http://www.freemedialibrary.com/index.php/Levenshtein_distance#Lua
I hope you can help me.
thanks
gert
T3STY
02-28-2010, 11:03 AM
The code you've tried to embed is pure lua wich it's not really AMS's programming language, it has been modified a little in AMS.
I'll give a look to other languages and I'll try to recode the function as soon as I can.
EDIT
Hey, there is the JavaScript code.. you can use AMSWaves's Script plugin to execute javascript code:
http://www.indigorose.com/forums/showthread.php?t=25004
Imagine Programming
02-28-2010, 12:03 PM
The code you've tried to embed is pure lua wich it's not really AMS's programming language, it has been modified a little in AMS.
I'll give a look to other languages and I'll try to recode the function as soon as I can.
EDIT
Hey, there is the JavaScript code.. you can use AMSWaves's Script plugin to execute javascript code:
http://www.indigorose.com/forums/showthread.php?t=25004
As long lua is lua5.0, and uses no external libs/packages, it'll work in AMS :)
AMS lua is lua5.0 with added functions, the syntax is no diffirent.
Sakuya
02-28-2010, 12:24 PM
I want this in AMS.
http://www.lua.org/manual/5.1/manual.html#5.9
gvanassche
02-28-2010, 12:33 PM
I tried to install amswscript, but I always get the message attached.
I copied the folder AMSWScript (including the file AMSWScript.lmd)
to C:\Program Files\AutoPlay Media Studio 7.0\Plugins\Actions
For what I want to do, I also think this script-detour will be too slow.
I would appreciate a LUA solution for sure.
thanks
gert
Dermot
02-28-2010, 12:39 PM
That function was missing some subtable declarations. This works good.
function levenshtein(string1, string2)
local str1, str2, distance = {}, {}, {};
str1.len, str2.len = string.len(string1), string.len(string2);
string.gsub(string1, "(.)", function(s) table.insert(str1, s); end);
string.gsub(string2, "(.)", function(s) table.insert(str2, s); end);
for i = 0, str1.len do
distance[i] = {}
distance[i][0] = i;
end
distance[0] = {}
for i = 0, str2.len do
distance[0][i] = i;
end
for i = 1, str1.len do
for j = 1, str2.len do
local tmpdist = 1;
if(str1[i-1] == str2[j-1]) then tmpdist = 0; end
distance[i][j] = math.min(
distance[i-1][j] + 1, distance[i][j-1]+1, distance[i-1][j-1] + tmpdist);
end
end
return distance[str1.len][str2.len];
end
gvanassche
02-28-2010, 01:08 PM
Dermot, I don't know how I can thank you!
This is wonderful!
thanks
gert
T3STY
02-28-2010, 02:08 PM
(..)
for i = 0, str2.len do
distance[0][i] = i;
end
(..)
Hey, why is this working ??!
I tried lots of times to make for loops like this and it never worked, also the script editor were telling something like "unknown value (or something) in for... do".
Imagine Programming
02-28-2010, 02:17 PM
Hey, why is this working ??!
I tried lots of times to make for loops like this and it never worked, also the script editor were telling something like "unknown value (or something) in for... do".
Then you must have done something wrong, because
for iteration = 0, endnumber, step do
--code
end
should work.
Powered by vBulletin™ Version 4.0.6 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.