Skip to content

Commit 752208a

Browse files
author
tosaki
committed
feat: Implement score-aware time decay in gravity score calculation, allowing high-scoring items to decay slower.
1 parent 01a6bd6 commit 752208a

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

ranking.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,38 @@
22

33
def calculate_gravity_score(base_score: float, published_at_ts: float, gravity: float = 1.8) -> float:
44
"""
5-
Calculate the gravity score based on: Score / (Time + 2)^G
6-
Time is in hours since publication.
5+
Calculate the gravity score with score-aware time decay.
6+
7+
High-scoring items decay slower than low-scoring items.
8+
Formula: Score / (Time + base_offset + score_bonus)^effective_gravity
9+
10+
- base_offset: 0.5 (reduced from 2 to make decay faster overall)
11+
- score_bonus: 0 to 3 hours extra buffer based on score (0-100)
12+
- effective_gravity: slightly reduced for high scores
13+
14+
This ensures truly important news (high L2 score) stays relevant longer,
15+
while regular items decay more quickly.
716
"""
817
now = time.time()
918

1019
# Calculate age in hours
11-
# Ensure age is at least 0 to avoid issues with future timestamps (clock skew)
1220
age_hours = max(0, (now - published_at_ts) / 3600)
1321

14-
# Standard HNews Gravity Formula
15-
# score / (age_hours + 2) ^ gravity
16-
return base_score / pow((age_hours + 2), gravity)
22+
# Base offset reduced from 2 to 0.5 for faster overall decay
23+
base_offset = 0.5
24+
25+
# Score bonus: high scores get up to 3 hours of extra buffer
26+
# Normalized assuming L2 scores range 0-100
27+
# score 0 -> 0h bonus, score 50 -> 1.5h bonus, score 100 -> 3h bonus
28+
score_bonus = (base_score / 100) * 3.0
29+
30+
# Effective gravity: high scores decay slightly slower
31+
# score 0 -> gravity, score 100 -> gravity * 0.85
32+
gravity_reduction = (base_score / 100) * 0.15
33+
effective_gravity = gravity - gravity_reduction
34+
35+
# Combined offset
36+
total_offset = base_offset + score_bonus
37+
38+
# Final formula
39+
return base_score / pow((age_hours + total_offset), effective_gravity)

0 commit comments

Comments
 (0)