高级C#字符串比较

4
有没有在.Net中可以实现这个功能的类(函数):
如果 s1 =“我有一辆黑色汽车”和s2 =“我有一辆小汽车”; int matchingProcentage = matchingFunction(s1,s2);
  matchingProcentage == 70% <-- just as an example value :)

不是的。但应该很容易:只需比较字符,当它们不同时就可以得出百分比了。 - Tomas Voracek
5
“123456790”与“234567890”这两个字符串有多少相似度?是0%还是90%或其他数字?没有定义“匹配”实际上意味着什么,因此没有答案,这很可能是为什么没有内置方法的原因。 - dlev
2
@dlev 这取决于问题提出者如何定义它。 - Tomas Voracek
1
@Tomas 对,他们还没有做到。 - dlev
7个回答

12

+1,这是你应该使用的。我在下面发布了我的“修订”代码,它返回一个百分号而不是一个数字。 - TheGateKeeper

6
以下类似的函数应该可以使用,它是匆忙编写的,所以可以随意更改: 用法:
GetStringPercentage("I have a black car", "I have a car that is small");

方法:

public static decimal GetStringPercentage(string s1, string s2)
{
     decimal matches = 0.0m;
     List<string> s1Split = s1.Split(' ').ToList();
     List<string> s2Split = s2.Split(' ').ToList();

     if (s1Split.Count() > s2Split.Count())
     {
         foreach (string s in s1Split)
             if (s2Split.Any(st => st == s))
                 matches++;

             return (matches / s1Split.Count());
     }
     else
     {
         foreach (string s in s2Split)
             if (s1Split.Any(st => st == s))
                  matches++;

         return (matches / s2Split.Count());
     }

}

1

使用在http://www.dotnetperls.com/levenshtein找到的代码作为基础,我修改了它,使其返回一个百分比而不是一个数字:

    public static int Compute(string word1, string word2)
    {
        int n = word1.Length;
        int m = word2.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 = (word2[j - 1] == word1[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
        decimal changesRequired = d[n, m];

        //Find the longest word and calculate the percentage equality
        if (word1.Length > word2.Length)
            return Convert.ToInt32(100 - (changesRequired / word1.Length) * 100);
        else
            return Convert.ToInt32(100 - (changesRequired / word2.Length) * 100);
    }

希望这有所帮助。

1

0

试试这个:

public static int MatchingFunction(string s1, string s2, bool duplicate, bool keySensitive)
{

    if (!keySensitive)
    {
        s1 = s1.ToLower();
        s2 = s2.ToLower();
    }

    List<string> ls1 = null;
    s2 = s2.Trim();

    if (duplicate)
    {
        ls1 = s1.Trim().Split(' ').ToList();
    }
    else
    {
        ls1 = new List<string>();
        string[] as1 = s1.Trim().Split(' ');
        foreach (string s in as1)
            if (!ls1.Contains(s))
                ls1.Add(s);

        string[] as2 = s2.Trim().Split(' ');
        s2 = string.Empty;
        foreach (string s in as2)
            if (!s2.Contains(s))
                s2 = string.Format("{0} {1}", s2, s);
    }


    int has = 0;
    s2 = string.Format("@{0}@", s2.Replace(' ', '@');
    foreach (string s in ls1)
        has += s2.Contains(string.Format("@{0}@", s)) ? 1 : 0;

    return (has * 100 / ls1.Count());
}


string s1 =  " I have a black car";
string s2 = "I have a car that is small";

int p = MatchingFunction(s1, s2, false, false);

0

没有,你需要自己实现。


0
只是一个建议,您能否将两个字符串逐个字符进行比较,并根据匹配字符的数量定义百分比?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接