
算法一:
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() "%";
}
资源文件列表
Levenshtein/bin/Debug/Levenshtein.exe , 12800
Levenshtein/bin/Debug/Levenshtein.pdb , 30208
Levenshtein/bin/Debug/Levenshtein.vshost.exe , 22472
Levenshtein/bin/Debug/Levenshtein.vshost.exe.manifest , 490
Levenshtein/Form1.cs , 599
Levenshtein/Form1.Designer.cs , 4681
Levenshtein/Form1.resx , 5817
Levenshtein/Form2.cs , 2202
Levenshtein/Form2.Designer.cs , 4693
Levenshtein/Form2.resx , 5817
Levenshtein/Levenshtein.csproj , 4089
Levenshtein/Levenshtein.sln , 911
Levenshtein/Levenshtein.v11.suo , 35840
Levenshtein/LevenshteinDistance.cs , 3198
Levenshtein/obj/Debug/DesignTimeResolveAssemblyReferences.cache , 863
Levenshtein/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 6773
Levenshtein/obj/Debug/Levenshtein.csproj.FileListAbsolute.txt , 525
Levenshtein/obj/Debug/Levenshtein.csproj.GenerateResource.Cache , 1033
Levenshtein/obj/Debug/Levenshtein.exe , 12800
Levenshtein/obj/Debug/Levenshtein.Form1.resources , 180
Levenshtein/obj/Debug/Levenshtein.Form2.resources , 180
Levenshtein/obj/Debug/Levenshtein.pdb , 30208
Levenshtein/obj/Debug/Levenshtein.Properties.Resources.resources , 180
Levenshtein/Program.cs , 535
Levenshtein/Properties/AssemblyInfo.cs , 1360
Levenshtein/Properties/Resources.Designer.cs , 2874
Levenshtein/Properties/Resources.resx , 5612
Levenshtein/Properties/Settings.Designer.cs , 1098
Levenshtein/Properties/Settings.settings , 249
