bug: sort results from keyword search (#232)

* bug: sort results from keyword search

* Auto-format code 🧹🌟🤖

---------

Co-authored-by: Formatter [BOT] <runner@fv-az1538-928.upsp13a5k4ou3ds4kr34xzh2lh.cx.internal.cloudapp.net>
This commit is contained in:
Gao, Ruiyuan 2024-10-15 06:42:11 +08:00 committed by GitHub
parent 44819de3a5
commit 6ed5d0cb5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -116,12 +116,19 @@ def keyword_detection(user_input, str_list, n):
else:
remains.append(item)
matched = sorted(
matched,
key=lambda s: difflib.SequenceMatcher(None, s, user_input).ratio(),
reverse=True, # 降序排序,越相似的越靠前
)
# 如果 n 是 -1如果 n 大于匹配的数量,返回所有匹配的结果
if n == -1 or n > len(matched):
return matched, remains
# 随机选择 n 个匹配的结果
return random.sample(matched, n), remains
# 选择前 n 个匹配的结果
remains = matched[n:] + remains
return matched[:n], remains
def real_search(prompt, candidates, cutoff, n):