1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| def get_up(item_cate, input_file): """ :param item_cate: key itemid, value:dict, key category value ratio :param input_file: user rating file :return: a dict: key userid, value [(category, ratio),(category1, ratio1)] """ if not os.path.exists(input_file): return {} linenum = 0 score_thr = 4.0 topk = 2 record = {} up = {} fp = open(input_file) for line in fp: if linenum == 0: linenum += 1 continue item = line.strip().split(",") if len(item) < 4: continue userid, itemid, rating, timestamp = item[0], item[1], float(item[2]), int(item[3]) if rating < score_thr: continue if itemid not in item_cate: continue time_score = get_time_score(timestamp) if userid not in record: record[userid] = {} for fix_cate in item_cate[itemid]: if fix_cate not in record[userid]: record[userid][fix_cate] = 0 record[userid][fix_cate] += rating * time_score * item_cate[itemid][fix_cate] fp.close() for userid in record: if userid not in up: up[userid] = [] total_score = 0 for co in sorted(record[userid].items(), key=operator.itemgetter(1), reverse=True)[:topk]: up[userid].append((co[0], co[1])) total_score += co[1] for index in range(len(up[userid])): up[userid][index] = (up[userid][index][0], round(up[userid][index][1] / total_score, 3)) return up
|