基本信息
源码名称:C# 字符串文本相似度比较的两种算法 附完整源码
源码大小:0.06M
文件格式:.zip
开发语言:C#
更新时间:2013-05-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
算法一:
public class LevenshteinDistance
{
private static LevenshteinDistance _instance=null;
public static LevenshteinDistance Instance
{
get
{
if (_instance == null)
{
return new LevenshteinDistance();
}
return _instance;
}
}
/// <summary>
/// 取最小的一位数
/// </summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <param name="third"></param>
/// <returns></returns>
public int LowerOfThree(int first, int second, int third)
{
int min = first;
if (second < min)
min = second;
if (third < min)
min = third;
return min;
}
public int Levenshtein_Distance(string str1, string str2)
{
int[,] Matrix;
int n=str1.Length;
int m=str2.Length;
int temp = 0;
char ch1;
char ch2;
int i = 0;
int j = 0;
if (n ==0)
{
return m;
}
if (m == 0)
{
return n;
}
Matrix=new int[n 1,m 1];
for (i = 0; i <= n; i )
{
//初始化第一列
Matrix[i,0] = i;
}
for (j = 0; j <= m; j )
{
//初始化第一行
Matrix[0, j] = j;
}
for (i = 1; i <= n; i )
{
ch1 = str1[i-1];
for (j = 1; j <= m; j )
{
ch2 = str2[j-1];
if (ch1.Equals(ch2))
{
temp = 0;
}
else
{
temp = 1;
}
Matrix[i,j] = LowerOfThree(Matrix[i - 1,j] 1, Matrix[i,j - 1] 1, Matrix[i - 1,j - 1] temp);
}
}
for (i = 0; i <= n; i )
{
for (j = 0; j <= m; j )
{
Console.Write(" {0} ", Matrix[i, j]);
}
Console.WriteLine("");
}
return Matrix[n, m];
}
/// <summary>
/// 计算字符串相似度
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public decimal LevenshteinDistancePercent(string str1,string str2)
{
int maxLenth = str1.Length > str2.Length ? str1.Length : str2.Length;
int val = Levenshtein_Distance(str1, str2);
return 1 - (decimal)val / maxLenth;
}
}
使用代码:
this.lbResult.Text = (LevenshteinDistance.Instance.LevenshteinDistancePercent(this.textBox1.Text, this.textBox2.Text) * 100).ToString();
算法二:
public string get_semblance_By_2words(string word1, string word2)
{
int re = 0;
int maxLength;
int i, l;
List<string> tb1 = new List<string>();
List<string> tb2 = new List<string>();
i = 0;
l = 1;
maxLength = word1.Length;
if (word1.Length < word2.Length)
maxLength = word2.Length;
while (l <= word1.Length)
{
while (i < word1.Length - 1)
{
if (i l > word1.Length)
break;
tb1.Add(word1.Substring(i, l));
i ;
}
i = 0;
l ;
}
i = 0;
l = 1;
while (l <= word2.Length)
{
while (i < word2.Length - 1)
{
if (i l > word2.Length)
break;
tb2.Add(word2.Substring(i, l));
i ;
}
i = 0;
l ;
}
foreach (string subStr in tb1)
{
int tempRe = 0;
if (tb2.Contains(subStr))
{
tempRe = subStr.Length * 100 / maxLength;
if (tempRe > re)
re = tempRe;
if (tempRe == 100)
break;
}
}
return re.ToString() "%";
}