Created
January 22, 2013 11:16
-
-
Save ai212983/4593883 to your computer and use it in GitHub Desktop.
Levenstein distance in ActionScript 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function levenshteinDistance(s1:String,s2:String):int | |
{ | |
var m:int=s1.length; | |
var n:int=s2.length; | |
var matrix:Array=new Array(); | |
var line:Array; | |
var i:int; | |
var j:int; | |
for (i=0;i<=m;i++) | |
{ | |
line=new Array(); | |
for (j=0;j<=n;j++) | |
{ | |
if (i!=0)line.push(0) | |
else line.push(j); | |
} | |
line[0]=i | |
matrix.push(line); | |
} | |
var cost:int; | |
for (i=1;i<=m;i++) | |
for (j=1;j<=n;j++) | |
{ | |
if (s1.charAt(i-1)==s2.charAt(j-1)) cost=0 | |
else cost=1; | |
matrix[i][j]=Math.min(matrix[i-1][j]+1,matrix[i][j-1]+1,matrix[i-1][j-1]+cost); | |
} | |
return matrix[m][n]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment