Skip to content

Commit

Permalink
优化搜索相关代码
Browse files Browse the repository at this point in the history
  • Loading branch information
whp98 committed Oct 11, 2023
1 parent 128b8bc commit 936b6ba
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
8 changes: 3 additions & 5 deletions app/src/main/kotlin/com/aistra/hail/ui/apps/AppsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ object AppsAdapter :
&& ((HailData.filterFrozenApps && AppManager.isAppFrozen(it.packageName))
|| (HailData.filterUnfrozenApps && !AppManager.isAppFrozen(it.packageName)))

&& (query.isNullOrEmpty()
|| it.packageName.contains(query, true)
|| it.loadLabel(pm).toString().contains(query, true)
|| PinyinSearch.searchPinyinAll(it.loadLabel(pm).toString(), query)
|| FuzzySearch.search(it.loadLabel(pm).toString(), query))
&& (FuzzySearch.search(it.packageName, query)
|| FuzzySearch.search(it.loadLabel(pm).toString(), query)
|| PinyinSearch.searchPinyinAll(it.loadLabel(pm).toString(), query))
}.run {
when (HailData.sortBy) {
HailData.SORT_INSTALL -> sortedBy {
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/kotlin/com/aistra/hail/ui/home/PagerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ class PagerFragment : MainFragment(), PagerAdapter.OnItemClickListener,

private fun updateCurrentList() = HailData.checkedList.filter {
if (query.isEmpty()) it.tagId == tag.second
else (it.packageName.contains(query, true)
|| it.name.contains(query, true)
|| PinyinSearch.searchPinyinAll(it.name.toString(), query)
else (FuzzySearch.search(it.packageName, query)
|| FuzzySearch.search(it.name.toString(), query)
)
|| PinyinSearch.searchPinyinAll(it.name.toString(), query))
}.sortedWith(NameComparator).let {
binding.empty.isVisible = it.isEmpty()
pagerAdapter.submitList(it)
Expand Down
22 changes: 13 additions & 9 deletions app/src/main/kotlin/com/aistra/hail/utils/FuzzySearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ object FuzzySearch {
private val levenshteinDistance: LevenshteinDistance = LevenshteinDistance()

/**
* 字符串差异在20个字符以下 且 距离小于搜索字符串长度 且 搜索字符全部包含在搜索字符串中 则显示在搜索结果中
* 两个字符串差异小于搜索字符串长度 且 搜索字符全部包含在搜索字符串中 则显示在搜索结果中
* @param textToSearch 尝试匹配的字符串
* @param query 用户输入字符串
* */
fun search(textToSearch: String?, query: String?): Boolean {
if (textToSearch == null || query == null) {
if (query.isNullOrEmpty()) {
return true
}
if (textToSearch.isNullOrEmpty()) {
return false
}
if (textToSearch.contains(query, true)) {
return true
}
val textToSearchUpp = textToSearch.uppercase()
val queryUpp = query.uppercase()
val d = levenshteinDistance.apply(textToSearchUpp, queryUpp)
val diff = levenshteinDistance.apply(textToSearchUpp, queryUpp)
val lenTextToSearch = textToSearchUpp.length
val maxD = 20
// 字符串差异在20个字符以下 且 距离小于搜索字符串长度 且 搜索字符全部包含在搜索字符串中 则显示在搜索结果中
return d < maxD && d < lenTextToSearch && containsAllChars(textToSearchUpp, queryUpp)
return diff < lenTextToSearch && containsAllChars(textToSearchUpp, queryUpp)
}

fun containsAllChars(str1: String, str2: String): Boolean {
Expand All @@ -33,11 +37,11 @@ object FuzzySearch {

@JvmStatic
fun main(args: Array<String>) {
val testResult1 = search("支付宝","")
val testResult1 = search("支付宝", "")
assert(testResult1)
val testResult2 = search("World Peace","wp")
val testResult2 = search("World Peace", "wp")
assert(testResult2)
val testResult3 = search("World Peace","pee")
val testResult3 = search("World Peace", "pee")
assert(testResult3)
}
}
11 changes: 7 additions & 4 deletions app/src/main/kotlin/com/aistra/hail/utils/PinyinSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ object PinyinSearch {
* 分别使用首字母和全拼进行匹配,满足条件之一就返回true,
* 如果当前语言不是中文就直接返回false
* @param textToSearch 需要匹配的字符串
* @param textInput 用户输入
* @param query 用户输入
* */
fun searchPinyinAll(textToSearch: String?, textInput: String): Boolean {
if (textToSearch==null){
fun searchPinyinAll(textToSearch: String?, query: String?): Boolean {
if (query.isNullOrEmpty()) {
return true
}
if (textToSearch.isNullOrEmpty()) {
return false
}
val language = Locale.getDefault().language
return if (language.equals(Locale.CHINESE.language)) {
searchCap(textToSearch, textInput) || searchAllSpell(textToSearch, textInput)
searchCap(textToSearch, query) || searchAllSpell(textToSearch, query)
} else {
false
}
Expand Down

0 comments on commit 936b6ba

Please sign in to comment.