Refactor, improve search accuracy when multiple contains results

If multiple items contain the search string perfectly the fuzzy search still expand it with suggestions. These fuzzy-suggestions, however, might have **less** occurrences in the search string than the items that contained it. The changes ensure that fuzzy-suggestions must have at least the same amount of occurrences as apps that contain the search string. Also, I removed the `appsSecondary` wrapper-list and directly work on `r`.
This commit is contained in:
Jan Koll 2025-06-27 00:38:18 +02:00
parent 7744974abb
commit c5eb0664c0

View file

@ -64,8 +64,7 @@ class AppFilter(
if (query.isEmpty()) { if (query.isEmpty()) {
return apps return apps
} else { } else {
val r: MutableList<AbstractDetailedAppInfo> = ArrayList() val r: MutableSet<AbstractDetailedAppInfo> = hashSetOf()
val appsSecondary: MutableList<AbstractDetailedAppInfo> = ArrayList()
val normalizedQuery: String = normalize(query) val normalizedQuery: String = normalize(query)
val subsequentResult: MutableList<AbstractDetailedAppInfo> = mutableListOf(); val subsequentResult: MutableList<AbstractDetailedAppInfo> = mutableListOf();
val occurrences: MutableMap<AbstractDetailedAppInfo, Int> = mutableMapOf(); val occurrences: MutableMap<AbstractDetailedAppInfo, Int> = mutableMapOf();
@ -73,29 +72,28 @@ class AppFilter(
val itemLabel: String = normalize(item.getCustomLabel(context)) val itemLabel: String = normalize(item.getCustomLabel(context))
if (itemLabel.startsWith(normalizedQuery)) { if (itemLabel.startsWith(normalizedQuery)) {
appsSecondary.add(item); r.add(item);
} else if (itemLabel.contains(normalizedQuery)) { } else if (itemLabel.contains(normalizedQuery)) {
appsSecondary.add(item) r.add(item)
} else if (LauncherPreferences.functionality().searchFuzzy()) { }
if (LauncherPreferences.functionality().searchFuzzy()) {
if (isSubsequent(itemLabel, normalizedQuery)) { if (isSubsequent(itemLabel, normalizedQuery)) {
subsequentResult.add(item) subsequentResult.add(item)
} }
occurrences[item] = countOccurrences(itemLabel, normalizedQuery) occurrences[item] = countOccurrences(itemLabel, normalizedQuery)
} }
} }
if (LauncherPreferences.functionality().searchFuzzy() && appsSecondary.size != 1) { if (LauncherPreferences.functionality().searchFuzzy() && r.size != 1) {
if (subsequentResult.isNotEmpty()) { if (subsequentResult.isNotEmpty()) {
appsSecondary.addAll(subsequentResult) r.addAll(subsequentResult)
} else { } else {
val maxOccurrences = occurrences.values.maxOrNull() val maxOccurrences = occurrences.values.maxOrNull()
if (maxOccurrences == 0) return apps if (maxOccurrences == 0) return apps
val result = occurrences.filter { it.value == maxOccurrences } val result = occurrences.filter { it.value == maxOccurrences }
appsSecondary.addAll(result.keys) r.addAll(result.keys)
} }
} }
r.addAll(appsSecondary) return r.toList()
return r
} }
} }