In this post I'll try to explain how Hacker News ranking algorithm works and how you can reuse it in your own applications. It's a very simple ranking algorithm and works surprising well when you want to highlight hot or new stuff.

这篇文章我要向大家介绍Hacker News网站的文章排名算法工作原理,以及如何在自己的应用里使用这种算法。这个算法非常的简单,但却在突出热门文章和遴选新文章上表现的异常优秀。

Digging into news.arc code

Hacker News is implemented in Arc, a Lisp dialect coded by Paul Graham. Hacker News is opensource and the code can be found at arclanguage.org. Digging through the news.arc code you can find the ranking algorithm which looks like this:

深入 news.arc 程序代码
Hacker News是用Arc语言开发的,这是一种Lisp方言,由Y Combinator投资公司创始人Paul Graham创造。Hacker News的开源的,你可以在arclanguage.org找到它的源代码。深入发掘 news.arc 程序,你会找到这段排名算法代码,就是下面这段:

; Votes divided by the age in hours to the gravityth power.
; Would be interesting to scale gravity in a slider. (= gravity* 1.8 timebase* 120 front-threshold* 1
nourl-factor* .4 lightweight-factor* .3 ) (def frontpage-rank (s (o scorefn realscore) (o gravity gravity*))
(* (/ (let base (- (scorefn s) 1)
(if (> base 0) (expt base .8) base))
(expt (/ (+ (item-age s) timebase*) 60) gravity))
(if (no (in s!type 'story 'poll)) 1
(blank s!url) nourl-factor*
(lightweight s) (min lightweight-factor*
(contro-factor s))
(contro-factor s))))

In essence the ranking performed by Hacker News looks like this:

本质上,这段 Hacker News采用的排名算法的工作原理看起来大概是这个样子:

Score = (P-1) / (T+2)^G

where,
P = points of an item (and -1 is to negate submitters vote)
T = time since submission (in hours)
G = Gravity, defaults to 1.8 in news.arc

As you see the algorithm is rather trivial to implement. In the upcoming section we'll see how the algorithm behaves.

Score = (P-1) / (T+2)^G
其中,
P = 文章获得的票数( -1 是去掉文章提交人的票)
T = 从文章提交至今的时间(小时)
G = 比重,news.arc里缺省值是1.8
正如你看到的,这个算法很容易实现。在下面的内容里,我们将会看到这个算法是如何工作的。

Effects of gravity (G) and time (T)

Gravity and time have a significant impact on the score of an item. Generally these things hold true:

  • the score decreases as T increases, meaning that older items will get lower and lower scores
  • the score decreases much faster for older items if gravity is increased

To see this visually we can plot the algorithm to Wolfram Alpha.

比重(G)和时间(T)对排名的影响
比重和时间在文章的排名得分上有重大的影响。正常情况下如下面所述:
当T增加时文章得分会下降,这就是说越老的文章分数会越底。
当比重加大时,老的文章的得分会减的更快
为了能视觉呈现这个算法,我们可以把它绘制到Wolfram Alpha。
得分随着时间是如何变化的

How score is behaving over time

As you can see the score decreases a lot as time goes by, for example a 24 hour old item will have a very low score regardless of how many votes it got.

Plot query:

你可以看到,随着时间的流逝,得分骤然下降,例如,24小时前的文章的分数变的非常低——不管它获得了如何多的票数。
Plot语句:

plot(
(30 - 1) / (t + 2)^1.8,
(60 - 1) / (t + 2)^1.8,
(200 - 1) / (t + 2)^1.8
) where t=0..24

How gravity parameter behaves

比重参数是如何影响排名的

As you can see by the graph the score decreases a lot faster the larger the gravity is.

Plotting query:

图中你可以看到,比重越大,得分下降的越快。
Plot语句:

plot(
(p - 1) / (t + 2)^1.8,
(p - 1) / (t + 2)^0.5,
(p - 1) / (t + 2)^2.0
) where t=0..24, p=10

Python implementation

As already stated it's rather simple to implementing the score function:

def calculate_score(votes, item_hour_age, gravity=1.8):
return (votes - 1) / pow((item_hour_age+2), gravity)

The most crucial aspect is understanding how the algorithm behaves and how you can customize it for your application and I hope I have contributed that knowledge :-)

Happy hacking!

plot(
    (p - 1) / (t + 2)^1.8, 
    (p - 1) / (t + 2)^0.5,
    (p - 1) / (t + 2)^2.0
) where t=0..24, p=10
Python语言实现
之前已经说了,这个评分算法很容易实现:
def calculate_score(votes, item_hour_age, gravity=1.8):
    return (votes - 1) / pow((item_hour_age+2), gravity)
关键是要理解算法中的各个因素对评分的影响,这样你可以在你的应用中进行定制。我希望这篇文章已经向你说明了这些 
祝编程快乐!

Edit:
You can view comments to this post and a lot more thoughts on HN's ranking here:

