/// <summary> /// Calculates the Dice Coefficient for two given strings. /// </summary> /// <remarks>This is a similarity metric based on bigrams that is calculated as follows: /// * D is Dice coefficient /// * SB is Shared Bigrams /// * TBg1 is total number of bigrams in Qgram1 /// * TBg2 is total number of bigrams in Qgram2 /// * D = (2SB)/(TBg1+TBg2) /// A good Dice Coefficient value would be a value greater than 0.33</remarks> /// <param name="pStr1">First string to compare</param> /// <param name="pStr2">Second string to compare</param> /// <returns>Dice Coefficient</returns> internal static double DiceCoefficient(String pStr1, String pStr2) { // faulty input parameters if (string.IsNullOrEmpty(pStr1) || string.IsNullOrEmpty(pStr1)) { return(0.0); } var bigram1 = Bigram.Parse(pStr1); var bigram2 = Bigram.Parse(pStr2); // calculate number of shared bigrams int sharedBigrams = 0; foreach (var s1 in bigram1) { foreach (var s2 in bigram2) { if (s1.Equals(s2)) { sharedBigrams++; } } } // calculate dice coefficient double dice = Convert.ToDouble(sharedBigrams * 2) / Convert.ToDouble(bigram1.Length + bigram2.Length); return(dice); }
/// <summary> /// Calculates the Dice Coefficient for two given strings. /// </summary> /// <remarks>This is a similarity metric based on bigrams that is calculated as follows: /// * D is Dice coefficient /// * SB is Shared Bigrams /// * TBg1 is total number of bigrams in Qgram1 /// * TBg2 is total number of bigrams in Qgram2 /// * D = (2SB)/(TBg1+TBg2) /// A good Dice Coefficient value would be a value greater than 0.33</remarks> /// <param name="pStr1">First string to compare</param> /// <param name="pStr2">Second string to compare</param> /// <returns>Dice Coefficient</returns> internal static double DiceCoefficient(string pStr1, string pStr2) { // faulty input parameters if (string.IsNullOrEmpty(pStr1) || string.IsNullOrEmpty(pStr1)) { return(0.0); } var bigram1 = Bigram.Parse(pStr1); var bigram2 = Bigram.Parse(pStr2); // calculate number of shared bigrams var sharedBigrams = bigram1.Sum(s1 => bigram2.Count(s1.Equals)); // calculate dice coefficient return(Convert.ToDouble(sharedBigrams * 2) / Convert.ToDouble(bigram1.Length + bigram2.Length)); }