繁体   English   中英

提前 C# 字符串比较

[英]advance C# string comparison

.Net中是否有任何class(函数)可以做到这一点:

if s1 = " I have a black car" and s2 = "I have a car that is small";
int matchingProcentage = matchingFunction(s1,s2);
s1 = " I have a black car" and s2 = "I have a car that is small";
int matchingProcentage = matchingFunction(s1,s2);

  matchingProcentage == 70% <-- just as an example value :)

这是一个很好的方法!

莱文斯坦距离

像下面这样的 function 应该可以工作,它是仓促编写的,所以请随意更改:

用法:

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());
     }

}

您可以使用Levenshtein 距离算法

使用在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);
    }

希望这可以帮助。

不,没有。 你必须实现你自己的。

只是一个建议,但是您能否同时获取两个字符串并比较字符并根据匹配字符的数量定义百分比?

尝试这个:

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM