Announcement

Collapse
No announcement yet.

LUA levenshteihn distance

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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..

    Comment


    • #3
      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

      Comment


      • #4
        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, 10:11 AM.

        Comment


        • #5
          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

          AMS8 Plugins
          IMXLH Compiler

          Comment


          • #6
            I want this in AMS.

            http://www.lua.org/manual/5.1/manual.html#5.9

            Comment


            • #7
              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 Files

              Comment


              • #8
                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 :yes

                Comment


                • #9
                  Dermot, I don't know how I can thank you!
                  This is wonderful!

                  thanks

                  gert

                  Comment


                  • #10
                    (..)
                    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".

                    Comment


                    • #11
                      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

                      AMS8 Plugins
                      IMXLH Compiler

                      Comment

                      Working...
                      X