Add Acronym Support for Fuzzy Search

This commit is contained in:
弘韬 张 2020-10-18 21:17:29 +08:00
parent 7dc3ac1383
commit c63c98645c

View file

@ -54,9 +54,55 @@ namespace Flow.Launcher.Infrastructure
stringToCompare = _alphabet.Translate(stringToCompare);
}
// This also can be done by spliting the query
//(var spaceSplit, var upperSplit) = stringToCompare switch
//{
// string s when s.Contains(' ') => (s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(w => w.First()),
// default(IEnumerable<char>)),
// string s when s.Any(c => char.IsUpper(c)) && s.Any(c => char.IsLower(c)) =>
// (null, Regex.Split(s, @"(?<!^)(?=[A-Z])").Select(w => w.First())),
// _ => ((IEnumerable<char>)null, (IEnumerable<char>)null)
//};
var currentQueryIndex = 0;
var acronymMatchData = new List<int>();
var queryWithoutCase = opt.IgnoreCase ? query.ToLower() : query;
int acronymScore = 100;
for (int compareIndex = 0; compareIndex < stringToCompare.Length; compareIndex++)
{
if (currentQueryIndex >= queryWithoutCase.Length)
break;
if (compareIndex == 0 && queryWithoutCase[currentQueryIndex] == char.ToLower(stringToCompare[compareIndex]))
{
acronymMatchData.Add(compareIndex);
currentQueryIndex++;
continue;
}
switch (stringToCompare[compareIndex])
{
case char c when (char.IsUpper(c) && char.ToLower(c) == queryWithoutCase[currentQueryIndex])
|| (char.IsWhiteSpace(c) && char.ToLower(stringToCompare[++compareIndex]) == queryWithoutCase[currentQueryIndex]):
acronymMatchData.Add(compareIndex);
currentQueryIndex++;
continue;
case char c when char.IsWhiteSpace(c):
compareIndex++;
acronymScore -= 10;
break;
case char c when char.IsUpper(c):
acronymScore -= 10;
break;
}
}
var fullStringToCompareWithoutCase = opt.IgnoreCase ? stringToCompare.ToLower() : stringToCompare;
var queryWithoutCase = opt.IgnoreCase ? query.ToLower() : query;
var querySubstrings = queryWithoutCase.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int currentQuerySubstringIndex = 0;