def _score_item(hanger, user_id, item_id, weight): mc = get_mc() USERS = mc.get("USERS") ITEMS = mc.get("ITEMS") u_idx = USERS.index(user_id) i_idx = ITEMS.index(item_id) cur = get_cursor() mc = get_mc() cr_read = mc.get("cr_read") cr_mutex = MemcacheMutex("cr_mutex", mc) cr_write = MemcacheMutex("cr_write", mc) with cr_mutex: cr_read += 1 if cr_read == 1: cr_write.acquire() COMP_RATING = mc.get("COMP_RATING") try: score = weight[0]* ( COMP_RATING[u_idx][i_idx] + 2 )/ 4 except IndexError: score = 0 with cr_mutex: cr_read -= 1 if cr_read == 0: cr_write.release() points = [] if len(hanger) != 0: points = hanger_getColor(hanger) color_list = get_color_list(item_id) color_score = {} for sty in color_list: cids, c_ratios = get_color(sty) color_d = 0 for cid, ratio in zip(cids, c_ratios): if ratio == 0: break color_d += eval_color(points, cid) * ratio / 100 color_score[sty] = color_d try: max_sty = max(color_score, key=color_score.get) score += weight[1]* color_score[max_sty] except ValueError: max_sty = None score += weight[1] cur.execute("SELECT rate_count FROM diver_item WHERE id=?",(item_id,)) rate_count = cur.fetchone()[0] if rate_count > LIKE_MAX: rate_count = LIKE_MAX score += weight[2]* rate_count / LIKE_MAX return score, max_sty
def init_rating(): cur = get_cursor() mc = get_mc() custs = list(cur.execute("SELECT id FROM diver_customer ORDER BY id")) USERS = [-1] for r in custs: USERS.append(r[0]) mc.set("USERS", USERS) its = list(cur.execute("SELECT id FROM diver_item ORDER BY id")) ITEMS = [] for r in its: ITEMS.append(r[0]) mc.set("ITEMS", ITEMS) mc.set("RATING", dok_matrix((len(USERS), len(ITEMS)), dtype=np.float), 0) rats = list(cur.execute("SELECT customer_id, item_id, score FROM diver_itempref")) RATING = mc.get("RATING") for u_id, i_id, rating in rats: RATING[(USERS.index(u_id),ITEMS.index(i_id))] = rating for i in range(len(ITEMS)): RATING[(0,i)] = 1 mc.set("RATING", RATING, 0) mc.set("ch_count", 0, 0) mc.set("cr_read", 0, 0) mc.set("COMP_RATING", None, 0) th_r = threading.Thread(None, _rating_refresh, "rate_refresh") th_r.start()
def _hanger_getMatch(h_set): mc = get_mc() cand = {} for match, sup in mc.get("mItemSet"): if h_set < set(match): cand[frozenset(match)]=sup return cand
def main(): conn = sql.connect("../diver/db.sqlite3") cur = conn.cursor() init_color() mc = get_mc() print("MATCH SET:") print(mc.get("COLOR")) print() print("Caculated Item sets:") print(mc.get("cItemSet"))
def hanger_getColor(hanger): h_set = set([]) for c in map(_get_color_type, hanger): h_set |= c cand = {} mc = get_mc() cItemSet = mc.get("cItemSet") for match, sup in cItemSet: if h_set < set(match): cand[frozenset(match)] = sup result = set([]) for i in sorted(cand, key=cand.get)[:3]: result |= i return result
def hanger_getColor(hanger): h_set = set([]) for c in map(_get_color_type, hanger): h_set |= c cand = {} mc = get_mc() cItemSet = mc.get("cItemSet") for match, sup in cItemSet: if h_set < set(match): cand[frozenset(match)]=sup result = set([]) for i in sorted(cand, key=cand.get)[:3]: result |= i return result
def rating_remove_user(user_id): mc = get_mc() r_mutex = MemcacheMutex("r_mutex", mc) ch_lock = MemcacheMutex("ch_lock", mc) ITEMS = mc.get("ITEMS") with r_mutex: USERS = mc.get("USERS") RATING = mc.get("RATING") USERS.remove(user_id) RATING.resize((len(USERS), len(ITEMS))) mc.set("USERS", USERS) mc.set("RATING", RATING) with ch_lock: ch_count = mc.get("ch_count") ch_count += FLUSH_MIN mc.set("ch_count", ch_count)
def rating_fill(user_id, item_id, rating): mc = get_mc() ch_lock = MemcacheMutex("ch_lock", mc) USERS = mc.get("USERS") ITEMS = mc.get("ITEMS") r_mutex = MemcacheMutex("r_mutex", mc) u_idx, i_idx = USERS.index(user_id), ITEMS.index(item_id) with r_mutex: RATING = mc.get("RATING") RATING[(u_idx,i_idx)] = rating mc.set("RATING", RATING) with ch_lock: ch_count = mc.get("ch_count") ch_count += 1 mc.set("ch_count", ch_count)
def rating_remove_item(item_id): #Q.put(('i-', item_id)) mc = get_mc() r_mutex = MemcacheMutex("r_mutex", mc) ch_lock = MemcacheMutex("ch_lock", mc) USERS = mc.get("USERS") with r_mutex: ITEMS = mc.get("ITEMS") RATING = mc.get("RATING") ITEMS.remove(item_id) RATING.resize((len(USERS), len(ITEMS))) mc.set("ITEMS", ITEMS) mc.set("RATING", RATING) with ch_lock: ch_count = mc.get("ch_count") ch_count += FLUSH_MIN mc.set("ch_count", ch_count)
def _rating_refresh(): mc = get_mc() ch_count = mc.get("ch_count") cr_write = MemcacheMutex("cr_write", mc) ch_lock = MemcacheMutex("ch_lock", mc) RATING = mc.get("RATING") LRMC = MatrixCompletion(RATING) LRMC.complete_it("sASD") with cr_write: COMP_RATING = LRMC.get_optimized_matrix() mc.set("COMP_RATING", COMP_RATING) while True: time.sleep(SLEEP_TIME) RATING = mc.get("RATING") LRMC = MatrixCompletion(RATING) ch_count = mc.get("ch_count") with ch_lock: if ch_count < FLUSH_MIN: continue ch_count = 0 mc.set("ch_count", ch_count) LRMC.complete_it("sASD") COMP_RATING = LRMC.get_optimized_matrix() mc.set("COMP_RATING", COMP_RATING)
def init_color(): mc = get_mc() COLOR = _init_color_data() mc.set("COLOR", COLOR, 0) mc.set("cItemSet", list(find_frequent_itemsets(COLOR, 1, True)), 0)
def init_category(): mc = get_mc() mc.set("MATCH", init_match_data(), 0) mc.set("mItemSet", list(find_frequent_itemsets(mc.get("MATCH"), 1, True)), 0)