Optimized local search a bit + better results

This commit is contained in:
augustin64 2023-12-06 19:57:28 +01:00
parent 2c144674ba
commit 2d807112c7

View File

@ -19,29 +19,34 @@ def local_search(query, partitions):
""" """
Renvoie les 5 résultats les plus pertinents parmi une liste donnée Renvoie les 5 résultats les plus pertinents parmi une liste donnée
""" """
query_words = [word.lower() for word in query.split(" ")]
def score_attribution(partition): def score_attribution(partition):
score = 0 score = 0
for word in query.split(" "): for word in query_words:
if word != "": if word != "":
low_word = word.lower() if word in partition["name"].lower():
if low_word in partition["name"].lower(): score += 6
score += 3 elif word in partition["author"].lower():
elif low_word in partition["body"].lower(): score += 4
score += 1 elif word in partition["body"].lower():
elif low_word in partition["author"].lower():
score += 2 score += 2
else: else:
score -= .5 score -= 1
for word in partition["name"].split(" "):
if word != "" and word.lower() not in query_words:
score -= 1
return score return score
partitions = sorted(partitions, key=score_attribution, reverse=True) score_partitions = [(score_attribution(partition), partition) for partition in partitions]
sorted_partitions = [] score_partitions.sort(key=lambda x: x[0], reverse=True)
for partition in partitions:
if score_attribution(partition) > 0: selection = []
sorted_partitions.append(partition) for score, partition in score_partitions[:5]:
if score > 0:
selection.append(partition)
else: else:
break break
return sorted_partitions[:min(5,len(sorted_partitions))] return selection
def download_search_result(element): def download_search_result(element):