public SearchQuality CompareTitleSuccess(List <String> queryTerms, String pageTitle) { SearchQuality deviationValue = SearchQuality.GOOD; pageTitle = Regex.Replace(pageTitle, commonWords, " ", RegexOptions.IgnoreCase); pageTitle = Regex.Replace(pageTitle, @"[\s]+", " "); pageTitle = pageTitle.Trim().ToLowerInvariant(); int success = 0; int derivedsuccess = 0; int exactsuccess = 0; foreach (var word in queryTerms) { MatchCollection m1 = Regex.Matches(pageTitle, @"^" + word + @"$", RegexOptions.IgnoreCase); if (m1.Count > 0) { exactsuccess++; } else { MatchCollection m2 = Regex.Matches(pageTitle, @"\b(" + word + @")\b", RegexOptions.IgnoreCase); if (m2.Count > 0) { success++; } else { MatchCollection m3 = Regex.Matches(pageTitle, word, RegexOptions.IgnoreCase); if (m3.Count > 0) { derivedsuccess++; } } } } if (success == pageTitle.Split(' ').Length || exactsuccess == pageTitle.Split(' ').Length) { deviationValue = SearchQuality.BEST; } else if (exactsuccess > 0 || (success > 0 && success < pageTitle.Split(' ').Length)) { deviationValue = SearchQuality.BETTER; } else if (derivedsuccess > 0 && derivedsuccess <= pageTitle.Split(' ').Length) { deviationValue = SearchQuality.GOOD; } return(deviationValue); }
public Dictionary <String, titleHits> findBestHitPage(List <String> queryTerms, out LinkItem mostValuable, out Boolean Did_you_mean) { Dictionary <String, titleHits> exactWordsMatch = new Dictionary <string, titleHits>(); Dictionary <String, titleHits> DerivedWordmatch = new Dictionary <String, titleHits>(); mostValuable = new LinkItem(); LinkItem SimilarMatch = null;; Boolean impDone = false; Boolean SimDone = false; float benchmark = 0.45F; Did_you_mean = false; foreach (var result in LR.LoadedResultsSet1) //.OrderByDescending(key => key.Value) { int success = 0; foreach (var queryterm in queryTerms) { String term = queryterm; if (Regex.IsMatch(result.Key, term, RegexOptions.IgnoreCase) == true) { success++; } else { float value = CompareSimilarMatch(term, result.Key); if (value < benchmark) { benchmark = value; SimilarMatch = new LinkItem(); SimilarMatch.Href = result.Value.Link; SimilarMatch.Text = result.Key; SimDone = true; success++; } } } if (success == queryTerms.Count) { SearchQuality decision = CompareTitleSuccess(queryTerms, result.Key); if (decision == SearchQuality.BETTER) { exactWordsMatch.Add(result.Key, new titleHits(result.Value)); } else if (decision == SearchQuality.GOOD) { DerivedWordmatch.Add(result.Key, new titleHits(result.Value)); } else if (decision == SearchQuality.BEST) { if (impDone == false) { mostValuable.Text = result.Key; mostValuable.Href = result.Value.Link; impDone = true; } } } } foreach (var match in DerivedWordmatch) { exactWordsMatch.Add(match.Key, new titleHits(match.Value)); } bool b1 = String.IsNullOrEmpty(mostValuable.Href); bool b2 = String.IsNullOrEmpty(mostValuable.Text); if (b1 && b2 && SimDone == true && impDone == false) { mostValuable.Href = SimilarMatch.Href; mostValuable.Text = SimilarMatch.Text; Did_you_mean = true; } return(exactWordsMatch); }