From 1e673d9cf769278104b552ab6c96e76b13e1f155 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Tue, 20 Jul 2021 11:11:36 +0800 Subject: [PATCH] Adjust Scoring --- Helper/ResultHelper.cs | 120 +++++++++++++---------------------------- 1 file changed, 36 insertions(+), 84 deletions(-) diff --git a/Helper/ResultHelper.cs b/Helper/ResultHelper.cs index dcda4c073..76d05089d 100644 --- a/Helper/ResultHelper.cs +++ b/Helper/ResultHelper.cs @@ -26,6 +26,7 @@ namespace Flow.Plugin.WindowsSettings.Helper /// Return a list with s, based on the given list. /// /// The original result list to convert. + /// Query for specific result List /// The path to the icon of each entry. /// A list with . internal static List GetResultList( @@ -36,7 +37,39 @@ namespace Flow.Plugin.WindowsSettings.Helper var resultList = new List(); foreach (var entry in list) { - var result = Predicate(); + const int highScore = 20; + const int midScore = 10; + + Result? result; + Debug.Assert(_api != null, nameof(_api) + " != null"); + + var nameMatch = _api.FuzzySearch(query.Search, entry.Name); + + if (nameMatch.IsSearchPrecisionScoreMet()) + { + var settingResult = NewSettingResult(nameMatch.Score + highScore); + settingResult.TitleHighlightData = nameMatch.MatchData; + result = settingResult; + } + else + { + var areaMatch = _api.FuzzySearch(query.Search, entry.Area); + if (areaMatch.IsSearchPrecisionScoreMet()) + { + var settingResult = NewSettingResult(areaMatch.Score + midScore); + settingResult.SubTitleHighlightData = areaMatch.MatchData.Select(x => x + 6).ToList(); + result = settingResult; + } + else + { + result = entry.AltNames? + .Select(altName => _api.FuzzySearch(query.Search, altName)) + .Where(match => match.IsSearchPrecisionScoreMet()) + .Select(altNameMatch => NewSettingResult(altNameMatch.Score + midScore)) + .FirstOrDefault(); + } + + } if (result is null) continue; @@ -44,7 +77,7 @@ namespace Flow.Plugin.WindowsSettings.Helper resultList.Add(result); - Result NewSettingResult(int score) => new Result() + Result NewSettingResult(int score) => new() { Action = _ => DoOpenSettingsAction(entry), IcoPath = iconPath, @@ -54,36 +87,9 @@ namespace Flow.Plugin.WindowsSettings.Helper Score = score }; - Result? Predicate() - { - Debug.Assert(_api != null, nameof(_api) + " != null"); - - var nameMatch = _api.FuzzySearch(query.Search, entry.Name); - - if (nameMatch.IsSearchPrecisionScoreMet()) - { - var settingResult = NewSettingResult(nameMatch.Score); - settingResult.TitleHighlightData = nameMatch.MatchData; - return settingResult; - } - - var areaMatch = _api.FuzzySearch(query.Search, entry.Area); - if (areaMatch.IsSearchPrecisionScoreMet()) - { - var settingResult = NewSettingResult(areaMatch.Score); - settingResult.SubTitleHighlightData = areaMatch.MatchData.Select(x => x + 6).ToList(); - return settingResult; - } - - return entry.AltNames? - .Select(altName => _api.FuzzySearch(query.Search, altName)) - .Where(match => match.IsSearchPrecisionScoreMet()) - .Select(altNameMatch => NewSettingResult(altNameMatch.Score)) - .FirstOrDefault(); - - } } + return resultList; } @@ -167,59 +173,5 @@ namespace Flow.Plugin.WindowsSettings.Helper return false; } } - - /// - /// Set the score (known as order number or ranking number) - /// for all in the given list, based on the given query. - /// - /// A list with s that need scores. - /// The query to calculated the score for the s. - private static void SetScores(IEnumerable resultList, string query) - { - var lowScore = 1_000; - var mediumScore = 5_000; - var highScore = 10_000; - - foreach (var result in resultList) - { - if (!(result.ContextData is WindowsSetting windowsSetting)) - { - continue; - } - - if (windowsSetting.Name.StartsWith(query, StringComparison.CurrentCultureIgnoreCase)) - { - result.Score = highScore--; - continue; - } - - // If query starts with second or next word of name, set score. - if (windowsSetting.Name.Contains($" {query}", StringComparison.CurrentCultureIgnoreCase)) - { - result.Score = mediumScore--; - continue; - } - - if (windowsSetting.Area.StartsWith(query, StringComparison.CurrentCultureIgnoreCase)) - { - result.Score = lowScore--; - continue; - } - - if (windowsSetting.AltNames is null) - { - result.Score = lowScore--; - continue; - } - - if (windowsSetting.AltNames.Any(x => x.StartsWith(query, StringComparison.CurrentCultureIgnoreCase))) - { - result.Score = mediumScore--; - continue; - } - - result.Score = lowScore--; - } - } } }