# Ordering similarity def calc_ordering_sim(ordering1, ordering2, matrix): return similarity(ordering1, ordering2, matrix) # Cuisine similarity def calc_cuisine_sim(cuisine1, cuisine2, matrix): return similarity(cuisine1, cuisine2, matrix) if __name__ == '__main__': # 直接執行r2R.py的時候,輸出餐廳相似矩陣的csv from db import DBConn conn = DBConn() conn.open() restaurants_num = conn.getRestaurantsNum() order_sim_matrix = conn.getOrderingSimMatrix() cuisine_sim_matrix = conn.getCuisineSimMatrix() num = len(restaurants_num) similarities = [] for i, restaurant1 in enumerate(restaurants_num): for j in range(i, num): restaurant2 = restaurants_num[j] sim = calc_r2r(restaurant1, restaurant2, conn.getRestaurantDistance(restaurant1['rid'], restaurant2['rid']), order_sim_matrix, cuisine_sim_matrix) if restaurant1['rid'] != restaurant2['rid'] else 1 similarities.append((restaurant1['rid'], restaurant2['rid'], sim)) conn.close()
""" def calc_ordering_sim(u1, u2): return similarity(u1, u2) def calc_cuisine_sim(u1, u2): return similarity(u1, u2) if __name__ == '__main__': from db import DBConn conn = DBConn() conn.open() # output similarity users_info = conn.getUsersInfo() num = len(users_info) similarities = [] u2u_weights = { 'price': 0.25, 'ordering': 0.25, 'cuisine': 0.25, 'tag': 0.25 } for i, user1 in enumerate(users_info): weights = conn.getWeightWithID(user1['uid']) for j in range(i, num): user2 = users_info[j] sim = calc_u2u(user1, user2, u2u_weights)