Famous sorting algorithms based on vote popularity and time
This library houses 3 popularity estimating algorithms employed by bigger news sites used to sort for best content:
wilsonScore- Reddit's best comment scoring system
redditHot- Reddit's hot post scoring system for news posts
hackerHot- Hackernews' scoring system
Algorithms may cause scores to decay based on distance to post time.
Algorithms that are designed to decay based on time needs continual recomputation of scores. An example of doing so would be keeping track of, and periodically computing the score(s) required in a node process on a set of suitable candidates:
var decay = require'decay'hotScore = decayredditHot;setIntervalcandidates = ; // perhaps get recent posts saved in db herecandidatesforEachcscore = hotScorecupVotes cdnVotes cdate;// save so that next GET /entry/ gets an updated orderingsavec;;1000 * 60 * 5; // run every 5 minutes, say
Algorithms that produce a time agnostic popularity score is typically good for comments. For best results, simply recompute the score at every new vote:
var decay = require'decay'wilsonScore = decaywilsonScore;// assume req.entry is the item being voted onapppost'/entry/upvote' middleWare// call wilsonScore with ups, downs, post_date to recomputereqentryscore = wilsonScorereqentryupVotes + 1 reqentrydnVotes reqentrypostDate;// save new score in database so that new pageviews sortsavereqentry;;
Decay exports 3 scoring function factories.
Two of these algorithms decay with time, and the other is based purely on statistical popularity.
// 1. zero decayvar wilsonScore = decaywilsonScorezScore;var score = wilsonScoreupVotes downVotes;// 2. decaysvar redditHotScore = decayredditHothalflife;var score = redditHotScoreupVotes downVotes date;// 3. decaysvar hackerHotScore = decayhackerHotdecay;var score = hackerHotScoreupVotes date;
AKA Reddit's Best comment sorting system.
Statistically, it is the lower bound of the Wilson Score interval at the alpha level based on supplied Z score.
zScore parameter can be passed as to the exported
The Z score is a statistical value which roughly means how many standard deviations of safety you want, so it maps directly onto the confidence level of the Wilson Score interval.
It will default to
z=1.96 if left out, representing a
95% confidence level in the lower bound. Otherwise, values through
1.0 (69%), to
3.3 (99.9%) good alternatives.
Based on the difference between ups/downs, and decays with time. Causes hive mind effects in large crowds.
An optional halflife parameter can be passed to the exported
The half-life defaults to 45000 [s]. For info on the effects on this parameter read the original blog post about it.
Based on simply the amount of upvotes, and decays with time. Prone to advertising abuse.
gravity parameter (defaulting to
1.8) can be passed to the exported
hackerHot factory. For info on the effects of this parameter read the original blog post about it.
$ npm install decay
MIT-Licensed. See LICENSE file for details.