达默劳-编辑 php

发布于 2024-09-08 03:22:17 字数 523 浏览 9 评论 0原文

我正在寻找 PHP 的 Damerau–Levenshtein 算法的实现,但是我用我的朋友google似乎找不到任何东西。到目前为止,我必须使用 PHP 实现的 Levenshtein(没有 Damerau 转置,这非常重要),或者获取原始源代码(C、C++、C#、Perl)并将其编写(翻译)为 PHP。

有人了解 PHP 实现吗?

我在公司 Intranet 上使用 soundex 和 double metaphone 作为“您的意思是:”扩展,并且我想实现 Damerau–Levenshtein 算法来帮助我更好地对结果进行排序。与此想法类似的东西: http://www.briandrought.com/blog/?p=66 ,我的实现与前5步类似。

I'm searching for an implementations of the Damerau–Levenshtein algorithm for PHP, but it seems that I can't find anything with my friend google. So far I have to use PHP implemented Levenshtein (without Damerau transposition, which is very important), or get a original source code (in C, C++, C#, Perl) and write (translate) it to PHP.

Does anybody have any knowledge of a PHP implementation ?

I'm using soundex and double metaphone for a "Did you mean:" extension on my corporate intranet, and I want to implement the Damerau–Levenshtein algorithm to help me sort the results better. Something similar to this idea: http://www.briandrought.com/blog/?p=66, my implementation is similar to the first 5 steps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬花如无物 2024-09-15 03:22:17

我回来时有一个刺穿它一个递归解决方案。

/*
 * Naïve implementation of Damerau-Levenshtein distance
 * (Does not work when there are neighbouring transpositions)!
 */
function DamerauLevenshtein($S1, $S2)
{
    $L1 = strlen($S1);
    $L2 = strlen($S2);
    if ($L1==0 || $L2==0) {
        // Trivial case: one string is 0-length
        return max($L1, $L2);
    }
    else {
        // The cost of substituting the last character
        $substitutionCost = ($S1[$L1-1] != $S2[$L2-1])? 1 : 0;
        // {H1,H2} are {L1,L2} with the last character chopped off
        $H1 = substr($S1, 0, $L1-1);
        $H2 = substr($S2, 0, $L2-1);
        if ($L1>1 && $L2>1 && $S1[$L1-1]==$S2[$L2-2] && $S1[$L1-2]==$S2[$L2-1]) {
            return min (
                DamerauLevenshtein($H1, $S2) + 1,
                DamerauLevenshtein($S1, $H2) + 1,
                DamerauLevenshtein($H1, $H2) + $substitutionCost,
                DamerauLevenshtein(substr($S1, 0, $L1-2), substr($S2, 0, $L2-2)) + 1
            );
        }
        return min (
            DamerauLevenshtein($H1, $S2) + 1,
            DamerauLevenshtein($S1, $H2) + 1,
            DamerauLevenshtein($H1, $H2) + $substitutionCost
        );
    }
}

I had a stab at it a recursive solution while back.

/*
 * Naïve implementation of Damerau-Levenshtein distance
 * (Does not work when there are neighbouring transpositions)!
 */
function DamerauLevenshtein($S1, $S2)
{
    $L1 = strlen($S1);
    $L2 = strlen($S2);
    if ($L1==0 || $L2==0) {
        // Trivial case: one string is 0-length
        return max($L1, $L2);
    }
    else {
        // The cost of substituting the last character
        $substitutionCost = ($S1[$L1-1] != $S2[$L2-1])? 1 : 0;
        // {H1,H2} are {L1,L2} with the last character chopped off
        $H1 = substr($S1, 0, $L1-1);
        $H2 = substr($S2, 0, $L2-1);
        if ($L1>1 && $L2>1 && $S1[$L1-1]==$S2[$L2-2] && $S1[$L1-2]==$S2[$L2-1]) {
            return min (
                DamerauLevenshtein($H1, $S2) + 1,
                DamerauLevenshtein($S1, $H2) + 1,
                DamerauLevenshtein($H1, $H2) + $substitutionCost,
                DamerauLevenshtein(substr($S1, 0, $L1-2), substr($S2, 0, $L2-2)) + 1
            );
        }
        return min (
            DamerauLevenshtein($H1, $S2) + 1,
            DamerauLevenshtein($S1, $H2) + 1,
            DamerauLevenshtein($H1, $H2) + $substitutionCost
        );
    }
}
无风消散 2024-09-15 03:22:17

看看我们的实现(带有测试和文档)。

Have a look at our implementation (with tests and documentation).

鸢与 2024-09-15 03:22:17

只使用内置的 php 函数怎么样?

http://php.net/manual/en/function.levenshtein.php

int levenshtein ( string $str1 , string $str2 )


int levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )

How about just using the built in php function... ?

http://php.net/manual/en/function.levenshtein.php

int levenshtein ( string $str1 , string $str2 )


int levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文