可视化 2+ 中的差异纯文本字符串

发布于 2024-10-31 18:38:20 字数 134 浏览 0 评论 0原文

我正在寻找一种好方法来显示两个或多个文本(并排)之间的差异。我不需要能够创建补丁或类似的东西——只需逐行显示差异即可。

是否有任何现有的开源 C# 库可以执行类似的操作?如果没有,是否存在适用于 2 个以上字符串的 diff 算法的变体?

I'm looking for a good way to show differences between 2 or more texts (side by side). I don't need to be able to create a patch or anything like that--just show the differences line by line.

Are there any existing open-source C# libraries that do something like this? If not, is there a variation of a diff algorithm that works with more than 2 strings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2024-11-07 18:38:20

以下是 C# 中 Levenshtein Distance 算法的两个实现

链接 1
链接2

结果越大,差异越大。

编辑:复制代码,以防链接失效以供将来使用

示例 1:

using System;

/// <summary>
/// Contains approximate string matching
/// </summary>
static class LevenshteinDistance
{
    /// <summary>
    /// Compute the distance between two strings.
    /// </summary>
    public static int Compute(string s, string t)
    {
    int n = s.Length;
    int m = t.Length;
    int[,] d = new int[n + 1, m + 1];

    // Step 1
    if (n == 0)
    {
        return m;
    }

    if (m == 0)
    {
        return n;
    }

    // Step 2
    for (int i = 0; i <= n; d[i, 0] = i++)
    {
    }

    for (int j = 0; j <= m; d[0, j] = j++)
    {
    }

    // Step 3
    for (int i = 1; i <= n; i++)
    {
        //Step 4
        for (int j = 1; j <= m; j++)
        {
        // Step 5
        int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

        // Step 6
        d[i, j] = Math.Min(
            Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
            d[i - 1, j - 1] + cost);
        }
    }
    // Step 7
    return d[n, m];
    }
}

class Program
{
    static void Main()
    {
    Console.WriteLine(LevenshteinDistance.Compute("aunt", "ant"));
    Console.WriteLine(LevenshteinDistance.Compute("Sam", "Samantha"));
    Console.WriteLine(LevenshteinDistance.Compute("flomax", "volmax"));
    }
}

示例 2:

public class Distance {

/// <summary>
/// Compute Levenshtein distance
/// </summary>
/// <param name="s">String 1</param>
/// <param name="t">String 2</param>
/// <returns>Distance between the two strings.
/// The larger the number, the bigger the difference.
/// </returns>

  public int LD (string s, string t) {

  int n = s.Length; //length of s

  int m = t.Length; //length of t

  int[,] d = new int[n + 1, m + 1]; // matrix

  int cost; // cost

    // Step 1

    if(n == 0) return m;

    if(m == 0) return n;

    // Step 2

    for(int i = 0; i <= n; d[i, 0] = i++);

    for(int j = 0; j <= m; d[0, j] = j++);

    // Step 3

    for(int i = 1; i <= n;i++) {

      //Step 4

      for(int j = 1; j <= m;j++) {

        // Step 5

        cost = (t.Substring(j - 1, 1) == s.Substring(i - 1, 1) ? 0 : 1);

        // Step 6

        d[i, j] = System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                  d[i - 1, j - 1] + cost);

      }

    }


    // Step 7


    return d[n, m];

  }

Here are two implementations of the Levenshtein Distance algorithm in C#

Link 1
Link 2

The larger the result, the bigger the difference.

Edit: Copying code in case links go dead for future use

Example 1:

using System;

/// <summary>
/// Contains approximate string matching
/// </summary>
static class LevenshteinDistance
{
    /// <summary>
    /// Compute the distance between two strings.
    /// </summary>
    public static int Compute(string s, string t)
    {
    int n = s.Length;
    int m = t.Length;
    int[,] d = new int[n + 1, m + 1];

    // Step 1
    if (n == 0)
    {
        return m;
    }

    if (m == 0)
    {
        return n;
    }

    // Step 2
    for (int i = 0; i <= n; d[i, 0] = i++)
    {
    }

    for (int j = 0; j <= m; d[0, j] = j++)
    {
    }

    // Step 3
    for (int i = 1; i <= n; i++)
    {
        //Step 4
        for (int j = 1; j <= m; j++)
        {
        // Step 5
        int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

        // Step 6
        d[i, j] = Math.Min(
            Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
            d[i - 1, j - 1] + cost);
        }
    }
    // Step 7
    return d[n, m];
    }
}

class Program
{
    static void Main()
    {
    Console.WriteLine(LevenshteinDistance.Compute("aunt", "ant"));
    Console.WriteLine(LevenshteinDistance.Compute("Sam", "Samantha"));
    Console.WriteLine(LevenshteinDistance.Compute("flomax", "volmax"));
    }
}

Example 2:

public class Distance {

/// <summary>
/// Compute Levenshtein distance
/// </summary>
/// <param name="s">String 1</param>
/// <param name="t">String 2</param>
/// <returns>Distance between the two strings.
/// The larger the number, the bigger the difference.
/// </returns>

  public int LD (string s, string t) {

  int n = s.Length; //length of s

  int m = t.Length; //length of t

  int[,] d = new int[n + 1, m + 1]; // matrix

  int cost; // cost

    // Step 1

    if(n == 0) return m;

    if(m == 0) return n;

    // Step 2

    for(int i = 0; i <= n; d[i, 0] = i++);

    for(int j = 0; j <= m; d[0, j] = j++);

    // Step 3

    for(int i = 1; i <= n;i++) {

      //Step 4

      for(int j = 1; j <= m;j++) {

        // Step 5

        cost = (t.Substring(j - 1, 1) == s.Substring(i - 1, 1) ? 0 : 1);

        // Step 6

        d[i, j] = System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                  d[i - 1, j - 1] + cost);

      }

    }


    // Step 7


    return d[n, m];

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文