Install Circle Chain on your iPhone for free and get the source code!! 645 downloads to go - last updated: April 21, 2012

Find Levenshtein distance with AS3

From Wikipedia: The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.

In real world, one of the most common uses of this distance is the suggestion Google throws when it thinks you mispelled your search.

When you search for an uncommon word that is similar to a popular search, Google suggests the word it supposes you were looking for.

This is probably made with Levenshtein distance. Some languages such as PHP have their native function to determine the distance between two string, but Actionscript 3 does not have any function to accomplish this task.

That’s why I am showing you how to do it

The Levenshtein algorithm

Giving two string made by respectively n and m characters, we create a m*n matrix this way:

This is the starting matrix, with all cells close to the strings set at their n-th character and the remaining ones at zero.

Then, we must fill the matrix from the upper left to the lower right corner starting from cell 1,1 (second row, second column) in this way:

I fill the x,y cell, with the minimum value among

* x-1,y cell value + 1
* x,y-1 cell value + 1
* x-1,y-1 cell value + d

where d = 0 if the xth character of the second string is the same as the yth character of the first string, and 1 if such characters are different.

So in our case the first line of the matrix will be filled this way:

The red value is determined by the minimum value among 2 (leftmost cell value plus one), 2 (upper cell value plus one) and 1 (upper left diagonal cell value plus one because T and F aren’t the same character)

The green value is determined by the minimum value among 2 (leftmost cell value plus one), 2 (upper cell value plus one) and 2 (upper left diagonal cell value plus one because O and F aren’t the same character)

Apply this principle to the entire matrix and you will get

The red number at the lower right of the matrix is the distance.

Here it is translated into AS3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.events.Event;
	public class levenshtein extends Sprite {
		var from:TextField=new TextField();
		var to:TextField=new TextField();
		var lev:TextField=new TextField();
		var text_format:TextFormat = new TextFormat();
		public function levenshtein():void {
			text_format.color=0x000000;
			text_format.size=24;
			addChild(from);
			with (from) {
				type=TextFieldType.INPUT;
				x=20;
				y=20;
				width=460;
				height=30;
				text="from";
				border=true;
				setTextFormat(text_format);
				addEventListener(Event.CHANGE,on_change);
			}
			addChild(to);
			with (to) {
				type=TextFieldType.INPUT;
				x=20;
				y=70;
				width=460;
				height=30;
				text="to";
				border=true;
				setTextFormat(text_format);
				addEventListener(Event.CHANGE,on_change);
			}
			addChild(lev);
			with (lev) {
				x=20;
				y=120;
				width=460;
				height=30;
				text="distance = "+distance(from.text,to.text);
				setTextFormat(text_format);
			}
		}
		public function on_change(e:Event):void {
			lev.text="distance = "+distance(from.text,to.text);
			lev.setTextFormat(text_format);
		}
		public function distance(string_1:String, string_2:String):int {
			var matrix:Array=new Array();
			var dist:int;
			for (var i:int=0; i<=string_1.length; i++) {
				matrix[i]=new Array();
				for (var j:int=0; j<=string_2.length; j++) {
					if (i!=0) {
						matrix[i].push(0);
					} else {
						matrix[i].push(j);
					}
				}
				matrix[i][0]=i;
			}
			for (i=1; i<=string_1.length; i++) {
				for (j=1; j<=string_2.length; j++) {
					if (string_1.charAt(i-1)==string_2.charAt(j-1)) {
						dist=0;
					} else {
						dist=1;
					}
					matrix[i][j]=Math.min(matrix[i-1][j]+1,matrix[i][j-1]+1,matrix[i-1][j-1]+dist);
				}
			}
			return matrix[string_1.length][string_2.length];
		}
	}
}

And this is the result:

Type anything you want in both text areas and see the Levenshtein distance in real time

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.80 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 5 comments

  1. Fabio

    on June 9, 2010 at 4:29 pm

    Great post! You can find another as3 implementation of the algorithm in GSkinner StringUtils.editDistance: http://www.gskinner.com/blog/archives/2007/04/free_extension.html

  2. Wilson Silva

    on June 10, 2010 at 1:26 pm

    That’s it. I’m going to subscribe to this blog via RSS :D

  3. Detecting mouse gestures in Flash with AS3 - Emanuele Feronato

    on July 5, 2010 at 3:55 pm

    [...] you are almost ready to use mouse gestures in your code… you just should add some Levenshtein distance adjustment and you’re done… I’ll show you how to do this next time. Do you [...]

  4. Skas

    on March 30, 2012 at 4:19 pm

    Wow, ive been looking for days now regarding mouse gesturesLevensthien distances in flash. Tank you Emanuel for taking the time to enlighten the world :>

  5. ActionScript Community | Pearltrees

    on April 9, 2012 at 12:18 am

    [...] This is probably made with Levenshtein distance. Some languages such as PHP have their native function to determine the distance between two string, but Actionscript 3 does not have any function to accomplish this task. That’s why I am showing you how to do it When you search for an uncommon word that is similar to a popular search, Google suggests the word it supposes you were looking for. The Levenshtein algorithm Find Levenshtein distance with AS3 – Emanuele Feronato [...]