def get_recommendation(matrix_name, time_stamp, coordinates, user_id, ignore, number): """ Main handler function for recommend. :param time_stamp: Time from beginning of the epoch (1.1.1970) :param coordinates: Dictionary with coordinates. :param user_id: Identification of the current user. :param ignore: Identification of POI user ignored from previous recommend. :param number: Number of POIs to be recommended to end user. :return: Dictionary representing answer for request to recommend. """ # Get all POIs in radius of 300 meters from user. recommendation_matrix = read_matrix(matrix_name) points_of_interest = fetch_poi(matrix_name, recommendation_matrix['categories'], coordinates['lat'], coordinates['lon'], 300) # First we have to prepare necessary variables. length = len(points_of_interest[1]) poi_dict = dict() # Add temporary keys and merge the two dictionaries. poi_lst_of_dict = points_of_interest[0] poi_lst_foursquare = dict() for i in range(0, length): poi_lst_foursquare['some-temporary-key-%s-%d' % (user_id, (i+1))] = points_of_interest[1][i] chunk_lst = make_chunks(poi_lst_foursquare, int(length/15) + 1) threads = [] for chunk in chunk_lst: t = WritePOIToDatabase(chunk) threads.append(t) t.start() poi_lst_of_dict.update(poi_lst_foursquare) # Get all required data for reading correct element of weight matrix. act_rest_answer = get_user_activity(user_id) if 'error' in act_rest_answer: activity = 'not recognised' else: req_act = dict() for k, v in act_rest_answer['svm_vector'].iteritems(): req_act[k] = float(v) activity = get_curr_activity(req_act) # Get id of all poi and rate them based on activity, context and distance lookup_table = get_grades(recommendation_matrix, activity, time_stamp) for key, val in poi_lst_of_dict.iteritems(): val['fw_core']['category'] = val['fw_core']['label'][''] val['fw_core']['label'] = val['fw_core']['short_name'] val['fw_core']['short_name'] = val['fw_core']['name'] category_num = decode_category(recommendation_matrix['categories'], val['fw_core']['category']) f_res = lookup_table[category_num] s_res = grade_distance(coordinates['lat'], coordinates['lon'], val['fw_core']['location']['wgs84']['latitude'], val['fw_core']['location']['wgs84']['longitude']) poi_dict[key] = f_res * s_res # Slice out ignored. if ignore != 'None' and ignore in poi_dict: save_ignored_for_current_user(user_id, ignore) ignored = get_ignored_for_current_user(user_id) for ig_poi in ignored: if ig_poi in poi_dict: del poi_dict[ig_poi] elif ig_poi[:18] == 'some-temporary-key': key = Keys.objects.filter(temp=ig_poi, time__gte=timezone.now()-timedelta(minutes=30)) del poi_dict[key.real] # Sort POIs based on grades and return first 5 elements. sort_poi_lst = sorted(poi_dict.items(), key=operator.itemgetter(1), reverse=True) ret_dict = {"POIS": [], "activity": activity} if len(sort_poi_lst) > number: n_it = number else: n_it = len(sort_poi_lst) for num in range(0, n_it): ret_dict['POIS'].append({sort_poi_lst[num][0]: poi_lst_of_dict[sort_poi_lst[num][0]]}) # Delete old keys keys = Keys.objects.filter(time__lt=(timezone.now()-timedelta(minutes=30))) keys.delete() # Wait until all threads finish the job. for thread in threads: thread.join() return ret_dict
def get_recommendation(time_stamp, coordinates, user_id, ignore): """ Main handler function for recommend. :param time_stamp: Time from beginning of the epoch (1.1.1970) :param coordinates: Dictionary with coordinates. :param user_id: Identification of the current user. :param ignore: Identification of POI user ignored from previous recommend. :return: Dictionary representing answer for request to recommend. """ ''' Get all POIs in radius of 300 meters from user. ''' points_of_interest = fetch_poi(coordinates['lat'], coordinates['lon'], 300) # First we have to assign writing jobs to threads. length = len(points_of_interest[1]) poi_dict = dict() # Add temporary keys and merge the two dictionaries. poi_lst_of_dict = points_of_interest[0] poi_lst_foursquare = dict() for i in range(0, length): poi_lst_foursquare['some-temporary-key-%s %d' % (user_id, (i+1))] = points_of_interest[1][i] chunk_lst = make_chunks(poi_lst_foursquare, int(length/15) + 1) threads = [] for chunk in chunk_lst: t = WritePOIToDatabase(chunk) threads.append(t) t.start() poi_lst_of_dict.update(poi_lst_foursquare) ''' Get all required data. ''' part_of_day = get_time_section(time_stamp) act_rest_answer = get_user_activity(user_id) if 'error' in act_rest_answer: activity = 3 # If activity recognition provider encountered some error. else: req_act = dict() for k, v in act_rest_answer['svm_vector'].iteritems(): req_act[k] = float(v) activity = get_curr_activity(req_act) ''' Get id of all poi and rate them based on activity, context and distance. ''' for key, val in poi_lst_of_dict.iteritems(): f_res = lookup_table[part_of_day][activity][decode_category(val['fw_core']['category'])] s_res = grade_distance(coordinates['lat'], coordinates['lon'], val['fw_core']['location']['wgs84'][ 'latitude'], val['fw_core']['location']['wgs84']['longitude']) poi_dict[key] = f_res * s_res ''' Slice out ignored. ''' if ignore != 'None' and ignore in poi_dict: save_ignored_for_current_user(user_id, ignore) ignored = get_ignored_for_current_user(user_id) for ig_poi in ignored: if ig_poi in poi_dict: del poi_dict[ig_poi] elif ig_poi[:18] == 'some-temporary-key': key = Keys.objects.filter(temp=ig_poi, time__gte=datetime.now()-timedelta(minutes=3)) del poi_dict[key.real] ''' Sort pois based on grades and return first 15 elements. ''' sort_poi_lst = sorted(poi_dict.items(), key=operator.itemgetter(1), reverse=True) ret_dict = {"POIS": [], "activity": decode_activity(activity)} if len(sort_poi_lst) > 5: n_it = 5 else: n_it = len(sort_poi_lst) for num in range(0, n_it): ret_dict['POIS'].append({sort_poi_lst[num][0]: poi_lst_of_dict[sort_poi_lst[num][0]]}) # Delete old keys keys = Keys.objects.filter(time__lt=(datetime.now()-timedelta(minutes=3))) keys.delete() # Wait until all threads finish the job. for thread in threads: thread.join() return ret_dict