personas.py 1.81 KB
import json

from utils.cache import redis_client2


def get_user_portrait_tag3_redis_key(device_id):
    return "doris:user_portrait:tag3:device_id:" + str(device_id)


def get_user_portrait_tag3_from_redis(device_id, limit_score=0, tags_num=5):
    def items_gt_score(d, tags_num=tags_num):
        new_d = dict(sorted(d.items(), key=lambda x: x[1], reverse=True)[:tags_num])
        res = {tag: float(score) for tag, score in new_d.items() if float(score) >= limit_score}
        return list(res.keys())

    portrait_key = get_user_portrait_tag3_redis_key(device_id)
    if redis_client2.exists(portrait_key):
        user_portrait = json.loads(redis_client2.get(portrait_key))
        first_demands = items_gt_score(user_portrait.get("first_demands", {}))
        second_demands = items_gt_score(user_portrait.get("second_demands", {}))
        first_solutions = items_gt_score(user_portrait.get("first_solutions", {}))
        second_solutions = items_gt_score(user_portrait.get("second_solutions", {}))
        first_positions = items_gt_score(user_portrait.get("first_positions", {}))
        second_positions = items_gt_score(user_portrait.get("second_positions", {}))
        projects = items_gt_score(user_portrait.get("projects", {}))
        anecdote_tags = items_gt_score(user_portrait.get("anecdote_tags", {}))
        business_tags = items_gt_score(user_portrait.get("business_tags", {}))
        return {
            "first_demands": first_demands,
            "second_demands": second_demands,
            "first_solutions": first_solutions,
            "second_solutions": second_solutions,
            "first_positions": first_positions,
            "second_positions": second_positions,
            "projects": projects,
            "anecdote_tags": anecdote_tags,
            "business_tags": business_tags
        }
    return {}