Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor calc_rising() and adjust rising algorithm.

  • Loading branch information...
commit 060de64a57e94b8bb625d7451d81ac9d10cac410 1 parent dd67f3a
@alienth alienth authored
Showing with 15 additions and 25 deletions.
  1. +2 −2 r2/r2/lib/recommender.py
  2. +13 −23 r2/r2/lib/rising.py
View
4 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,
View
36 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)]
Please sign in to comment.
Something went wrong with that request. Please try again.