0.1.4: versus rankings IN top tags

This commit is contained in:
Mark Joshwel 2024-05-25 01:37:17 +08:00
parent dfd9e342bb
commit 1888447570
2 changed files with 83 additions and 21 deletions

View file

@ -51,6 +51,8 @@ USER_AGENT: Final[str] = (
"Mozilla/5.0 " "(compatible; lfcircle; https://github.com/markjoshwel/lfcircle)" "Mozilla/5.0 " "(compatible; lfcircle; https://github.com/markjoshwel/lfcircle)"
) )
GlobalTagCounter = dict[str, Counter[str]]
class FormatTypeEnum(Enum): class FormatTypeEnum(Enum):
""" """
@ -251,11 +253,15 @@ class ListeningReport(NamedTuple):
leaderboard_albums_pos: int, leaderboard_albums_pos: int,
leaderboard_tracks_pos: int, leaderboard_tracks_pos: int,
leaderboard_n: int, leaderboard_n: int,
global_tag_counter: GlobalTagCounter,
) -> str: ) -> str:
basket: list[str] = [] basket: list[str] = []
prefix: str = "" prefix: str = ""
text: str = "" text: str = ""
tag_counter: Counter[str]
tags: list[str]
match behaviour.format: match behaviour.format:
case FormatTypeEnum.ASCII: case FormatTypeEnum.ASCII:
# intro # intro
@ -328,20 +334,33 @@ class ListeningReport(NamedTuple):
tag_counter = Counter[str]() tag_counter = Counter[str]()
for tag_name, tag_values in self.tags.items(): for tag_name, tag_values in self.tags.items():
tag_counter.update({tag_name: sum(tag_values)}) tag_counter.update(
{tag_name: calculate_tag_score(tag_values, tags=self.tags)}
)
tags = []
for tn, tc in tag_counter.most_common(5):
tv: str = ""
if tn in global_tag_counter:
counter = global_tag_counter[tn]
if len(counter) > 1:
_tv = sum(
[
i if (user == self.user) else 0
for i, (user, _) in enumerate(
counter.most_common(), start=1
)
]
)
tv = f": #{_tv}" if (_tv > 0) else ""
tags.append(f"{tn} ({tc}%{tv})")
basket.append( basket.append(
indent( indent(
( ("Top tags".ljust(len(d4_l) - 6) + " : " + ", ".join(tags)),
"Top tags".ljust(len(d4_l) - 6)
+ " : "
+ ", ".join(
[
f"{tn} ({tc})"
for tn, tc in tag_counter.most_common(5)
]
)
),
prefix=prefix, prefix=prefix,
) )
) )
@ -398,18 +417,34 @@ class ListeningReport(NamedTuple):
# detail 5: top tags # detail 5: top tags
if len(self.tags) > 0: if len(self.tags) > 0:
tag_counter = Counter[str]() tag_counter = Counter()
for tag_name, tag_values in self.tags.items(): for tag_name, tag_values in self.tags.items():
tag_counter.update({tag_name: sum(tag_values)}) tag_counter.update(
{tag_name: calculate_tag_score(tag_values, tags=self.tags)}
basket.append(
f"\n{prefix}Top tags"
+ ": "
+ ", ".join(
[f"{tn} ({tc})" for tn, tc in tag_counter.most_common(5)]
) )
)
tags = []
for tn, tc in tag_counter.most_common(5):
tv = ""
if tn in global_tag_counter:
counter = global_tag_counter[tn]
if len(counter) > 1:
_tv = sum(
[
i if (user == self.user) else 0
for i, (user, _) in enumerate(
counter.most_common(), start=1
)
]
)
tv = f": #{_tv}" if (_tv > 0) else ""
tags.append(f"{tn} ({tc}%{tv})")
basket.append(f"\n{prefix}Top tags" + ": " + ", ".join(tags))
if not behaviour.lowercase: if not behaviour.lowercase:
text = "\n".join(basket) text = "\n".join(basket)
@ -682,6 +717,20 @@ def _rank(
return 0 return 0
def calculate_tag_score(
tag_values: tuple[int, ...], tags: dict[str, tuple[int, ...]]
) -> int:
v_cum_sum = sum([sum(tv) for tv in tags.values()])
v_cum_avg = sum([(sum(tv) / len(tv)) for tv in tags.values()])
v_avg = (v_sum := sum(tag_values)) / len(tag_values)
tag_obj_score = (0.5 * v_avg) + (0.5 * v_sum)
tag_cum_score = (0.5 * v_cum_sum) + (0.5 * v_cum_avg)
return round(100 * (tag_obj_score / tag_cum_score))
def make_circle_report( def make_circle_report(
listening_reports: list[ListeningReport], listening_reports: list[ListeningReport],
behaviour: Behaviour, behaviour: Behaviour,
@ -698,6 +747,18 @@ def make_circle_report(
case FormatTypeEnum.TELEGRAM: case FormatTypeEnum.TELEGRAM:
text.append(behaviour.header + "\n") text.append(behaviour.header + "\n")
# pre-string building global tag versus calculation
global_tag_counter: GlobalTagCounter = {}
for report in listening_reports:
for tag_name, tag_value in report.tags.items():
if tag_name not in global_tag_counter:
global_tag_counter[tag_name] = Counter()
global_tag_counter[tag_name][report.user] += calculate_tag_score(
tag_value, tags=report.tags
)
for leaderboard_pos, report in enumerate( for leaderboard_pos, report in enumerate(
reversed( reversed(
sorted( sorted(
@ -732,6 +793,7 @@ def make_circle_report(
k=lambda r: r.tracks_count, k=lambda r: r.tracks_count,
), ),
leaderboard_n=len(listening_reports), leaderboard_n=len(listening_reports),
global_tag_counter=global_tag_counter,
) )
+ "\n" + "\n"
) )

View file

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "lfcircle" name = "lfcircle"
version = "0.1.3" version = "0.1.4"
description = "last.fm statistics generator for your friend circle!" description = "last.fm statistics generator for your friend circle!"
authors = ["Mark Joshwel <mark@joshwel.co>"] authors = ["Mark Joshwel <mark@joshwel.co>"]
license = "Unlicense" license = "Unlicense"