From fbb5d4a4a65db3ed9359d028b859e1330b251338 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 29 Sep 2019 14:26:15 +1000 Subject: [PATCH] Make FuzzMatcher class obsolete and keep backwards compatibility --- Wox.Infrastructure/FuzzyMatcher.cs | 100 +---------------------------- 1 file changed, 3 insertions(+), 97 deletions(-) diff --git a/Wox.Infrastructure/FuzzyMatcher.cs b/Wox.Infrastructure/FuzzyMatcher.cs index c2d7e02e94..cb1e735f5c 100644 --- a/Wox.Infrastructure/FuzzyMatcher.cs +++ b/Wox.Infrastructure/FuzzyMatcher.cs @@ -1,10 +1,8 @@ -using System.Text; +using System; namespace Wox.Infrastructure { - /// - /// refer to https://github.com/mattyork/fuzzy - /// + [Obsolete("This class is obsolete and should not be used. Please use the static function StringMatcher.FuzzySearch")] public class FuzzyMatcher { private string query; @@ -28,99 +26,7 @@ namespace Wox.Infrastructure public MatchResult Evaluate(string str) { - if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(query)) return new MatchResult { Success = false }; - - var len = str.Length; - var compareString = opt.IgnoreCase ? str.ToLower() : str; - var pattern = opt.IgnoreCase ? query.ToLower() : query; - - var sb = new StringBuilder(str.Length + (query.Length * (opt.Prefix.Length + opt.Suffix.Length))); - var patternIdx = 0; - var firstMatchIndex = -1; - var lastMatchIndex = 0; - char ch; - for (var idx = 0; idx < len; idx++) - { - ch = str[idx]; - if (compareString[idx] == pattern[patternIdx]) - { - if (firstMatchIndex < 0) - firstMatchIndex = idx; - lastMatchIndex = idx + 1; - - sb.Append(opt.Prefix + ch + opt.Suffix); - patternIdx += 1; - } - else - { - sb.Append(ch); - } - - // match success, append remain char - if (patternIdx == pattern.Length && (idx + 1) != compareString.Length) - { - sb.Append(str.Substring(idx + 1)); - break; - } - } - - // return rendered string if we have a match for every char - if (patternIdx == pattern.Length) - { - return new MatchResult - { - Success = true, - Value = sb.ToString(), - Score = CalScore(str, firstMatchIndex, lastMatchIndex - firstMatchIndex) - }; - } - - return new MatchResult { Success = false }; + return StringMatcher.FuzzySearch(query, str, opt); } - - private int CalScore(string str, int firstIndex, int matchLen) - { - //a match found near the beginning of a string is scored more than a match found near the end - //a match is scored more if the characters in the patterns are closer to each other, while the score is lower if they are more spread out - var score = 100 * (query.Length + 1) / ((1 + firstIndex) + (matchLen + 1)); - //a match with less characters assigning more weights - if (str.Length - query.Length < 5) - score = score + 20; - else if (str.Length - query.Length < 10) - score = score + 10; - - return score; - } - } - - public class MatchResult - { - public bool Success { get; set; } - public int Score { get; set; } - /// - /// hightlight string - /// - public string Value { get; set; } - } - - public class MatchOption - { - public MatchOption() - { - Prefix = ""; - Suffix = ""; - IgnoreCase = true; - } - - /// - /// prefix of match char, use for hightlight - /// - public string Prefix { get; set; } - /// - /// suffix of match char, use for hightlight - /// - public string Suffix { get; set; } - - public bool IgnoreCase { get; set; } } }