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 filter_tags(): tags = db.tags.find() for tag in tags: remove = True items = tag['tags'] for item in items: key = BASIC_TAG_PREFIX + item if cache.exists(key): remove = False break if remove: db.tags.remove({'_id': tag['_id']}) else: print("retain tag %s" %tag['name'])
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 is_keyword(cls, word): word_utf8 = to_unicode(word) key = '%s%s' %(TAG_CACHE_KEY, word_utf8) return cache.exists(key)