/ Published in: ActionScript 3
This is the implementation of the algorithm that computes Levenshtein distance . This is used in different applications as spell checkers.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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]; }