Determine the similarity between two characters and determine the two characters
# Region determine the similarity between two characters
// Formula: l = q/(q + r + s );
Private string Similarity (string a, string B)
{
String a1 = "";
String b1 = "";
// Eliminate duplicates
For (int I = 0; I <a. Length; I ++)
{
String emtpy = a. Substring (I, 1). ToString ();
If (a1.Length = 0)
{
A1 = emtpy;
}
If (a1.IndexOf (emtpy, 0) <0)
{
A1 + = emtpy;
}
}
// Eliminate duplicates
For (int I = 0; I <B. Length; I ++)
{
String emtpy = B. Substring (I, 1). ToString ();
If (b1.Length = 0)
{
B1 = emtpy;
}
If (b1.IndexOf (emtpy, 0) <0)
{
B1 + = emtpy;
}
}
// Calculate similarity
If (a1.Length> = b1.Length)
{
Int q = 0;
For (int I = 0; I <b1.Length; I ++)
{
For (int j = 0; j <a1.Length; j ++)
{
If (b1.Substring (I, 1). ToString () = a1.Substring (j, 1). ToString ())
{
Q + = 1;
}
}
}
System. Globalization. NumberFormatInfo provider = new System. Globalization. NumberFormatInfo ();
Provider. PercentDecimalDigits = 2; // number of decimal places reserved.
Provider. PercentPositivePattern = 1; // Where is the percent sign.
Double result = (double) q/(a1.Length + b1.Length-q); // be sure to use the double type.
Return result. ToString ("P", provider );
}
Else
{
Int q = 0;
For (int I = 0; I <a1.Length; I ++)
{
For (int j = 0; j <b1.Length; j ++)
{
If (a1.Substring (I, 1). ToString () = b1.Substring (j, 1). ToString ())
{
Q + = 1;
}
}
}
System. Globalization. NumberFormatInfo provider = new System. Globalization. NumberFormatInfo ();
Provider. PercentDecimalDigits = 2; // number of decimal places reserved.
Provider. PercentPositivePattern = 1; // Where is the percent sign.
Double result = (double) q/(a1.Length + b1.Length-q); // be sure to use the double type.
Return result. ToString ("P", provider );
}
}
# Endregion
Load http://blog.sina.com.cn/s/blog_5f254b8a0100t4hy.html
Reference: http://www.360doc.com/content/09/0201/10/96202_2430832.shtml