Indigo Rose Software

Professional Software Development Tools

 
+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2004
    Location
    Belgium, Leuven
    Posts
    145

    LUA levenshteihn distance

    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
    Attached Files

  2. #2
    Join Date
    Feb 2009
    Posts
    1,285
    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:
    Code:
    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..

  3. #3
    Join Date
    Nov 2004
    Location
    Belgium, Leuven
    Posts
    145
    He, thanks for reacting.

    An example of an implementation is on
    http://www.miislita.com/searchito/le...-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/Algorit...htein_distance

    This code however came from:
    http://www.freemedialibrary.com/inde...n_distance#Lua

    I hope you can help me.

    thanks

    gert

  4. #4
    Join Date
    Feb 2009
    Posts
    1,285
    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
    Last edited by T3STY; 02-28-2010 at 10:11 AM.

  5. #5
    Join Date
    Apr 2007
    Location
    Raalte, OV, Netherlands
    Posts
    3,287
    Quote Originally Posted by T3STY View Post
    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.
    Bas Groothedde
    Imagine Programming :: Blog :: Familiar people here

    My AMS Plugins:

  6. #6
    Join Date
    Oct 2009
    Location
    Merton, United Kingdom
    Posts
    684

  7. #7
    Join Date
    Nov 2004
    Location
    Belgium, Leuven
    Posts
    145
    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
    Attached Images

  8. #8
    Join Date
    Apr 2004
    Location
    Vancouver, Canada
    Posts
    1,790
    That function was missing some subtable declarations. This works good.

    Code:
    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
    Dermot

    I am so out of here

  9. #9
    Join Date
    Nov 2004
    Location
    Belgium, Leuven
    Posts
    145
    Dermot, I don't know how I can thank you!
    This is wonderful!

    thanks

    gert

  10. #10
    Join Date
    Feb 2009
    Posts
    1,285
    (..)
    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".

  11. #11
    Join Date
    Apr 2007
    Location
    Raalte, OV, Netherlands
    Posts
    3,287
    Quote Originally Posted by T3STY View Post
    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
    Code:
    for iteration = 0, endnumber, step do
       --code
    end
    should work.
    Bas Groothedde
    Imagine Programming :: Blog :: Familiar people here

    My AMS Plugins:

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts