def _get_results(self): if len(self.recent_albums) < 5: return {"error":"no or too little data"} recent_album_dict = dict([((a["artist"], a["name"]), a) for a in self.recent_albums]) topicsums = None topics = [] topic_influence = {} for dist in data.find_topics(map(lambda a: {"artist": a["artist"],"name": a["name"]}, self.recent_albums)): if not topicsums: topicsums = [0]*len(dist["distribution"]) full_album_info = recent_album_dict[(dist["artist"],dist["name"])] count = (int(full_album_info["playcount"]))**0.5 topicsums = map(lambda (a,b): a+b*count, zip(topicsums, dist["distribution"])) for topic_index, dist_value in enumerate(dist["distribution"]): influence = dist_value * count if influence<0.1: continue topic_influence.setdefault(topic_index,[]).append((influence,full_album_info)) top_topics = filter(lambda (e,t): t>0, sorted(enumerate(topicsums), key=lambda (e,t): t, reverse=True)) for topic_index, score in top_topics[:10]: topic = (topic_index,{"new":[]}) for album in data.get_top_of_topic(topic_index): if (album["artist"],album["name"]) in self.all_albums: continue topic[1]["new"].append(album) topic[1]["exist"] = [a for count, a in sorted(topic_influence[topic_index],reverse=True)[:10]] topics.append(topic) return topics
def retrieve_results_for_topic(self, topic, offset, past_album): for a in data.get_user_top_albums(self.name,"overall"): self.all_albums.add((a["artist"],a["name"])) results = [] past = False for album in data.get_top_of_topic(topic, limit=100, offset=offset-1): if not past: if album["name"]==past_album: past=True continue if (album["artist"],album["name"]) in self.all_albums: continue results.append(album) return results