Skip to content

Commit c63c986

Browse files
committed
Add Acronym Support for Fuzzy Search
1 parent 7dc3ac1 commit c63c986

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

Flow.Launcher.Infrastructure/StringMatcher.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,55 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
5454
stringToCompare = _alphabet.Translate(stringToCompare);
5555
}
5656

57+
// This also can be done by spliting the query
58+
59+
//(var spaceSplit, var upperSplit) = stringToCompare switch
60+
//{
61+
// string s when s.Contains(' ') => (s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(w => w.First()),
62+
// default(IEnumerable<char>)),
63+
// string s when s.Any(c => char.IsUpper(c)) && s.Any(c => char.IsLower(c)) =>
64+
// (null, Regex.Split(s, @"(?<!^)(?=[A-Z])").Select(w => w.First())),
65+
// _ => ((IEnumerable<char>)null, (IEnumerable<char>)null)
66+
//};
67+
68+
var currentQueryIndex = 0;
69+
var acronymMatchData = new List<int>();
70+
var queryWithoutCase = opt.IgnoreCase ? query.ToLower() : query;
71+
72+
int acronymScore = 100;
73+
74+
for (int compareIndex = 0; compareIndex < stringToCompare.Length; compareIndex++)
75+
{
76+
if (currentQueryIndex >= queryWithoutCase.Length)
77+
break;
78+
79+
if (compareIndex == 0 && queryWithoutCase[currentQueryIndex] == char.ToLower(stringToCompare[compareIndex]))
80+
{
81+
acronymMatchData.Add(compareIndex);
82+
currentQueryIndex++;
83+
continue;
84+
}
85+
86+
switch (stringToCompare[compareIndex])
87+
{
88+
case char c when (char.IsUpper(c) && char.ToLower(c) == queryWithoutCase[currentQueryIndex])
89+
|| (char.IsWhiteSpace(c) && char.ToLower(stringToCompare[++compareIndex]) == queryWithoutCase[currentQueryIndex]):
90+
acronymMatchData.Add(compareIndex);
91+
currentQueryIndex++;
92+
continue;
93+
94+
case char c when char.IsWhiteSpace(c):
95+
compareIndex++;
96+
acronymScore -= 10;
97+
break;
98+
case char c when char.IsUpper(c):
99+
acronymScore -= 10;
100+
break;
101+
}
102+
}
103+
57104
var fullStringToCompareWithoutCase = opt.IgnoreCase ? stringToCompare.ToLower() : stringToCompare;
58105

59-
var queryWithoutCase = opt.IgnoreCase ? query.ToLower() : query;
60106

61107
var querySubstrings = queryWithoutCase.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
62108
int currentQuerySubstringIndex = 0;

0 commit comments

Comments
 (0)