- public class LDMaker//搞成一个类看起来专业,实际上就是
- //脱裤子放屁,这里使用来文史特距离算法
- //用于计算字符串之间的相似性
- {
- char[] str1;
- char[] str2;
- public LDMaker(string s1, string s2)
- {
- //替换掉 所有 数字 为固定数字 数字干扰 太严重
- //这里因人而异,在中文文章的匹配中,数字是干扰很严重
- 的,所以我在某些应用中把数字替换掉了。
- //原因是有的文章之间实际上相似性很高,但是故意在里面加一些数字
- 干扰该函数的执行,让机器看出来两篇文章很不同。一般不需要做如下
- 步骤
- s1=System.Text.RegularExpressions.Regex.Replace(s1,@"(\\d+)","1");
- s2 = System.Text.RegularExpressions.Regex.Replace(s2, @"(\\d+)", "1");
- str1 = s1.ToCharArray();
- str2 = s2.ToCharArray();
- }
- public int GetLD()这是莱文史特距离的算法实现
- {
- try
- {
- int m=str1.Length;
- int n=str2.Length;
- int[,] d = new int[m+1, n+1];
- for (int i = 0; i <= m ; i++)
- d[i, 0] = i;
- for (int i = 0; i <= n ; i++)
- d[0, i] = i;
- for (int i = 1; i <= m; i++)
- {
- for (int j = 1; j <= n; j++)
- {
- d[i,j] = d[i - 1,j - 1] + (str1[i - 1] == str2[j - 1] ? 0 : 1); //修改一个字符
- d[i,j] = Math.Min(d[i,j], d[i - 1,j] + 1);
- // 插入一个字符串
- d[i,j] = Math.Min(d[i,j], d[i,j - 1] + 1);
- //删除一个字符
- }
- }
- return d[m, n];
- } catch(//出错返回一个很大值
- {
- return 10000;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1010201513764.html
来源: http://www.codesnippet.cn/detail/1010201513764.html