Пример #1
0
        private IEnumerable <IWordSimilarityNode> OrderPossibilities(string input, int maxResults, ICollection <string> possibleResults)
        {
            //If we only have a small amount, we can sort them all.
            if (possibleResults.Count <= maxResults)
            {
                var queue = new WordQueue(possibleResults.Count);
                foreach (var possibleResult in possibleResults)
                {
                    queue.Enqueue(possibleResult, StringSimilarityProvider.Similarity(input, possibleResult));
                }

                return(queue.OrderByDescending(x => x.Similarity));
            }

            var max = Math.Min(possibleResults.Count, maxResults);

            //With a large number of words to evaluate, but we only care about the top matches.
            //Instead of sorting a large number of elements at the end, we use a queue and restrict it's running size to max.
            //This queue will do that faster than other container types.
            //SortedList.RemoveAt is O(n)
            //SortedDictionary/SortedSet.ElementAt is O(n)
            var likelyWordsQueue = new WordQueue(max);

            foreach (var word in possibleResults)
            {
                var jw = StringSimilarityProvider.Similarity(input, word);
                likelyWordsQueue.Enqueue(word, jw);
            }

            return(likelyWordsQueue.OrderByDescending(x => x.Similarity));
        }