Beispiel #1
0
        public List <DistanceSearchResult <T> > FindByDistance(string key, int distanceLimit = int.MaxValue)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var sortedKey = AlphabetSort(key);

            List <DistanceSearchResult <T> > wordsByDistance = new List <DistanceSearchResult <T> >();

            foreach (var comparisonKey in _wordSet.Keys)
            {
                int distance = Levenshtein.Calculate(sortedKey, comparisonKey.Value);

                if (distance < distanceLimit)
                {
                    var results = _wordSet[comparisonKey].Select(item => new DistanceSearchResult <T>(distance, comparisonKey.Id, item));

                    wordsByDistance.AddRange(results);
                }
            }

            return(wordsByDistance.OrderBy(x => x.Distance).ToList());
        }
        public List <DistanceSearchResult <T> > FindByDistance(string key, int distanceLimit = int.MaxValue)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var normalizedKey = key.ToLower();

            List <DistanceSearchResult <T> > wordsByDistance = new List <DistanceSearchResult <T> >();

            foreach (var knownKey in _wordSet.Keys)
            {
                int distance = Levenshtein.Calculate(normalizedKey, knownKey.Value);

                if (distance < distanceLimit)
                {
                    wordsByDistance.Add(new DistanceSearchResult <T>(distance, knownKey.Id, _wordSet[knownKey]));
                }
            }

            return(wordsByDistance.OrderBy(x => x.Distance).ToList());
        }