From f9b818e7ce1af4a6b4e7e8b17bbdc6960aba3848 Mon Sep 17 00:00:00 2001 From: quillcraftsman Date: Tue, 10 Oct 2023 22:55:39 +0400 Subject: [PATCH] change sorting with keywords --- find_similar/calc_functions.py | 4 +- testing/test_algorithm/test_calc_functions.py | 52 +++++++++++++------ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/find_similar/calc_functions.py b/find_similar/calc_functions.py index a478462..db982da 100644 --- a/find_similar/calc_functions.py +++ b/find_similar/calc_functions.py @@ -108,8 +108,8 @@ def sort_search_list(token_texts, keywords=None): ) if keywords: text_sorted_by_cos = sorted( - text_sorted_by_cos, - key=lambda item: item.key, + token_texts, + key=lambda item: (item.key, item.cos), reverse=True ) return text_sorted_by_cos diff --git a/testing/test_algorithm/test_calc_functions.py b/testing/test_algorithm/test_calc_functions.py index f359a1d..f7bcb8a 100644 --- a/testing/test_algorithm/test_calc_functions.py +++ b/testing/test_algorithm/test_calc_functions.py @@ -38,24 +38,44 @@ def test_sort_search_list(): """ sort_search_list """ - texts = [ - TokenText("ночь"), - TokenText("улица"), - TokenText("фонарь"), - ] - token_texts = [] - cos = 0 - key = 3 - for text in texts: - text.cos = cos + 0.2 - cos = text.cos - text.key = key - 1 - key = text.key - token_texts.append(text) + night_text = 'ночь' + street_text = 'улица' + lamp_text = 'фонарь' + + night = TokenText(night_text) + street = TokenText(street_text) + lamp = TokenText(lamp_text) + + token_texts = [night, street, lamp] + + night.cos = 0.9 + street.cos = 0.8 + lamp.cos = 1 + + night.key = 1 + street.key = 0 + lamp.key = 0 + + sort_with_keyword = sort_search_list(token_texts, {"аптека": 1}) + sort_without_keyword = sort_search_list(token_texts) + + assert [text.text for text in sort_with_keyword] == [night_text, lamp_text, street_text] + assert [text.text for text in sort_without_keyword] == [lamp_text, night_text, street_text] + + night.cos = 1 + street.cos = 0.8 + lamp.cos = 0.9 + + night.key = 0 + street.key = 1 + lamp.key = 0 + sort_with_keyword = sort_search_list(token_texts, {"аптека": 1}) sort_without_keyword = sort_search_list(token_texts) - assert sort_with_keyword[0].text == "ночь" - assert sort_without_keyword[0].text == "фонарь" + + # by key first + assert [text.text for text in sort_with_keyword] == [street_text, night_text, lamp_text] + assert [text.text for text in sort_without_keyword] == [night_text, lamp_text, street_text] class TestTokenText: