def get_color_list(item_id): cur = get_cursor() cid_list = [] for sty_id, in list(cur.execute("SELECT id FROM diver_color WHERE item_id=?", (item_id,))): cid_list.append(sty_id) return cid_list
def get_color(color_id): cur = get_cursor() cur.execute("SELECT color_id1, color_ratio1, color_id2, color_ratio2, color_id3, color_ratio3 \ FROM diver_color WHERE id=?", (color_id,)) temp = cur.fetchone() cid, c_ratio = temp[::2], temp[1::2] return cid, c_ratio
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 get_color(color_id): cur = get_cursor() cur.execute( "SELECT color_id1, color_ratio1, color_id2, color_ratio2, color_id3, color_ratio3 \ FROM diver_color WHERE id=?", (color_id, )) temp = cur.fetchone() cid, c_ratio = temp[::2], temp[1::2] return cid, c_ratio
def get_color_list(item_id): cur = get_cursor() cid_list = [] for sty_id, in list( cur.execute("SELECT id FROM diver_color WHERE item_id=?", (item_id, ))): cid_list.append(sty_id) return cid_list
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 _get_item_type(item_id): cur = get_cursor() cur.execute("SELECT type, category, pattern, collar, padding FROM diver_item WHERE id=?", (item_id,)) temp = cur.fetchone() ty = temp[0] vec_i = [(0, temp[1], temp[2]), (1, temp[1], temp[3], temp[4]), (2, temp[1]), (3, temp[1])][ty] return vec_i
def init_match_data(): cur = get_cursor() dataSet = [] mats = list(cur.execute("SELECT outer1_id, outer2_id, top1_id, top2_id, bottom_id, shoes_id FROM diver_match")) for m in mats: vec_m = [] for i in m: if i != None and type(i) != str: item_id = color._get_item_id(i) vec_m.append(_get_item_type(item_id)) dataSet.append(frozenset(vec_m)) # caching return dataSet
def _init_color_data(): cur = get_cursor() colorSet = [] vec_c = None rows = list(cur.execute("SELECT outer1_id, outer2_id, top1_id, top2_id, bottom_id, shoes_id FROM diver_match")) for m in rows: vec_c = set([]) for i in m: if i != None and type(i) != str: vec_c |= _get_color_type(i) colorSet.append(vec_c) # caching return colorSet
def _get_color_data(match_id): cur = get_cursor() ColorSet = [] cData = list(cur.execute("SELECT outer1_id, outer2_id, top1_id, \ top2_id, bottom_id, shoes_id \ FROM diver_match WHERE id=?", (match_id,))) for m in cData: vec_m = [] for i in m: if i != None: vec_m.append(_get_color_type(i)) ColorSet.append(vec_m) # caching return ColorSet
def main(): cur = get_cursor() conn = sql.connect("../diver/db.sqlite3") cur = conn.cursor() init_color() init_category() mc = get_mc() print("MATCH SET:") print(mc.get("MATCH")) print() print("Caculated Item sets:") print(mc.get("mItemSet"))
def _get_match_data(match_id): cur = get_cursor() dataSet = [] mData = list(cur.execute("SELECT outer1_id, outer2_id, top1_id, \ top2_id, bottom_id, shoes_id \ FROM diver_match WHERE id=?", (match_id,))) for m in mData: vec_m = [] for i in m: if i != None: item_id = color._get_item_id(i) vec_m.append(_get_item_type(item_id)) dataSet.append(frozenset(vec_m)) # caching return dataSet
def hanger_complete(hanger, customer_id): cur = get_cursor() i_hanger = hanger h_set = set(map(_get_item_type, i_hanger)) candDict = _hanger_getMatch(h_set) if len(candDict) == 0: return None ratings = list(cur.execute("SELECT score, match_id FROM diver_rating WHERE customer_id=?", (customer_id,))) for r, mid in ratings: s = frozenset(_get_match_data(mid)) try: candDict[s] = candDict[s]*(r+2)/4 except KeyError: pass maxMatch = set(max(candDict, key=candDict.get)) return frozenset(maxMatch-h_set)
def _init_color_data(): cur = get_cursor() colorSet = [] vec_c = None rows = list( cur.execute( "SELECT outer1_id, outer2_id, top1_id, top2_id, bottom_id, shoes_id FROM diver_match" )) for m in rows: vec_c = set([]) for i in m: if i != None and type(i) != str: vec_c |= _get_color_type(i) colorSet.append(vec_c) # caching return colorSet
def _get_color_data(match_id): cur = get_cursor() ColorSet = [] cData = list( cur.execute( "SELECT outer1_id, outer2_id, top1_id, \ top2_id, bottom_id, shoes_id \ FROM diver_match WHERE id=?", (match_id, ))) for m in cData: vec_m = [] for i in m: if i != None: vec_m.append(_get_color_type(i)) ColorSet.append(vec_m) # caching return ColorSet
def _cal_subscore(item_id, customer_id): cur = get_cursor() cur.execute("SELECT type FROM diver_item WHERE id=?", (item_id,)) type_, = cur.fetchone() #top if type_ == 0: cur.execute("SELECT pattern, neck, sleeve_level \ FROM diver_item WHERE id=?", (item_id,)) p, n, s = cur.fetchone() cur.execute("SELECT pattern_{}, neck_{}, sleeveT_{} FROM diver_pref \ WHERE customer_id=?".format(p, n, s),(customer_id,)) o_p, o_n, o_s = cur.fetchone() return 1.2**o_p + 1.2**o_n + 1.2**o_s #outer elif type_ == 1: # zipper, button, hat, length cur.execute("SELECT zipper, button, hat, length_level FROM diver_item WHERE id=?", (item_id,)) z, b, h, l = cur.fetchone() cur.execute("SELECT zipperO_{}, outer_button_{}, hatO_{}, outer_len_{} \ FROM diver_pref WHERE customer_id=?".format(z,b,h,l), (customer_id,)) o_z, o_b, o_h, o_l = cur.fetchone() return 1.2**o_z + 1.2**o_b + 1.2**o_h + 1.2**o_l #bottom elif type_ == 2: # color, fit cur.execute("SELECT fit FROM diver_item WHERE id=?", (item_id,)) f, = cur.fetchone() cur.execute("SELECT fit_{} FROM diver_pref WHERE \ customer_id=?".format(f), (customer_id,)) o_f, = cur.fetchone() return 1.2**o_f else: return 0
def update_preference(customer_id, item_id, new_rating, prev_rating=0): cur = get_cursor() rating = new_rating - prev_rating cur.execute("SELECT type FROM diver_item WHERE id=?", (item_id,)) type_, = cur.fetchone() # top if type_ == 0: # pattern, neck, sleeve_level cur.execute("SELECT pattern, neck, sleeve_level \ FROM diver_item WHERE id=?", (item_id,)) p, n, s = cur.fetchone() cur.execute("SELECT pattern_{}, neck_{}, sleeveT_{} FROM diver_pref \ WHERE customer_id=?".format(p, n, s),(customer_id,)) o_p, o_n, o_s = cur.fetchone() cur.execute("UPDATE diver_pref SET pattern_{}=?, neck_{}=?,\ sleeveT_{}=? WHERE customer_id=?".format(p, n, s), (o_p + rating, o_n + rating, o_s + rating, customer_id)) # outer elif type_ == 1: # zipper, button, hat, length cur.execute("SELECT zipper, button, hat, length_level FROM diver_item WHERE id=?", (item_id,)) z, b, h, l = cur.fetchone() cur.execute("SELECT zipperO_{}, outer_button_{}, hatO_{}, outer_len_{} \ FROM diver_pref WHERE customer_id=?".format(z,b,h,l), (customer_id,)) o_z, o_b, o_h, o_l = cur.fetchone() cur.execute("UPDATE diver_pref SET zipperO_{}=?, outer_button_{}=?, \ hatO_{}=?, outer_len_{}=? WHERE customer_id=?".format(z,b,h,l), (o_z+rating, o_b+rating, o_h+rating, o_l+rating, customer_id)) # bottom elif type_ == 2: # fit cur.execute("SELECT fit FROM diver_item WHERE id=?", (item_id,)) f, = cur.fetchone() cur.execute("SELECT fit_{} FROM diver_pref WHERE \ customer_id=?".format(f),(customer_id,)) o_f, = cur.fetchone() cur.execute("UPDATE diver_pref SET fit_{}=? WHERE \ customer_id=?".format(f),(o_f+rating, customer_id))
def _get_item_id(color_id): cur = get_cursor() cur.execute("SELECT item_id \ FROM diver_color WHERE id=?", (color_id, )) item_id, = cur.fetchone() return item_id
def _get_item_id(color_id): cur = get_cursor() cur.execute("SELECT item_id \ FROM diver_color WHERE id=?", (color_id,)) item_id, = cur.fetchone() return item_id