def load_from_dict(cls, path=path): file = open(path, 'r') lines = file.readlines() for line in lines: line = to_unicode(line[:-1]) key = '%s%s' %(TAG_CACHE_KEY, line) if not cache.exists(key): cache.incr(key, amount=1) print("Done loads") file.close()
def loads_from_pymongo(cls): tem_key = 'TEMP:KEY:' items = db.tags.find() for item in items: for tag in item['tags']: print("incr tag name %s" %tag) key = tem_key + tag cache.incr(name=key, amount=1) keys = cache.keys(pattern='%s*' %tem_key) for key in keys: name = key[9:].decode('utf-8') score = float(cache.get(key)) cls.create_tag(name=name, score=score) cache.delete(key)
def create_action(self): cache_key = "WEIBO:HOT:%s" %self.user.sns_id cache.delete(cache_key) tmp_cache_key = "TEMP:WEIBO:HISTORY:%s:::" %self.user.sns_id weibo_history = self.user.weibo_history for text in weibo_history: terms = seg_txt(text.encode('utf-8')) for term in terms: index_key = '%s%s' %(BASIC_TAG_PREFIX, term) if cache.exists(index_key): key = tmp_cache_key + term.decode('utf-8') cache.incr(name=key, amount=1) keys = cache.keys(pattern="%s*" %tmp_cache_key) for key in keys: name = key.split(":::")[1] value = float(cache.get(key)) cache.zadd(cache_key, value, name) cache.delete(key) tag = BasicTag.get_by_name(name=name) if not tag: continue relations = tag.friends score = tag.score for f in relations: items = f.split(':::') obj_name = items[0] obj_value = float(items[1]) result = obj_value/50*value cache.zadd(cache_key, result, obj_name) results = cache.zrevrange(name=cache_key, start=0, num=30, withscores=True) tags = [result[0].decode('utf-8') +'__' + str(result[1]) for result in results] self.user.update(set__tags=tags)
def generate_basic_tags(): Tag.clear_top_tags() for basic in BASIC_TAGS: delete_keys() items = db.tags.find() for item in items: print("processed item %s" %item['name']) if not basic in item['tags']: continue for tag in item['tags']: key = BASIC_CACHE_PREFIX + basic + ':' + tag cache.incr(key, amount=1) results = Tag.see_top_tags() Tag.loads_basic_tags_to_cache() delete_keys()
def __calculate_relation(cls, obj_tag, item_name, num=3): calculate = cls.objects(name=obj_tag) if calculate: if calculate[0].friends: return if num == 0: return item = db.tags.find_one({'name': item_name}) if not item: return item_tags = item['tags'] damp = round(float(DAMP ** (4 - num)), 3)*1000 for item_tag in item_tags: cache.incr('TEMP:FRIENDS:%s:::%s' %(obj_tag, item_tag), amount=int(damp)) friends = item['items'] for friend in friends: cls.__calculate_relation(obj_tag, friend, num=num-1)
def add_keyword(cls, word): word_utf8 = to_unicode(word) key = '%s%s' %(TAG_CACHE_KEY, word_utf8) if not cls.is_keyword(word): cache.incr(key, amount=1)
def load_tag(tag): key = 'TEMP:TOP:TAG:' + tag cache.incr(name=key, amount=1)