/// <summary> /// Purges the invalid results when SearchOptions is AllWords. /// </summary> /// <param name="results">The results to purge.</param> /// <param name="queryWords">The query words.</param> /// <returns>The relevance value of the removed matches.</returns> public static float PurgeResultsForAllWords(SearchResultCollection results, string[] queryWords) { // Remove results that do not contain all the searched words float relevanceToRemove = 0; List <SearchResult> toRemove = new List <SearchResult>(); foreach (SearchResult r in results) { if (r.Matches.Count < queryWords.Length) { toRemove.Add(r); } else { foreach (string w in queryWords) { if (!r.Matches.Contains(w)) { toRemove.Add(r); break; } } } } foreach (SearchResult r in toRemove) { results.Remove(r); relevanceToRemove += r.Relevance.Value; } return(relevanceToRemove); }
/// <summary> /// Purges the invalid results when SearchOptions is ExactPhrase. /// </summary> /// <param name="results">The results to purge.</param> /// <param name="queryWords">The query words.</param> /// <returns>The relevance value of the removed matches.</returns> public static float PurgeResultsForExactPhrase(SearchResultCollection results, string[] queryWords) { // Remove results that do not contain the exact phrase float relevanceToRemove = 0; List <SearchResult> toRemove = new List <SearchResult>(); foreach (SearchResult r in results) { // Shortcut if (r.Matches.Count < queryWords.Length) { toRemove.Add(r); } else { // Verify that all matches are in the same order as in the query // and that their indices make up contiguous words, // re-iterating from every word in the result, for example: // query = 'repeated content', result = 'content repeated content' // result must be tested with 'content repeated' (failing) and with 'repeated content' (succeeding) int maxTestShift = 0; if (queryWords.Length < r.Matches.Count) { maxTestShift = r.Matches.Count - queryWords.Length; } bool sequenceFound = false; for (int shift = 0; shift <= maxTestShift; shift++) { int firstWordIndex = r.Matches[shift].WordIndex; bool allOk = true; for (int i = 0; i < queryWords.Length; i++) { if (queryWords[i] != r.Matches[i + shift].Text.ToLowerInvariant() || r.Matches[i + shift].WordIndex != firstWordIndex + i) { allOk = false; break; } } if (allOk) { sequenceFound = true; break; } } if (!sequenceFound) { toRemove.Add(r); } } } foreach (SearchResult r in toRemove) { results.Remove(r); relevanceToRemove += r.Relevance.Value; } return(relevanceToRemove); }