PageRenderTime 183ms CodeModel.GetById 141ms app.highlight 6ms RepoModel.GetById 27ms app.codeStats 7ms

/src/LinFu.Finders/FinderExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 152 lines | 66 code | 13 blank | 73 comment | 3 complexity | 162e04f3ceb4731e20a7973c0eefc156 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using LinFu.Finders.Interfaces;
  4
  5namespace LinFu.Finders
  6{
  7    /// <summary>
  8    ///     A class that adds fuzzy search support to <see cref="IList{T}" /> instances.
  9    /// </summary>
 10    public static class FinderExtensions
 11    {
 12        /// <summary>
 13        ///     Applies a criteria to the <paramref name="list" /> of
 14        ///     fuzzy items.
 15        /// </summary>
 16        /// <typeparam name="TItem">The type of item to test.</typeparam>
 17        /// <param name="list">
 18        ///     The list of <see cref="IFuzzyItem{T}" /> instances that represent a single test case in a fuzzy
 19        ///     search.
 20        /// </param>
 21        /// <param name="criteria">The criteria to test against each item in the list.</param>
 22        public static void AddCriteria<TItem>(this IList<IFuzzyItem<TItem>> list, ICriteria<TItem> criteria)
 23        {
 24            foreach (var item in list)
 25            {
 26                if (item == null)
 27                    continue;
 28
 29                item.Test(criteria);
 30            }
 31        }
 32
 33        /// <summary>
 34        ///     Applies a criteria to the <paramref name="list" /> of
 35        ///     fuzzy items using the given <paramref name="predicate" />.
 36        /// </summary>
 37        /// <typeparam name="TItem">The type of item to test.</typeparam>
 38        /// <param name="list">
 39        ///     The list of <see cref="IFuzzyItem{T}" /> instances that represent a single test case in a fuzzy
 40        ///     search.
 41        /// </param>
 42        /// <param name="predicate">The condition that will be used to test the target item.</param>
 43        public static void AddCriteria<TItem>(this IList<IFuzzyItem<TItem>> list, Func<TItem, bool> predicate)
 44        {
 45            list.AddCriteria(predicate, CriteriaType.Standard);
 46        }
 47
 48        /// <summary>
 49        ///     Applies a criteria to the <paramref name="list" /> of
 50        ///     fuzzy items using the given <paramref name="predicate" />.
 51        /// </summary>
 52        /// <typeparam name="TItem">The type of item to test.</typeparam>
 53        /// <param name="list">
 54        ///     The list of <see cref="IFuzzyItem{T}" /> instances that represent a single test case in a fuzzy
 55        ///     search.
 56        /// </param>
 57        /// <param name="predicate">The condition that will be used to test the target item.</param>
 58        /// <param name="criteriaType">The <see cref="CriteriaType" /> to associate with the predicate.</param>
 59        public static void AddCriteria<TItem>(this IList<IFuzzyItem<TItem>> list, Func<TItem, bool> predicate,
 60            CriteriaType criteriaType)
 61        {
 62            const int defaultWeight = 1;
 63            list.AddCriteria(predicate, criteriaType, defaultWeight);
 64        }
 65
 66        /// <summary>
 67        ///     Applies a criteria to the <paramref name="list" /> of
 68        ///     fuzzy items using the given <paramref name="predicate" />.
 69        /// </summary>
 70        /// <typeparam name="TItem">The type of item to test.</typeparam>
 71        /// <param name="list">
 72        ///     The list of <see cref="IFuzzyItem{T}" /> instances that represent a single test case in a fuzzy
 73        ///     search.
 74        /// </param>
 75        /// <param name="predicate">The condition that will be used to test the target item.</param>
 76        /// <param name="criteriaType">The <see cref="CriteriaType" /> to associate with the predicate.</param>
 77        /// <param name="weight">
 78        ///     The weight of the predicate value expressed in the number of tests that will be counted
 79        ///     for/against the target item as a result of the predicate.
 80        /// </param>
 81        public static void AddCriteria<TItem>(this IList<IFuzzyItem<TItem>> list, Func<TItem, bool> predicate,
 82            CriteriaType criteriaType, int weight)
 83        {
 84            var criteria = new Criteria<TItem>
 85            {
 86                Predicate = predicate,
 87                Weight = weight,
 88                Type = criteriaType
 89            };
 90
 91            list.AddCriteria(criteria);
 92        }
 93
 94        /// <summary>
 95        ///     Adds an item to a fuzzy list.
 96        /// </summary>
 97        /// <typeparam name="T">The type of the item being added.</typeparam>
 98        /// <param name="list">The fuzzy list that will contain the new item.</param>
 99        /// <param name="item">The item being added.</param>
100        public static void Add<T>(this IList<IFuzzyItem<T>> list, T item)
101        {
102            list.Add(new FuzzyItem<T>(item));
103        }
104
105        /// <summary>
106        ///     Returns the FuzzyItem with the highest confidence score in a given
107        ///     <see cref="IFuzzyItem{T}" /> list.
108        /// </summary>
109        /// <typeparam name="TItem">The type of item being compared.</typeparam>
110        /// <param name="list">The fuzzy list that contains the list of possible matches.</param>
111        /// <returns>The item with the highest match.</returns>
112        public static IFuzzyItem<TItem> BestMatch<TItem>(this IList<IFuzzyItem<TItem>> list)
113        {
114            double bestScore = 0;
115            IFuzzyItem<TItem> bestMatch = null;
116            foreach (var item in list)
117            {
118                if (item.Confidence <= bestScore)
119                    continue;
120
121                bestMatch = item;
122                bestScore = item.Confidence;
123            }
124
125            return bestMatch;
126        }
127
128        /// <summary>
129        ///     Resets the scores of all fuzzy items in the current list.
130        /// </summary>
131        /// <typeparam name="TItem">The target item type.</typeparam>
132        /// <param name="list">The fuzzy list itself.</param>
133        public static void Reset<TItem>(this IList<IFuzzyItem<TItem>> list)
134        {
135            foreach (var item in list) item.Reset();
136        }
137
138        /// <summary>
139        ///     Converts a list into a list of <see cref="IFuzzyItem{T}" /> objects.
140        /// </summary>
141        /// <typeparam name="TItem">The item type will be used in the fuzzy search.</typeparam>
142        /// <param name="items">The target list to be converted.</param>
143        /// <returns>A fuzzy list containing the elements from the given list.</returns>
144        public static IList<IFuzzyItem<TItem>> AsFuzzyList<TItem>(this IEnumerable<TItem> items)
145        {
146            var result = new List<IFuzzyItem<TItem>>();
147            foreach (var item in items) result.Add(item);
148
149            return result;
150        }
151    }
152}