Maybe you can do something with this.
It's an implemenataion of the Damerau-Levenshtein distance.
The output is equal to the minimal number of insertions, deletions,
substitutions and transpositions needed to transform one string into the
other. 
Could be handy for a spellchecker...
It's not optimized, just wrote it a few days ago for testing...

private static function compare(first:String, second:String):Number
        {
                if(first == "" || second == "") return null;
                
                var f:Array = first.split("");
                var s:Array = second.split("");
                
                var dist:Array = new Array();
                
                for(var i:Number = f.length+1 ; i--)
                {
                        dist[i] = new Array();
                        for(var j:Number = s.length+1 ; j--)
                        {
                                if(i==0) dist[i][j] = j;
                                else dist[i][j] = 0;                    
                        }
                        dist[i][0] = i;
                }
                
                for(var i:Number = 1 ; i <= f.length ; i++)
                {
                        for(var j:Number = 1 ; j <= s.length ; j++)
                        {
                                var cost:Number = ((f[i-1]==s[j-1])?0:1);
                                var dx:Number = dist[i-1][j]+1;
                                var dy:Number = dist[i][j-1]+1;
                                var dz:Number = dist[i-1][j-1] + cost;
                                dist[i][j] =
Math.min(Math.min(dx,dy),Math.min(dy,dz));
                                
                                if(i > 1 && j > 1 && f[i] == s[j-1] &&
f[i-1] == s[j])
                                        dist[i][j] =
Math.min(dist[i][j],dist[i-2][j-2]+cost);
                        }       
                }
                
                return dist[f.length][s.length];
        } 


Bernard

> -----Oorspronkelijk bericht-----
> Van: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] Namens 
> Steven Sacks | BLITZ
> Verzonden: vrijdag 28 juli 2006 0:59
> Aan: Flashcoders mailing list
> Onderwerp: RE: [Flashcoders] String Empowerment
> 
> Pseudocode examples of usage:
> 
> "hello".capitalize()  > "Hello"  
> "HELLO".capitalize()  > "Hello"  
> "123ABC".capitalize()  > "123abc"  
> 
> "hello".center(4)  > "hello"  
> "hello".center(20, "_")  > "_______hello________"  
> 
> "hello".chomp()  > "hello"  
> "hello\n".chomp()  > "hello"  
> "hello \n there".chomp()  > "hello \n there"  
> "hello".chomp("llo")  > "he"
> 
> "string\r\n".chop()  > "string"  
> "string\n\r".chop()  > "string\n"  
> "string\n".chop()  > "string"  
> "string".chop()  > "strin"  
> "x".chop().chop()  > ""
> 
> a = "hello world"  
> a.count("lo")  > 5
> a.count("^hello ")  > 3
> a.count("ej-m")  > 4
> a.count("^e-t")  > 3 
> 
> a = "hello"
> a.ljust(4)  > "hello"  
> a.ljust(20)  > "hello               "  
> 
> a = "hello world"
> a.remove("l")  > "heo word"  
> a.remove("lo ")  > "hewrd"  
> a.remove("^aeiou")  > "eoo"  
> a.remove("ej-m")  > "ho word" 
> a.remove("^e-t")  > "helloorl"
> 
> "stressed".reverse()  > "desserts"  
> 
> a = "hello"
> a.rjust(4)  > "hello"  
> a.rjust(20, "-")  > "---------------hello"  
> 
> "yellow  moon".squeeze()  > "yelow mon"  
> "  now   is  the".squeeze(" ")  > " now is the"  
> "putters shoot balls".squeeze("m-z")  > "puters shot balls"  
> "hello  world".squeeze("^ ")  > "helo  world"  
> 
> "    hello    ".strip()  > "hello"  
> "\tgoodbye\r\n".strip()  > "goodbye"
> 
> "Hello".swapcase  > "hELLO"  
> "cYbEr_PuNk11".swapcase  > "CyBeR_pUnK11"
> 
> 
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> 
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training 
> http://www.figleaf.com http://training.figleaf.com
> 

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to