public Dictionary <string, string> RestrictedSearchTexts; // restricted to certain provider keys private MatchResult Match(string searchText, ISearchable searchable, string key, string text) { var result = new MatchResult(searchable, key, text, this); var index = text.IndexOf(searchText); if (index >= 0) { var span = new MatchResult.Span(index, searchText.Length); result.AddSpan(span); result.TargetRatio = result.TotalMatched / (float)text.Length; } return(result); }
private MatchResult FuzzyMatch(ISearchable searchable, string key, string text) { var result = new MatchResult(searchable, key, text, this); var searchTextIndex = 0; var targetIndex = -1; var searchText = result.Context.SearchText; var target = result.Text; // find a common prefix if any, so n:cat h:catsgrace is better than n:cat h:blahcatsgrace targetIndex = target.IndexOf(searchText[searchTextIndex]); if (targetIndex == 0) { result.Bonus = 2.0f; } // penalise matches that don't have a common prefix, while increasing searchTextIndex and targetIndex to the first match, so: // n:bOb h:hellOworldbob // ^ ^ while (targetIndex == -1 && searchTextIndex < searchText.Length) { if (searchTextIndex == 0) { result.Penalty = 2; } else { result.Penalty += result.Penalty * .5f; } targetIndex = target.IndexOf(searchText[searchTextIndex]); searchTextIndex++; } // continue to match the next searchTextIndex greedily in target while (searchTextIndex < searchText.Length) { // find the next point in target that matches searchIndex: // n:bOb h:helloworldBob // ^ ^ targetIndex = target.IndexOf(searchText[searchTextIndex], targetIndex); if (targetIndex == -1) { break; } //continue matching while both are in sync var span = new MatchResult.Span(targetIndex); while (targetIndex < target.Length && searchTextIndex < searchText.Length && searchText[searchTextIndex] == target[targetIndex]) { //if this span is rooted at the start of the word give a bonus because start is most importatn if (span.From == 0 && searchTextIndex > 0) { result.Bonus += result.Bonus; } searchTextIndex++; targetIndex++; } //record the end of the span span.End(targetIndex); result.AddSpan(span); } result.TargetRatio = result.TotalMatched / (float)target.Length; return(result); }