Example #1
0
        public void CompareStringMetrics(string s1, string s2)
        {
            var consine      = new F23.StringSimilarity.Cosine().Distance(s1, s2);
            var damerau      = new F23.StringSimilarity.Damerau().Distance(s1, s2);
            var jaccard      = new F23.StringSimilarity.Jaccard().Distance(s1, s2);
            var jarodwinkler = new F23.StringSimilarity.JaroWinkler().Distance(s1, s2);
            var levenshtein  = new F23.StringSimilarity.Levenshtein().Distance(s1, s2);

            var lcs  = new F23.StringSimilarity.LongestCommonSubsequence().Distance(s1, s2);
            var mlcs = new F23.StringSimilarity.MetricLCS().Distance(s1, s2);
            var ng   = new F23.StringSimilarity.NGram().Distance(s1, s2);
            var nl   = new F23.StringSimilarity.NormalizedLevenshtein().Distance(s1, s2);
            var osa  = new F23.StringSimilarity.OptimalStringAlignment().Distance(s1, s2);
            var qg   = new F23.StringSimilarity.QGram().Distance(s1, s2);
            //var ng = new F23.StringSimilarity.ShingleBased
            var sd = new F23.StringSimilarity.SorensenDice().Distance(s1, s2);

            //var wl = new F23.StringSimilarity.WeightedLevenshtein().Distance

            Console.WriteLine("consine score = {0}", consine.ToString());
            Console.WriteLine("damerau score = {0}", damerau.ToString());
            Console.WriteLine("jaccard score = {0}", jaccard.ToString());
            Console.WriteLine("jarodwinkler score = {0}", jarodwinkler.ToString());
            Console.WriteLine("levenshtein score = {0}", levenshtein.ToString());

            Console.WriteLine("lcs score = {0}", lcs.ToString());
            Console.WriteLine("mlcs score = {0}", mlcs.ToString());
            Console.WriteLine("ng score = {0}", ng.ToString());
            Console.WriteLine("nl score = {0}", nl.ToString());
            Console.WriteLine("osa score = {0}", osa.ToString());
            Console.WriteLine("qg score = {0}", qg.ToString());
            //Console.WriteLine("ng score = {0}",ng.ToString());
            Console.WriteLine("sd score = {0}", sd.ToString());
            //Console.WriteLine("wl score = {0}",wl.ToString());
        }
        public double CalculateSimilarity(string source, string target)
        {
            //F23.StringSimilarity.Cosine cosine = new F23.StringSimilarity.Cosine();
            //F23.StringSimilarity.Levenshtein levenshtein = new F23.StringSimilarity.Levenshtein();
            F23.StringSimilarity.NGram ngram = new F23.StringSimilarity.NGram();
            //F23.StringSimilarity.JaroWinkler jaroWinkler = new F23.StringSimilarity.JaroWinkler();
            //double cosineRes = cosine.Distance(source, target);
            //double levenshteinRes = (1.0 - ((double)levenshtein.Distance(source, target) / (double)Math.Max(source.Length, target.Length)));
            double ngramRes = 1 - ngram.Distance(source, target);

            //double jaroWinklerRes = 1- jaroWinkler.Distance(source, target);
            if ((source.Length * 2 <= target.Length || target.Length * 2 <= source.Length) &&
                (source.Contains(target) || target.Contains(source)))
            {
                return(2.5);
            }
            if ((source.Length * 2 <= target.Length || target.Length * 2 <= source.Length) &&
                (!source.Contains(target) || !target.Contains(source)))
            {
                return(0);
            }

            //if ((source == null) || (target == null)) return 0.0;
            //if ((source.Length == 0) || (target.Length == 0)) return 0.0;
            //if (source == target) return 1.0;

            //int stepsToSame = ComputeLevenshteinDistance(source, target);
            if (source.Length == 1 && target == source || target.Length == 1 && target == source)
            {
                return(1);
            }
            if (source.Length == 1 && target.Length > 1 || source.Length == 1 && target != source ||
                target.Length == 1 && source.Length > 1 || target.Length == 1 && target != source)
            {
                return(0);
            }
            //return (1.0 - ((double)stepsToSame / (double)Math.Max(source.Length, target.Length)));
            return(ngramRes);
        }