extensions-website/ratings/utils.py
Oleg Komarov 86fe0bbe51 Extension: implement sorting by rating_sortkey # 145
Adding new field rating_sortkey, computed according to
https://www.evanmiller.org/ranking-items-with-star-ratings.html

We keep Extension.average_score and use it to display average rating.

Exposing rating_sortkey in place of average_score would be confusing:

    compute_rating_sortkey([5] * 10) = 3.82

seeing average rating lower than minimum rating causes too many questions.
2024-06-06 19:11:23 +02:00

22 lines
599 B
Python

from collections import defaultdict
from math import sqrt
def compute_rating_sortkey(scores):
"""Implementation of https://www.evanmiller.org/ranking-items-with-star-ratings.html
Suggested by HugeMenace in
https://projects.blender.org/infrastructure/extensions-website/issues/145
"""
N = len(scores)
K = 5
n = defaultdict(int)
z = 1.65
for score in scores:
n[score] += 1
A = sum(k**2 * (n[k] + 1) for k in range(1, K + 1)) / (N + K)
B = sum(k * (n[k] + 1) for k in range(1, K + 1)) / (N + K)
return B - z * sqrt((A - B**2) / (N + K + 1))