Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Levenshtein编辑距离算法,支持C语言中两个相邻字母的换位#_C#_Nlp_Edit Distance - Fatal编程技术网

C# Levenshtein编辑距离算法,支持C语言中两个相邻字母的换位#

C# Levenshtein编辑距离算法,支持C语言中两个相邻字母的换位#,c#,nlp,edit-distance,C#,Nlp,Edit Distance,我正在寻找一种计算Levenshtein编辑距离的算法,该算法也支持两个相邻字母被转置的情况,这种情况在C#中实现 例如,单词“动物”和“动物”: 在字母“n”和“i”之间切换 不会被分为两个替补-这将是一个很大的距离- 但取而代之的是,on将被评分为两个字母的转置,距离要小得多- 到目前为止,我在寻找中达到了什么程度 但它不包含替换项 参见维基百科上的实现。您可以轻松地调整算法,以包括字母交换的情况。例如: //bla bla. I'm just copying the code on t

我正在寻找一种计算Levenshtein编辑距离的算法,该算法也支持两个相邻字母被转置的情况,这种情况在C#中实现

例如,单词“动物”和“动物”: 在字母“n”和“i”之间切换 不会被分为两个替补-这将是一个很大的距离- 但取而代之的是,on将被评分为两个字母的转置,距离要小得多-

到目前为止,我在寻找中达到了什么程度

  • 但它不包含替换项

    • 参见维基百科上的实现。您可以轻松地调整算法,以包括字母交换的情况。例如:

      //bla bla. I'm just copying the code on the Wikipedia.
       d[i, j] := minimum
                         (
                           d[i-1, j] + 1,  // a deletion
                           d[i, j-1] + 1,  // an insertion
                           d[i-1, j-1] + 1, // a substitution
                         )
      
      // This single statement is all you need:
      if(s[i-1]==t[j-2] && s[i-2]==t[j-1])
         d[i,j] := minimum
                        (
                            d[i,j],               //cost without swapping 
                            d[i-2,j-2]+something  //cost with swapping. probably something=1 
                        );
      

      请参阅Wikipedia上的实现。您可以轻松地调整算法,以包括字母交换的情况。例如:

      //bla bla. I'm just copying the code on the Wikipedia.
       d[i, j] := minimum
                         (
                           d[i-1, j] + 1,  // a deletion
                           d[i, j-1] + 1,  // an insertion
                           d[i-1, j-1] + 1, // a substitution
                         )
      
      // This single statement is all you need:
      if(s[i-1]==t[j-2] && s[i-2]==t[j-1])
         d[i,j] := minimum
                        (
                            d[i,j],               //cost without swapping 
                            d[i-2,j-2]+something  //cost with swapping. probably something=1 
                        );
      

      您需要添加附加条件,使其成为“Damerau–Levenshtein距离”算法。因此,使用这里的示例:您只需要在步骤6之后添加以下条件:

       //** Step 7 to make it Damerau–Levenshtein distance
            if (i > 1 && j > 1 && (s[i - 1] == t[j - 2]) && (s[i - 2] == t[j - 1]))
            {
                   d[i, j] = Math.Min(
                                  d[i, j],
                                  d[i - 2, j - 2] + cost   // transposition
                               );
            }
      

      您需要添加附加条件,使其成为“Damerau–Levenshtein距离”算法。因此,使用这里的示例:您只需要在步骤6之后添加以下条件:

       //** Step 7 to make it Damerau–Levenshtein distance
            if (i > 1 && j > 1 && (s[i - 1] == t[j - 2]) && (s[i - 2] == t[j - 1]))
            {
                   d[i, j] = Math.Min(
                                  d[i, j],
                                  d[i - 2, j - 2] + cost   // transposition
                               );
            }
      

      我听说在这种情况下,
      transposition
      也可以使用递归关系来完成,但我不能这样做。我希望我能推断出来,否则有人会的。递归情况下的性能是线性的。我听说在这种情况下,
      transposition
      也可以使用递归关系来完成,但我不能这样做。我希望我能推断出来,否则有人会的。递归情况下的性能是线性的。