Example #1
0
        public static double GetCosineDistanceByChar(this string str1, string str2)
        {
            if (str1 == null || str2 == null)
            {
                return(1);
            }
            if (str1.Equals(str2))
            {
                return(0);
            }

            var cs1 = str1.ToCharArray();
            var cs2 = str2.ToCharArray();

            IDictionary <char, double> freq1 = new SortedDictionary <char, double>();

            foreach (var c in cs1.Concat(cs2))
            {
                freq1.AddOrUpdateValue(c, 0);
            }
            IDictionary <char, double> freq2 = new SortedDictionary <char, double>(freq1);

            var groupedCs1 = cs1.GroupBy(c => c);

            foreach (var charGroup in groupedCs1)
            {
                freq1[charGroup.Key] = charGroup.Count();
            }

            var groupedCs2 = cs2.GroupBy(c => c);

            foreach (var charGroup in groupedCs2)
            {
                freq2[charGroup.Key] = charGroup.Count();
            }

            var distance = Distance.Cosine(freq1.Values.ToArray(), freq2.Values.ToArray());

            return(distance.Equals(double.NaN) ? 1 : distance);
        }