Levenshtein距离c#计数错误类型
我发现了一段在答案和猜测之间进行计算的代码:Levenshtein距离c#计数错误类型,c#,count,levenshtein-distance,C#,Count,Levenshtein 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++)
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];
}
int检查错误(字符串回答、字符串猜测)
{
int[,]d=newint[Answer.Length+1,Guess.Length+1];
对于(int i=0;i,似乎可以为每个操作添加计数器:
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;
}
}
我用一个“hello”和“hello”的例子来尝试这个方法,如果正确的话,删除和插入都会累加起来。这是我需要做的事情,只是不确定这一点是否正确!删除和插入都会在“hello”时吐出数字9是吗?基本问题是我误解了发生的事情。在代码中,你只想在I==j
时增加删除、插入等操作。至少,我认为这就是问题所在。你可能必须对它进行实验。我对代码进行了更改。谢谢,我会尝试一下,看看这是否是我需要的.对不起,我以前看起来很急躁,现在是1:23,我很累。我遇到的问题是,如果我插入的单词比hello短,例如“hllo”,它说有插入和替换。如何解决这个问题?