C# - 比较字符串相似 [英] C# - Compare String Similarity
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/83777/are-there-any-fuzzy-search-or-string-similarity-functions-libraries-written-for-c">Are有任何模糊搜索或字符串相似函数库编写的C#?
什么是比较两个字符串,看看他们是如何的相似的最好方法是什么?
例如:
我的字符串
我的字符串,多余的话
或
我的字符串
我稍有不同的字符串
我在找的是确定相似每对中的第一和第二串。我想得分比较,如果字符串是相似的是,我会考虑他们的匹配对。
有没有好的办法做到这一点在C#?
静态类LevenshteinDistance
{
公共静态INT计算(字符串s,串T)
{
如果(string.IsNullOrEmpty(多个))
{
如果(string.IsNullOrEmpty(t))的
返回0;
返回t.Length;
}
如果(string.IsNullOrEmpty(t))的
{
返回s.Length;
}
INT N = s.Length;
INT M = t.Length;
INT [,] D =新INT [n + 1个,M + 1];
//初始化该表的顶部和右侧为0,1,2,...
的for(int i = 0; I&LT; = N,D [我,0] =我++);
对于(INT J = 1; J&LT; =米; D [0,D] = J ++);
的for(int i = 1; I&LT; = N;我++)
{
对于(INT J = 1; J&LT; = M,J ++)
{
INT成本=(T [J - 1] == S [我 - 1])? 0:1;
INT分1 = D [我 - 1,J] + 1;
INT MIN2 = D [I,J - 1] + 1;
INT MIN3 = D [我 - 1,J - 1] +费用;
D [I,J] = Math.Min(Math.Min(MIN1,MIN2),MIN3);
}
}
返回D [N,M]。
}
}
Possible Duplicate:
Are there any Fuzzy Search or String Similarity Functions libraries written for C#?
What is the best way to compare 2 strings to see how similar they are?
Examples:
My String
My String With Extra Words
Or
My String
My Slightly Different String
What I am looking for is to determine how similar the first and second string in each pair is. I would like to score the comparison and if the strings are similar enough, I would consider them a matching pair.
Is there a good way to do this in C#?
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];
}
}
这篇关于C# - 比较字符串相似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!