Damerau-Levenshtein距离实现

Damerau-Levenshtein距离实现,第1张

Damerau-Levenshtein距离实现

要点@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;}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5623076.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存