From 060de64a57e94b8bb625d7451d81ac9d10cac410 Mon Sep 17 00:00:00 2001 From: Jason Harvey Date: Thu, 20 Feb 2014 13:08:09 -0800 Subject: [PATCH] Refactor calc_rising() and adjust rising algorithm. --- r2/r2/lib/recommender.py | 4 ++-- r2/r2/lib/rising.py | 36 +++++++++++++----------------------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/r2/r2/lib/recommender.py b/r2/r2/lib/recommender.py index 6f7f82d794..e48b5a2250 100644 --- a/r2/r2/lib/recommender.py +++ b/r2/r2/lib/recommender.py @@ -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, diff --git a/r2/r2/lib/rising.py b/r2/r2/lib/rising.py index cacc3036c2..f2b178d016 100644 --- a/r2/r2/lib/rising.py +++ b/r2/r2/lib/rising.py @@ -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(): @@ -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)]