Levenshtein距离C#计数错误类型 [英] Levenshtein distance c# count error type

查看:157
本文介绍了Levenshtein距离C#计数错误类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,计算答案和猜测之间莱文斯坦的距离这段代码:

I found this bit of code that computes Levenshtein's distance between an answer and a guess:

int CheckErrors(string Answer, string Guess)
{
    int[,] d = new int[Answer.Length + 1, Guess.Length + 1];
    for (int i = 0; i <= Answer.Length; i++)
        d[i, 0] = i;
    for (int j = 0; j <= Guess.Length; j++)
        d[0, j] = j;
    for (int j = 1; j <= Guess.Length; j++)
        for (int i = 1; i <= Answer.Length; i++)
            if (Answer[i - 1] == Guess[j - 1])
                d[i, j] = d[i - 1, j - 1];  //no operation
            else
                d[i, j] = Math.Min(Math.Min(
                    d[i - 1, j] + 1,    //a deletion

                    d[i, j - 1] + 1),   //an insertion

                    d[i - 1, j - 1] + 1 //a substitution

                );
    return d[Answer.Length, Guess.Length];
}



不过,我需要一种方法来为次,每次误差量做一个计数发生。 ?有没有实现,一个简单的方法

But I need a way to do a count for the amount of times each error occurs. Is there an easy way to implement that?

推荐答案

好像你可以为每个操作添加计数器:

Seems like you could add counters for each of the operations:

                if (Answer[i - 1] == Guess[j - 1])
                    d[i, j] = d[i - 1, j - 1];  //no operation
                else
                {
                    int del = d[i-1, j] + 1;
                    int ins = d[i, j-1] + 1;
                    int sub = d[i-1, j-1] + 1;
                    int op = Math.Min(Math.Min(del, ins), sub);
                    d[i, j] = op;
                    if (i == j)
                    {
                        if (op == del)
                            ++deletions;
                        else if (op == ins)
                            ++insertions;
                        else
                            ++substitutions;
                    }
                }

这篇关于Levenshtein距离C#计数错误类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