From 0e241a0fc5b0698dea6f71bbb148440ff7ec8c38 Mon Sep 17 00:00:00 2001 From: Amir Tepper Date: Thu, 17 Oct 2019 13:37:09 +0300 Subject: [PATCH] added empty match option --- .../Commands/Bookmarks.cs | 6 +++--- Wox.Infrastructure/FuzzyMatcher.cs | 2 +- Wox.Infrastructure/StringMatcher.cs | 21 +++++++++++-------- Wox.Test/FuzzyMatcherTest.cs | 10 ++++----- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Plugins/Wox.Plugin.BrowserBookmark/Commands/Bookmarks.cs b/Plugins/Wox.Plugin.BrowserBookmark/Commands/Bookmarks.cs index 778e0ee65b..049b8f1668 100644 --- a/Plugins/Wox.Plugin.BrowserBookmark/Commands/Bookmarks.cs +++ b/Plugins/Wox.Plugin.BrowserBookmark/Commands/Bookmarks.cs @@ -8,9 +8,9 @@ namespace Wox.Plugin.BrowserBookmark.Commands { internal static bool MatchProgram(Bookmark bookmark, string queryString) { - if (StringMatcher.FuzzySearch(queryString, bookmark.Name, new MatchOption()).IsSearchPrecisionScoreMet()) return true; - if (StringMatcher.FuzzySearch(queryString, bookmark.PinyinName, new MatchOption()).IsSearchPrecisionScoreMet()) return true; - if (StringMatcher.FuzzySearch(queryString, bookmark.Url, new MatchOption()).IsSearchPrecisionScoreMet()) return true; + if (StringMatcher.FuzzySearch(queryString, bookmark.Name).IsSearchPrecisionScoreMet()) return true; + if (StringMatcher.FuzzySearch(queryString, bookmark.PinyinName).IsSearchPrecisionScoreMet()) return true; + if (StringMatcher.FuzzySearch(queryString, bookmark.Url).IsSearchPrecisionScoreMet()) return true; return false; } diff --git a/Wox.Infrastructure/FuzzyMatcher.cs b/Wox.Infrastructure/FuzzyMatcher.cs index cb1e735f5c..461f20ed56 100644 --- a/Wox.Infrastructure/FuzzyMatcher.cs +++ b/Wox.Infrastructure/FuzzyMatcher.cs @@ -16,7 +16,7 @@ namespace Wox.Infrastructure public static FuzzyMatcher Create(string query) { - return new FuzzyMatcher(query, new MatchOption()); + return new FuzzyMatcher(query, StringMatcher.EmptyMatchOption); } public static FuzzyMatcher Create(string query, MatchOption opt) diff --git a/Wox.Infrastructure/StringMatcher.cs b/Wox.Infrastructure/StringMatcher.cs index 0f2657a49a..ec1df0f03a 100644 --- a/Wox.Infrastructure/StringMatcher.cs +++ b/Wox.Infrastructure/StringMatcher.cs @@ -8,6 +8,8 @@ namespace Wox.Infrastructure { public static class StringMatcher { + public static MatchOption EmptyMatchOption = new MatchOption(); + public static string UserSettingSearchPrecision { get; set; } [Obsolete("This method is obsolete and should not be used. Please use the static function StringMatcher.FuzzySearch")] @@ -15,7 +17,7 @@ namespace Wox.Infrastructure { if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target)) { - return FuzzySearch(target, source, new MatchOption()).Score; + return FuzzySearch(target, source, EmptyMatchOption).Score; } else { @@ -26,12 +28,12 @@ namespace Wox.Infrastructure [Obsolete("This method is obsolete and should not be used. Please use the static function StringMatcher.FuzzySearch")] public static bool IsMatch(string source, string target) { - return FuzzySearch(target, source, new MatchOption()).Score > 0; + return FuzzySearch(target, source, EmptyMatchOption).Score > 0; } public static MatchResult FuzzySearch(string query, string stringToCompare) { - return FuzzySearch(query, stringToCompare, new MatchOption()); + return FuzzySearch(query, stringToCompare, EmptyMatchOption); } /// @@ -41,7 +43,7 @@ namespace Wox.Infrastructure { if (string.IsNullOrEmpty(stringToCompare) || string.IsNullOrEmpty(query)) return new MatchResult { Success = false }; - query.Trim(); + query = query.Trim(); var len = stringToCompare.Length; var compareString = opt.IgnoreCase ? stringToCompare.ToLower() : stringToCompare; @@ -98,9 +100,9 @@ namespace Wox.Infrastructure var score = 100 * (query.Length + 1) / ((1 + firstIndex) + (matchLen + 1)); //a match with less characters assigning more weights if (stringToCompare.Length - query.Length < 5) - score = score + 20; + score += 20; else if (stringToCompare.Length - query.Length < 10) - score = score + 10; + score += 10; return score; } @@ -139,10 +141,10 @@ namespace Wox.Infrastructure { var combination = Alphabet.PinyinComination(source); var pinyinScore = combination - .Select(pinyin => FuzzySearch(target, string.Join("", pinyin), new MatchOption()).Score) + .Select(pinyin => FuzzySearch(target, string.Join("", pinyin)).Score) .Max(); var acronymScore = combination.Select(Alphabet.Acronym) - .Select(pinyin => FuzzySearch(target, pinyin, new MatchOption()).Score) + .Select(pinyin => FuzzySearch(target, pinyin).Score) .Max(); var score = Math.Max(pinyinScore, acronymScore); return score; @@ -163,8 +165,9 @@ namespace Wox.Infrastructure { public bool Success { get; set; } public int Score { get; set; } + /// - /// hightlight string + /// highlight string /// public string Value { get; set; } } diff --git a/Wox.Test/FuzzyMatcherTest.cs b/Wox.Test/FuzzyMatcherTest.cs index 9aeca05348..ce1746a3a3 100644 --- a/Wox.Test/FuzzyMatcherTest.cs +++ b/Wox.Test/FuzzyMatcherTest.cs @@ -55,7 +55,7 @@ namespace Wox.Test results.Add(new Result { Title = str, - Score = StringMatcher.FuzzySearch("inst", str, new MatchOption()).Score + Score = StringMatcher.FuzzySearch("inst", str).Score }); } @@ -72,7 +72,7 @@ namespace Wox.Test { var compareString = "Can have rum only in my glass"; - var scoreResult = StringMatcher.FuzzySearch(searchString, compareString, new MatchOption()).Score; + var scoreResult = StringMatcher.FuzzySearch(searchString, compareString).Score; Assert.True(scoreResult == 0); } @@ -92,7 +92,7 @@ namespace Wox.Test results.Add(new Result { Title = str, - Score = StringMatcher.FuzzySearch(searchTerm, str, new MatchOption()).Score + Score = StringMatcher.FuzzySearch(searchTerm, str).Score }); } @@ -135,7 +135,7 @@ namespace Wox.Test results.Add(new Result { Title = str, - Score = StringMatcher.FuzzySearch(searchTerm, str, new MatchOption()).Score + Score = StringMatcher.FuzzySearch(searchTerm, str).Score }); } @@ -173,7 +173,7 @@ namespace Wox.Test { var expectedPrecisionString = (StringMatcher.SearchPrecisionScore)expectedPrecisionScore; StringMatcher.UserSettingSearchPrecision = expectedPrecisionString.ToString(); - var matchResult = StringMatcher.FuzzySearch(queryString, compareString, new MatchOption()); + var matchResult = StringMatcher.FuzzySearch(queryString, compareString); Debug.WriteLine(""); Debug.WriteLine("###############################################");