标签:style blog http color io ar java for sp
/** * 计算把source经过几个步骤可以转变到target的值 * @param source * @param target * @return * */ public static function editDistance(source:String,target:String):int { var slen:int = source.length; var tlen:int = target.length; var d:Array = []; for(var i:int=0;i<=slen;i++){ d[i] = []; d[i][0] = i; if(i==0){ for(var j:int=1;j<=tlen;j++){ d[0][j] = j; } } else { for(var j:int=1;j<=tlen;j++){ d[i][j] = 0; } } } for(var i:int=1;i<=slen;i++){ for(var j:int=1;j<=tlen;j++){ if(source.charCodeAt(i-1) == target.charCodeAt(j-1)){ d[i][j] = d[i-1][j-1]; } else { var insert:int = d[i][j-1]+1; var del:int = d[i-1][j]+1; var update:int = d[i-1][j-1]+1; d[i][j] = Math.min(insert,del)>Math.min(del,update)? Math.min(del,update):Math.min(insert,del); } } } return d[slen][tlen]; } /** * 相似度计算 * @param source * @param target * @return * */ public static function getSimilarityRate(source:String,target:String):Number { return 1-editDistance(source,target)/Math.max(source.length,target.length); }
标签:style blog http color io ar java for sp
原文地址:http://www.cnblogs.com/chzcb/p/4000102.html