gpt4 book ai didi

c# - Levenshtein 距离 c# 计数错误类型

转载 作者:太空狗 更新时间:2023-10-29 21:45:25 24 4
gpt4 key购买 nike

我发现了这段计算 Levenshtein's distance 的代码在答案和猜测之间:

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];
}

但是我需要一种方法来计算每个错误发生的次数。有没有简单的方法来实现它?

最佳答案

似乎您可以为每个操作添加计数器:

                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;
}
}

关于c# - Levenshtein 距离 c# 计数错误类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15559578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com