public double GetMatchPercentage(string source, string target)
        {
            if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target))
            {
                return(0);
            }


            double matchingCharacters = 0;
            var    sourceStrings      = source.Split(' ');
            var    targetStrings      = target.Split(' ');

            var sourceIsLarger = source.Length >= target.Length;

            matchingCharacters =
                CalculateMatchingCharacters(matchingCharacters, sourceStrings, targetStrings, sourceIsLarger);

            var sourceWhiteSpaceCount = source.Count(char.IsWhiteSpace);
            var targetWhiteSpaceCount = target.Count(char.IsWhiteSpace);

            matchingCharacters += sourceIsLarger ? sourceWhiteSpaceCount : targetWhiteSpaceCount;

            var amount        = matchingCharacters / (sourceIsLarger ? source.Length : target.Length) * 100;
            var wrappedAmount = _parser?.GetMatchPercentage(source, target) ?? 0;

            return(amount > wrappedAmount ? amount : wrappedAmount);
        }
Example #2
0
        //https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C#
        public int GetDistance(string source, string target)
        {
            if (string.IsNullOrEmpty(source))
            {
                if (string.IsNullOrEmpty(target))
                {
                    return(0);
                }
                return(target.Length);
            }

            if (string.IsNullOrEmpty(target))
            {
                return(source.Length);
            }

            if (source.Length > target.Length)
            {
                var temp = target;
                target = source;
                source = temp;
            }

            var m        = target.Length;
            var n        = source.Length;
            var distance = new int[2, m + 1];

            // Initialize the distance matrix
            for (var j = 1; j <= m; j++)
            {
                distance[0, j] = j;
            }

            var currentRow = 0;

            for (var i = 1; i <= n; ++i)
            {
                currentRow = i & 1;
                distance[currentRow, 0] = i;
                var previousRow = currentRow ^ 1;
                for (var j = 1; j <= m; j++)
                {
                    var cost = target[j - 1] == source[i - 1] ? 0 : 1;
                    distance[currentRow, j] = Math.Min(Math.Min(
                                                           distance[previousRow, j] + 1,
                                                           distance[currentRow, j - 1] + 1),
                                                       distance[previousRow, j - 1] + cost);
                }
            }

            var distancing = distance[currentRow, m];

            if (distancing >= 20)
            {
                return(0);
            }
            var parsing = _parser?.GetMatchPercentage(source, target) ?? 0;

            return(distancing >= parsing ? distancing : Convert.ToInt32(parsing));
        }
Example #3
0
        public IEnumerable <IMatch> GetMatches <T1, T2>(IEnumerable <T1> sourceCollection,
                                                        IEnumerable <T2> targetCollection, Func <T1, string> sourceSelector, Func <T2, string> targetSelector,
                                                        IEqualityComparer <T1> distinctComparer = null)
        {
            var matches    = new List <Match>();
            var collection = targetCollection as T2[] ?? targetCollection.ToArray();

            foreach (var source in sourceCollection.Distinct(distinctComparer))
            {
                foreach (var target in collection)
                {
                    var sourceString = sourceSelector(source);
                    var targetString = targetSelector(target);
                    var percentage   = _matchParser.GetMatchPercentage(sourceString?.ToUpper(), targetString?.ToUpper());
                    matches.Add(new Match(sourceString, targetString, percentage));
                }
            }

            return(matches);
        }