Adding negative score fix

- Fixes #84.
- Fixing unit tests.
This commit is contained in:
Dessalines 2019-04-20 16:47:23 -07:00
parent 8a4b2324a1
commit a05f53dae9
6 changed files with 8 additions and 6 deletions

View file

@ -12,14 +12,14 @@ The [Hacker New's ranking algorithm](https://medium.com/hacking-and-gonzo/how-ha
## My Algorithm
```
Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
Rank = ScaleFactor * sign(3 + Score) * log(abs(3 + Score)) / (Time + 2)^Gravity
Score = Upvotes - Downvotes
Time = time since submission (in hours)
Gravity = Decay gravity, 1.8 is default
```
- Add 1 to the score, so that the standard new comment score of +1 will be affected by time decay. Otherwise all new comments would stay at zero, near the bottom.
- Add 3 to the score, so that even minimally downvoted comments will be affected by time decay. Otherwise all new comments would stay at zero, near the bottom.
- The sign and abs of the score are necessary for dealing with the log of negative scores.
- A scale factor of 10k gets the rank in integer form.

View file

@ -5,7 +5,7 @@ create or replace function hot_rank(
returns integer as $$
begin
-- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
return floor(10000*sign(score)*log(1 + abs(score)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8))::integer;
return floor(10000*sign(3+score)*log(abs(3+score)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8))::integer;
end; $$
LANGUAGE plpgsql;

View file

@ -294,6 +294,7 @@ mod tests {
post_id: inserted_post.id,
parent_id: None,
removed: None,
read: None,
updated: None
};

View file

@ -465,6 +465,7 @@ mod tests {
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
read: None,
parent_id: None,
updated: None
};

View file

@ -259,7 +259,7 @@ mod tests {
score: 1,
upvotes: 1,
downvotes: 0,
hot_rank: 864,
hot_rank: 1728,
published: inserted_post.published,
updated: None,
subscribed: None,
@ -284,7 +284,7 @@ mod tests {
score: 1,
upvotes: 1,
downvotes: 0,
hot_rank: 864,
hot_rank: 1728,
published: inserted_post.published,
updated: None,
subscribed: None,

View file

@ -21,7 +21,7 @@ export function hotRank(comment: Comment): number {
let now: Date = new Date();
let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
let rank = (10000 * Math.sign(comment.score) * Math.log10(1 + Math.abs(comment.score))) / Math.pow(hoursElapsed + 2, 1.8);
let rank = (10000 * Math.sign(3+comment.score) * Math.log10(Math.abs(3+comment.score))) / Math.pow(hoursElapsed + 2, 1.8);
// console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);