Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Refactor calc_rising() and adjust rising algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
alienth committed Feb 27, 2014
1 parent dd67f3a commit 060de64
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
4 changes: 2 additions & 2 deletions r2/r2/lib/recommender.py
Expand Up @@ -208,8 +208,8 @@ def get_hot_items(srs, item_type, src):
def get_rising_items(omit_sr_ids, count=4):
"""Get links that are rising right now."""
all_rising = rising.get_all_rising()
candidate_sr_ids = {sr_id for link, sr_id in all_rising}.difference(omit_sr_ids)
link_fullnames = [link for link, sr_id in all_rising if sr_id in candidate_sr_ids]
candidate_sr_ids = {sr_id for link, score, sr_id in all_rising}.difference(omit_sr_ids)
link_fullnames = [link for link, score, sr_id in all_rising if sr_id in candidate_sr_ids]
link_fullnames_to_show = random_sample(link_fullnames, count)
rising_links = Link._by_fullname(link_fullnames_to_show,
return_dict=False,
Expand Down
36 changes: 13 additions & 23 deletions r2/r2/lib/rising.py
Expand Up @@ -32,32 +32,22 @@


def calc_rising():
sr_count = count.get_link_counts()
link_count = dict((k, v[0]) for k,v in sr_count.iteritems())
link_names = Link._by_fullname(sr_count.keys(), data=True)
link_counts = count.get_link_counts()

#max is half the average of the top 10 counts
counts = link_count.values()
counts.sort(reverse=True)
maxcount = sum(counts[:10]) / 20
links = Link._by_fullname(link_counts.keys(), data=True)

#prune the list
rising = [(n, link_names[n].sr_id)
for n in link_names.keys() if link_count[n] < maxcount]
def score(link):
count = link_counts[link._fullname][0]
return float(link._ups) / max(count, 1)

cur_time = datetime.now(g.tz)
# build the rising list, excluding items having 1 or less upvotes
rising = []
for link in links.values():
if link._ups > 1:
rising.append((link._fullname, score(link), link.sr_id))

def score(pair):
name = pair[0]
link = link_names[name]
hours = (cur_time - link._date).seconds / 3600 + 1
return float(link._ups) / (max(link_count[name], 1) * hours)

def r(x):
return 1 if x > 0 else -1 if x < 0 else 0

rising.sort(lambda x, y: r(score(y) - score(x)))
return rising
# return rising sorted by score
return sorted(rising, key=lambda x: x[1], reverse=True)


def set_rising():
Expand All @@ -70,4 +60,4 @@ def get_all_rising():

def get_rising(sr):
rising = get_all_rising()
return [link for link, sr_id in rising if sr.keep_for_rising(sr_id)]
return [link for link, score, sr_id in rising if sr.keep_for_rising(sr_id)]

0 comments on commit 060de64

Please sign in to comment.