/// <summary> /// S(A,B)=LCS(A,B)/(LD(A,B)+LCS(A,B)) /// </summary> /// <param name="strA"></param> /// <param name="strB"></param> /// <returns></returns> public static double SimilarityBy_LD_LCS(string strA, string strB) { int distance = LevenshteinDistance.CalcDistance(strA, strB); string lcs = LCS.MatchLongest(strA, strB); if (distance == 0) { return(1); } int lcsLength = lcs == null ? 0 : lcs.Length; if (lcsLength == 0) { return(0); } return((double)lcsLength / (lcsLength + distance)); }
public static void RunTest() { while (true) { Console.Write("string A:"); string strA = Console.ReadLine().Trim(); if (strA.Equals("q", StringComparison.OrdinalIgnoreCase)) { break; } Console.Write("string B:"); string strB = Console.ReadLine().Trim(); if (strB.Equals("q", StringComparison.OrdinalIgnoreCase)) { break; } int times = 10000; string dis = null; string disV = null; var dt1 = DateTime.Now; for (int i = 0; i < times; i++) { dis = LCS.MatchLongestMatrix(strA, strB); } var dt2 = DateTime.Now; for (int i = 0; i < times; i++) { disV = LCS.MatchLongest(strA, strB); } var dt3 = DateTime.Now; Console.WriteLine("length:{2}-{3},The longest is:{0},time:{1}", dis, (dt2 - dt1).TotalMilliseconds, strA.Length, strB.Length); Console.WriteLine("length:{2}-{3},The longest is:{0},time:{1}", disV, (dt3 - dt2).TotalMilliseconds, strA.Length, strB.Length); Console.WriteLine(); } }