当前位置: 首页 > 知识库问答 >
问题:

什么是C#完全模拟PHP函数“similar_text()”?

宗增
2023-03-14
int ComputeLevenshteinDistance(string source, string target)
{
    if ((source == null) || (target == null)) return 0;
    if ((source.Length == 0) || (target.Length == 0)) return 0;
    if (source == target) return source.Length;

    int sourceWordCount = source.Length;
    int targetWordCount = target.Length;

    // Step 1
    if (sourceWordCount == 0)
        return targetWordCount;

    if (targetWordCount == 0)
        return sourceWordCount;

    int[,] distance = new int[sourceWordCount + 1, targetWordCount + 1];

    // Step 2
    for (int i = 0; i <= sourceWordCount; distance[i, 0] = i++) ;
    for (int j = 0; j <= targetWordCount; distance[0, j] = j++) ;

    for (int i = 1; i <= sourceWordCount; i++)
    {
        for (int j = 1; j <= targetWordCount; j++)
        {
            // Step 3
            int cost = (target[j - 1] == source[i - 1]) ? 0 : 1;

            // Step 4
            distance[i, j] = Math.Min(Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1), distance[i - 1, j - 1] + cost);
        }
    }

    return distance[sourceWordCount, targetWordCount];
}

double CalculateSimilarity(string source, string target)
{
    if ((source == null) || (target == null)) return 0.0;
    if ((source.Length == 0) || (target.Length == 0)) return 0.0;
    if (source == target) return 1.0;

    int stepsToSame = ComputeLevenshteinDistance(source, target);
    return (1.0 - ((double)stepsToSame / (double)Math.Max(source.Length, target.Length)));
}

结果值不是等效的PHP。

PHP Similar_text()函数:它计算两个字符串之间的相似性,如

编程经典:实现世界上最好的算法

共有1个答案

谢璞
2023-03-14

首先,您必须实现编辑距离,比如说,Levenstein一;让我们尽可能地把它做得更通用:

请参见检测两个字符串之间的差异

有关编辑序列和更多细节。

using System.Linq;

...

private static double EditDistance<T>(IEnumerable<T> from,
                                      IEnumerable<T> to,
                                      Func<T, double> insertCost,
                                      Func<T, double> deleteCost,
                                      Func<T, T, double> editCost) {
  T[] source = from.ToArray();
  T[] target = to.ToArray();

  // Minimum cost so far
  double[][] D = Enumerable
    .Range(0, source.Length + 1)
    .Select(line => new double[target.Length + 1])
    .ToArray();

  // Edge: all removes
  double sum = 0.0;

  for (int i = 1; i <= source.Length; ++i)
    D[i][0] = (sum += deleteCost(source[i - 1]));

  // Edge: all inserts
  sum = 0.0;

  for (int i = 1; i <= target.Length; ++i)
    D[0][i] = (sum += insertCost(target[i - 1]));

  // Having fit N - 1, K - 1 characters let's fit N, K
  for (int i = 1; i <= source.Length; ++i)
    for (int j = 1; j <= target.Length; ++j) {
      // here we choose the operation with the least cost
      double insert = D[i][j - 1] + insertCost(target[j - 1]);
      double delete = D[i - 1][j] + deleteCost(source[i - 1]);
      double edit   = D[i - 1][j - 1] + editCost(source[i - 1], target[j - 1]);

      D[i][j] = Math.Min(Math.Min(insert, delete), edit);
    }

  return D[source.Length][target.Length];
}
public static double Similar_text(string left, string right) {
  left = left ?? "";
  right = right ?? "";

  return left.Equals(right)
    ? 1.0
    : 1.0 - EditDistance(left, right, 
              insert => 1.0, 
              delete => 1.0, 
          (from, to) => from == to ? 0.0 : 2.0) 
      / Math.Max(left.Length, right.Length);
}
 类似资料:
  • 问题内容: 在JavaScript中,嵌套函数非常有用:闭包,私有方法以及您拥有的东西。 什么是嵌套PHP函数?有人使用它们吗? 这是我做的小调查 问题答案: 基本上没有,我一直将其视为解析器的副作用。 伊兰·加珀林(Eran Galperin)误认为这些功能是某种私有的,只是在运行之前才声明它们。它们也不是私有范围的,尽管确实延迟了,但它们确实污染了全球范围。作为回调,外部回调仍然只能被调用一次

  • 本文向大家介绍什么是虚函数?什么是抽象函数?相关面试题,主要包含被问及什么是虚函数?什么是抽象函数?时的应答技巧和注意事项,需要的朋友参考一下 答: 虚函数:没有实现的,可由子类继承并重写的函数。Virtual CallSomeOne(); 抽象函数:规定其非虚子类必须实现的函数,必须被重写。public abstract void CallSomeOne();  

  • 主要内容:C语言中的函数和数学中的函数,库函数和自定义函数,参数,返回值从表面上看,函数在使用时必须带上括号,有必要的话还要传递参数,函数的执行结果也可以赋值给其它变量。例如,strcmp() 是一个用来比较字符串大小的函数,它的用法如下: str1 和 str2 是传递给 strcmp() 的参数,strcmp() 的处理结果赋值给了变量 result。 我们不妨设想一下,如果没有 strcmp() 函数,要想比较两个字符串的大小该怎么写呢?请看下面的代码: 比较字

  • 问题内容: 对于没有计算机科学背景的人来说,计算机科学世界中的lambda是什么? 问题答案: Lambda来自Lambda微积分,是指编程中的匿名函数。 为什么这很酷?它允许您编写快速抛出的函数而无需命名它们。它还提供了写闭包的好方法。有了这种能力,您就可以做这样的事情。 Python 从Python片段中可以看到,函数加法器接受一个参数x,并返回一个匿名函数(即lambda),该函数接受另一个

  • 问题内容: 什么是效用全球关键字? 有什么理由偏爱一种方法而不是另一种方法? 安全? 性能? 还要别的吗? 方法1: 方法2: 什么时候使用有意义? 对我来说,这似乎很危险 ……但可能只是缺乏知识。我对 文档化的 技术原因感兴趣(例如,带有示例代码,链接到文档…)。 提前致谢! 赏金 这是关于该主题的一个很好的一般性问题,我(@Gordon)会提供赏金以获取其他答案。您的答案是否与我的意见一致或给

  • 问题内容: Goetz的Java Concurrency in Practice ,第41页,提到了在构造过程中如何逃避引用。“请勿执行此操作”示例: 这是通过引用封闭实例的事实进行的“转义” 。可以通过使用静态工厂方法(首先构造普通对象,然后注册侦听器)而不是公共构造函数(完成所有工作)来解决这种情况。这本书继续说: 从其构造函数中发布对象可能会发布不完整构造的对象。这是真实的 ,即使是公布在构