Edit:
Paul Graham has shared the updated HN ranking algorithm:

    (= gravity* 1.8 timebase* 120 front-threshold* 1
nourl-factor* .4 lightweight-factor* .17 gag-factor* .1) (def frontpage-rank (s (o scorefn realscore) (o gravity gravity*))
(* (/ (let base (- (scorefn s) 1)
(if (> base 0) (expt base .8) base))
(expt (/ (+ (item-age s) timebase*) 60) gravity))
(if (no (in s!type 'story 'poll)) .8
(blank s!url) nourl-factor*
(mem 'bury s!keys) .001
(* (contro-factor s)
(if (mem 'gag s!keys)
gag-factor*
(lightweight s)
lightweight-factor*
1)))))

Hacker News网站的文章排名算法工作原理的更多相关文章

  1. Hacker News排名算法工作原理

    这篇文章我要向大家介绍Hacker News网站的文章排名算法工作原理,以及如何在自己的应用里使用这种算法,这个算法非常简单,但却在突出热门文章和遴选新文章上表现的非常优秀.本质上,这段Hacker ...

  2. 转:Reddit排名算法工作原理

    http://www.aqee.net/how-reddit-ranking-algorithms-work/ 这是一篇继<Hacker News 排名算法工作原理>之后的又一篇关于排名算 ...

  3. 如何更有效使用 Rational AppScan 扫描大型网站,第 1 部分: 工作原理及技术分析

    Rational AppScan 工作原理 Rational AppScan(简称 AppScan)其实是一个产品家族,包括众多的应用安全扫描产品,从开发阶段的源代码扫描的 AppScan sourc ...

  4. 【转】HashMap的工作原理

    很好的文章,推荐Java的一个好网站:ImportNew HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hasht ...

  5. 词向量( Distributed Representation)工作原理是什么

    原文:http://www.zhihu.com/question/21714667 4 个回答 83赞同反对,不会显示你的姓名 皮果提 刘鑫.莫教授要养猫.Starling Niohuru 等人赞同 ...

  6. 学习iis工作原理

    文章:IIs工作原理 文章:Asp.Net 构架(Http Handler 介绍) - Part.2

  7. 2015最新百度搜索引擎(seo优化)排名算法

    多少年来,对于弄清百度排名算法成为了一代又一代站长的最高目标.随着百度推出了搜索引擎网页质量**,直接揭开了神秘的百度排名算法,这是作为站长福音啊.现在小编就来为大家介绍一下. 首先想要得到直接需要的 ...

  8. 网站运营文章LIST

    如何建立网站地图(site map) ● 伤心SEO之太平洋 ● 关键字密度与网站排名 ● 哪些因素决定网站SEO的价格 ● SEO:站在Google背后的搜钱力量 ● 网站被一搜索屏蔽,郁闷! ●  ...

  9. 关系型数据库工作原理-归并排序(翻译自Coding-Geek文章)

    本文翻译自Coding-Geek文章:< How does a relational database work>. 原文链接:http://coding-geek.com/how-dat ...

随机推荐

  1. modelsim中的文件操作—— 大数据测试

    在modelsim中不可避免的需要进行文件操作,在窗口中查看代码的操作情况,下面是我自己M序列实验中的一段测试代码 integer i,j ,k,m; integer m_dataFILE , ind ...

  2. Android学习笔记:使用ViewPager组件实现图片切换

    在很多App中,尤其是第一次安装启动后,都会出现几个图片进行一些app的介绍和说明,图片可以随着滑动而切换. 我们这里利用 ViewPager组件来演示如何实现这一点. 1.创建一个app工程,默认创 ...

  3. Spark Core源代码分析: Spark任务模型

    概述 一个Spark的Job分为多个stage,最后一个stage会包含一个或多个ResultTask,前面的stages会包含一个或多个ShuffleMapTasks. ResultTask运行并将 ...

  4. 【Flume NG用户指南】(1)设置

    作者:周邦涛(Timen) Email:zhoubangtao@gmail.com 转载请注明出处:  http://blog.csdn.net/zhoubangtao/article/details ...

  5. sublime2 c++的一些使用配置

    1 下载安装好tdw gcc后,配置好环境变量后,配置sublime2. tools->build system-> new build system... 里面输入: { "c ...

  6. Http方式获取网络数据

    通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据.(sax解析主要是解析xml)具体代码如下: package com.wy ...

  7. 一天一个类,一点也不累 之 LinkedList

    我们的口号是,一天一个类,一点也不累 .. 今天要讲的是---LinkedList 首先,还是看看他的组织结构 Class LinkedList<E> java.lang.Object j ...

  8. Qt状态机框架

    The State Machine Framework 状态机框架提供了用于创建和执行状态图的类.概念和符号是基于Harel的Statecharts: A visual formalism for c ...

  9. 基于二叉树和数组实现限制长度的最优Huffman编码

    具体介绍详见上篇博客:基于二叉树和双向链表实现限制长度的最优Huffman编码 基于数组和基于链表的实现方式在效率上有明显区别: 编码256个符号,符号权重为1...256,限制长度为16,循环编码1 ...

  10. Android 应用程序签名

    本文主要介绍Android应用程序签名的相关理论知识以及怎样公布Android应用程序. 1.签名的概念 为大家所熟知的日常生活中的签名,它是代表某个人的特殊标记,用于唯一标识某个人.而Android ...