from datetime import datetime import pickle import json from config import logger from config import subject_xdeepfm_rank_dict from config import subject_bst_rank_dict from config import test_user_id from matching.subject.cold_start_recall import cold_start_user_recall logger.info('加载实训推荐离线排序字典') xdeepfm_rank_dict = pickle.load(open(subject_xdeepfm_rank_dict, 'rb')) bst_rank_dict = pickle.load(open(subject_bst_rank_dict, 'rb')) def subject_recommend_offline(user_id, disciplines_id_list=None, topk=100, rank_method='1'): """ 根据用户ID获取推荐的实训ID列表 直接从计算好的离线排序字典中取出数据返回 """ start_time = datetime.now() logger.info(f"本次需要进行推荐的用户ID: {user_id}") has_data = False rank_list = [] rank_list.clear() recommend_results = {} recommend_results.clear() # 如果离线排序字典中有此用户则直接取出 if rank_method == '1': if user_id in xdeepfm_rank_dict: has_data = True rank_list = xdeepfm_rank_dict[user_id] else: if user_id in bst_rank_dict: has_data = True rank_list = bst_rank_dict[user_id] # 没有召回数据则根据兴趣标签使用冷启动召回推荐 if has_data == False: recommend_results = cold_start_user_recall(user_id,disciplines_id_list, topk=topk) else: if topk > len(rank_list): topk = len(rank_list) # 取topk个返回 rank_list = rank_list[:topk] recommend_results = {subject_id: subject_name for subject_id, subject_name in rank_list} # 计算耗时毫秒 end_time = datetime.utcnow() cost_time_millisecond = round(float((end_time - start_time).microseconds / 1000.0), 3) logger.info(f"本次推荐总耗时: {cost_time_millisecond} 毫秒") return recommend_results if __name__ == '__main__': recommend_results = subject_recommend_offline(user_id=test_user_id, disciplines_id_list=[1,2], topk=100, rank_method=1) print(json.dumps(recommend_results, ensure_ascii=False, indent=4))