July 7, 2009 at 15:01[AS3] Levenshtein distance

La distance de Leventshtein est un algorithme imaginé en 1965 par Vladimir Levenshtein qui permet de définir le nombre d’opérations minimum nécessaires pour transformer une chaine de caractères en une autre.  Et qui permet donc de calculer la différence entre deux chaines.

Utilisé en ActionScript notamment pour l’API de Mouse Gesture Recognition :

mouse-gesture-recognition

Je réfléchit en ce moment à l’utilisation de ce script pour un moteur d’apriori dans une intelligence artificielle :

intelligence-artificielle-as3

Voici une implémentation ActionScript3.0 trouvée sur snipplr.com :

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];
}
Written by Rémi.T under Coding.
Tags: .
Add a comment »

May 26, 2009 at 22:40[AS3] Exploring cross-scripting, Part 1

While working on a huge AS3 application last month, i discovered the magic of cross-scripting. What’s that s*** ? Following the livedocs :

If two SWF files written with ActionScript 3.0 are served from the same domain–for example, the URL for one SWF file is http://www.example.com/swfA.swf and the URL for the other is http://www.example.com/swfB.swf–then one SWF file can examine and modify variables, objects, properties, methods, and so on in the other, and vice versa. This is called cross-scripting.

But they don’t talk about the multiple problems you can find loading SWFs into SWFs. Here’s my first test with cross-scripting :

multi-swf

(more…)

Written by Rémi.T under Coding.
Tags: , .
2 comments »

May 3, 2009 at 13:58[AS3] Using the … argument

This argument is very useful. You can type it as Array. It can’t be null :

function pouet(... args:Array):void {
    trace("pouet [", args.length, "]", args);
}

// pouet [ 4 ] pouet,tagada,tsoin,tsoin
pouet("pouet","tagada","tsoin","tsoin");
// pouet [ 4 ] pouet,tagada,,tsoin
pouet("pouet","tagada",null,"tsoin");
// pouet [ 4 ] pouet,tagada,,
pouet("pouet","tagada",null,null);
// pouet [ 4 ] ,,,
pouet(null,null,null,null);
// pouet [ 0 ]
pouet();
Written by Rémi.T under Coding.
Tags: .
Add a comment »