要点@doukremt给出了:https
://gist.github.com/doukremt/9473228
在Javascript中提供以下内容。
var levenshteinWeighted= function(seq1,seq2){ var len1=seq1.length; var len2=seq2.length; var i, j; var dist; var ic, dc, rc; var last, old, column; var weighter={ insert:function(c) { return 1.; }, delete:function(c) { return 0.5; }, replace:function(c, d) { return 0.3; } }; if (len1 == 0 || len2 == 0) { dist = 0; while (len1) dist += weighter.delete(seq1[--len1]); while (len2) dist += weighter.insert(seq2[--len2]); return dist; } column = []; // malloc((len2 + 1) * sizeof(double)); //if (!column) return -1; column[0] = 0; for (j = 1; j <= len2; ++j) column[j] = column[j - 1] + weighter.insert(seq2[j - 1]); for (i = 1; i <= len1; ++i) { last = column[0]; column[0] += weighter.delete(seq1[i - 1]); for (j = 1; j <= len2; ++j) { old = column[j]; if (seq1[i - 1] == seq2[j - 1]) { column[j] = last; } else { ic = column[j - 1] + weighter.insert(seq2[j - 1]); dc = column[j] + weighter.delete(seq1[i - 1]); rc = last + weighter.replace(seq1[i - 1], seq2[j - 1]); column[j] = ic < dc ? ic : (dc < rc ? dc : rc); } last = old; } } dist = column[len2]; return dist;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)