From 036b64b51efc9188ec7bee86c825e6840c45ba97 Mon Sep 17 00:00:00 2001 From: Simone Tesini Date: Fri, 1 Aug 2025 19:31:47 +0200 Subject: [PATCH 1/2] theoretically finish the algorithm --- backend/src/room.py | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/backend/src/room.py b/backend/src/room.py index 92b0ee6..6332fd0 100644 --- a/backend/src/room.py +++ b/backend/src/room.py @@ -3,15 +3,14 @@ from dataclasses import dataclass from .song import Song USER_SCORE_WEIGHT = 0.7 -GENRE_WEIGHT = 0.1 +ARTIST_WEIGHT = 0.1 +TAG_WEIGHT = 0.1 RANDOM_WEIGHT = 0.1 RECENT_PENALTY = 0.5 RECENT_COUNT = 10 -@dataclass -class UserScoredSong(Song): - user_score: int +type UserScoredSong = tuple[Song, int] @dataclass @@ -26,19 +25,38 @@ class Room: songs: dict[str, UserScoredSong] history: list[Song] - def rank_song( - self, - song: UserScoredSong, - ) -> float: + def rank_song(self, song: Song, user_score: int) -> float: rank = 0.0 song_items = self.songs.items() - lowest_score = min(song_items, key=lambda item: item[1].user_score)[1].user_score - highest_score = min(song_items, key=lambda item: item[1].user_score)[1].user_score + lowest_score = min(song_items, key=lambda item: item[1][1])[1][1] + highest_score = min(song_items, key=lambda item: item[1][1])[1][1] - rank += translate(song.user_score, lowest_score, highest_score, 0.0, USER_SCORE_WEIGHT) + rank += translate(user_score, lowest_score, highest_score, 0.0, USER_SCORE_WEIGHT) - # last10items = self.history[-10:] + recent_songs = self.history[-RECENT_COUNT:] + + tag_counts = {} + artist_counts = {} + for song in recent_songs: + for tag in song.tags: + if tag not in tag_counts: + tag_counts[tag] = 0 + tag_counts[tag] += 1 + if song.artist not in artist_counts: + artist_counts[song.artist] = 0 + artist_counts[song.artist] += 1 + + tag_total = 0 + for tag in song.tags: + if tag in tag_counts: + tag_total += tag_counts[tag] + + rank += translate(tag_total, 0, RECENT_COUNT, 0, TAG_WEIGHT) + rank += translate(artist_counts[song.artist], 0, RECENT_COUNT, 0, ARTIST_WEIGHT) + + if song in recent_songs: + rank -= RECENT_PENALTY return rank From 41f6f5ba1af381a00722501a94361a5c388ae465 Mon Sep 17 00:00:00 2001 From: Simone Tesini Date: Fri, 1 Aug 2025 20:46:42 +0200 Subject: [PATCH 2/2] pull --- backend/src/room.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/room.py b/backend/src/room.py index 6332fd0..51b96e2 100644 --- a/backend/src/room.py +++ b/backend/src/room.py @@ -20,7 +20,6 @@ class Room: name: str pin: int | None tags: set[str] - creative: bool songs: dict[str, UserScoredSong] history: list[Song]