Csharp/C#教程:C# – 比较字符串相似性分享


C# – 比较字符串相似性

可能重复:
是否有为C#编写的模糊搜索或字符串相似性函数库?

比较2个字符串以查看它们有多相似的最佳方法是什么?

例子:

My String My String With Extra Words 

要么

 My String My Slightly Different String 

我要找的是确定每对中第一个和第二个字符串的相似程度。 我想对比较得分,如果字符串足够相似,我会认为它们是匹配对。

有没有一种很好的方法在C#中做到这一点?

上述就是C#学习教程:C# – 比较字符串相似性分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 static class LevenshteinDistance { public static int Compute(string s, string t) { if (string.IsNullOrEmpty(s)) { if (string.IsNullOrEmpty(t)) return 0; return t.Length; } if (string.IsNullOrEmpty(t)) { return s.Length; } int n = s.Length; int m = t.Length; int[,] d = new int[n + 1, m + 1]; // initialize the top and right of the table to 0, 1, 2, ... for (int i = 0; i <= n; d[i, 0] = i++); for (int j = 1; j <= m; d[0, j] = j++); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int cost = (t[j - 1] == s[i - 1]) ? 0 : 1; int min1 = d[i - 1, j] + 1; int min2 = d[i, j - 1] + 1; int min3 = d[i - 1, j - 1] + cost; d[i, j] = Math.Min(Math.Min(min1, min2), min3); } } return d[n, m]; } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1012761.html

(0)
上一篇 2021年12月30日
下一篇 2021年12月30日

精彩推荐