Commit 83264ece authored by 段英荣's avatar 段英荣

modify

parent 3cf49537
......@@ -47,18 +47,19 @@ class CollectData(object):
def update_recommend_tag_list(self, device_id,user_feature=None):
try:
recommend_tag_list = list()
recommend_tag_dict = dict()
redis_linucb_tag_data_dict = self._get_user_linucb_info(device_id)
if len(redis_linucb_tag_data_dict) == 0:
recommend_tag_list = LinUCB.get_default_tag_list()
LinUCB.init_device_id_linucb_info(redis_client, self.linucb_matrix_redis_prefix,device_id,recommend_tag_list)
else:
user_feature = user_feature if user_feature else self.user_feature
recommend_tag_list = LinUCB.linucb_recommend_tag(device_id,redis_linucb_tag_data_dict,user_feature,list(redis_linucb_tag_data_dict.keys()))
recommend_tag_dict = LinUCB.linucb_recommend_tag(device_id,redis_linucb_tag_data_dict,user_feature,list(redis_linucb_tag_data_dict.keys()))
logging.info("duan add,device_id:%s,recommend_tag_list:%s" % (str(device_id), str(recommend_tag_list)))
if len(recommend_tag_list) > 0:
if len(recommend_tag_dict) > 0:
tag_recommend_redis_key = self.linucb_recommend_redis_prefix + str(device_id)
redis_client.set(tag_recommend_redis_key, json.dumps(recommend_tag_list))
redis_client.set(tag_recommend_redis_key, json.dumps(recommend_tag_dict))
# Todo:设置过期时间,调研set是否支持
redis_client.expire(tag_recommend_redis_key, 7*24*60*60)
......
......@@ -70,7 +70,7 @@ class LinUCB:
#art_max = tag_list[np.argmax(np.dot(xaT, theta_tmp) + cls.alpha * np.sqrt(np.dot(np.dot(xaT, AaI_tmp), xa)))]
top_tag_set = set()
top_tag_dict = dict()
np_score_list = list()
np_score_dict = dict()
......@@ -85,18 +85,19 @@ class LinUCB:
sorted_np_score_list = sorted(np_score_list,reverse=True)
for top_score in sorted_np_score_list:
for top_score_index in np_score_dict[top_score]:
top_tag_set.add(str(tag_list[top_score_index], encoding="utf-8"))
if len(top_tag_set) >= 100:
tag_id = str(tag_list[top_score_index], encoding="utf-8")
top_tag_dict[tag_id] = top_score
if len(top_tag_dict) >= 50:
break
if len(top_tag_set) >= 100:
if len(top_tag_dict) >= 50:
break
logging.info("duan add,device_id:%s,sorted_np_score_list:%s,np_score_dict:%s" % (str(device_id), str(sorted_np_score_list), str(np_score_dict)))
return list(top_tag_set)
return top_tag_dict
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []
return {}
@classmethod
def init_device_id_linucb_info(cls, redis_cli,redis_prefix, device_id, tag_list):
......
......@@ -121,7 +121,7 @@ class TopicUtils(object):
@classmethod
def get_recommend_topic_ids(cls,user_id,tag_id,offset,size,single_size,query=None,query_type=TopicPageType.FIND_PAGE,
filter_topic_id_list=[],test_score=False,must_topic_id_list=[],recommend_tag_list=[],
filter_topic_id_list=[],test_score=False,must_topic_id_list=[],recommend_tag_dict={},
user_similar_score_list=[],index_type="topic"):
"""
:需增加打散逻辑
......@@ -224,12 +224,11 @@ class TopicUtils(object):
"weight": 1
}
)
if len(recommend_tag_list) > 0:
for tag_id in recommend_tag_dict:
functions_list.append(
{
"filter": {"bool": {
"should": {"terms": {"tag_list": recommend_tag_list}}}},
"weight": 3
"filter": {"term": {"tag_list": tag_id}},
"weight": 3+recommend_tag_dict[tag_id]
}
)
......@@ -320,6 +319,11 @@ class TopicUtils(object):
"order": "desc"
}
},
# {
# "offline_score":{
# "order": "desc"
# }
# },
"_score"
]
result_dict = ESPerform.get_search_results(ESPerform.get_cli(), sub_index_name=index_type, query_body=q,
......
......@@ -57,10 +57,10 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
tag_recommend_redis_key = "physical:linucb:tag_recommend:device_id:" + str(device_id)
recommend_tag_list = []
recommend_tag_dict = dict()
tag_recommend_val = redis_client.get(tag_recommend_redis_key)
if tag_recommend_val:
recommend_tag_list = json.loads(str(tag_recommend_val, encoding="utf-8"))
recommend_tag_dict = json.loads(str(tag_recommend_val, encoding="utf-8"))
recommend_topic_ids = []
have_read_topic_id_list = list()
......@@ -80,7 +80,7 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
topic_id_list = TopicUtils.get_recommend_topic_ids(user_id=user_id, tag_id=tag_id, offset=offset, size=size,
single_size=size,query=query, query_type=query_type,
filter_topic_id_list=have_read_topic_id_list,
recommend_tag_list=recommend_tag_list,
recommend_tag_dict=recommend_tag_dict,
user_similar_score_list=user_similar_score_redis_list,index_type="topic-high-star")
have_read_group_id_set = set()
have_read_user_id_set = set()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment