|
2 | 2 |
|
3 | 3 | def calculate_gravity_score(base_score: float, published_at_ts: float, gravity: float = 1.8) -> float: |
4 | 4 | """ |
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. |
7 | 16 | """ |
8 | 17 | now = time.time() |
9 | 18 |
|
10 | 19 | # Calculate age in hours |
11 | | - # Ensure age is at least 0 to avoid issues with future timestamps (clock skew) |
12 | 20 | age_hours = max(0, (now - published_at_ts) / 3600) |
13 | 21 |
|
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