Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change sorting with keywords #56

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions find_similar/calc_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 36 additions & 16 deletions testing/test_algorithm/test_calc_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading