def get_home_location(): location_dict = read_file.read_location_file() home_dict = {} for location, data in location_dict.items(): if data['type'] == 'Home (private)': home_dict[location] = data return home_dict
def recommend(): unknown_user_checkins_dict = read_file.read_checkins_file('unknown_user') location_info_dict = read_file.read_location_file() average_location_dict = get_average_location(unknown_user_checkins_dict, location_info_dict) candidate_location_distance_dict = compute_distance( average_location_dict, location_info_dict) result_rank = rank_candidate_location(candidate_location_distance_dict) return result_rank
def get_candidate_category(): candidate_data = read_file.read_candidate_file() location_data = read_file.read_location_file() candidate_category_dic = {} for single_candidate in candidate_data: for location_key, location_value in location_data.items(): if single_candidate == location_key: candidate_category_dic[single_candidate] = location_value[ "type"] category_data = load_category() result_dic = {} for candidate_key, candidate_value in candidate_category_dic.items(): for high_category in category_data["response"]["categories"]: for mid_category in high_category["categories"]: if candidate_value == mid_category["name"]: result_dic[candidate_key] = {} result_dic[candidate_key]["category"] = high_category[ "name"] if mid_category["categories"] != []: for low_category in mid_category["categories"]: if candidate_value == low_category["name"]: result_dic[candidate_key] = {} result_dic[candidate_key][ "category"] = mid_category["name"] if low_category["categories"] != []: for last_category in low_category["categories"]: if candidate_value == last_category["name"]: result_dic[candidate_key] = {} result_dic[candidate_key][ "category"] = low_category["name"] candidate_popularity_list = popularity_recommendation.candidate_popularity( ) for single_result_key, single_result_value in result_dic.items(): count = 0 for candidate_popularity in candidate_popularity_list: count += 1 if single_result_key == candidate_popularity: result_dic[single_result_key]["popularity"] = count #pprint.pprint(result_dic) return result_dic
def compute_distance(user_home_location_dict): candidate_list = read_file.read_candidate_file() location_info_dict = read_file.read_location_file() candidate_location_distance_dict = {} for user_name, home_location in user_home_location_dict.items(): candidate_location_distance_dict[user_name] = {} for candidate_location in candidate_list: # candidate location candidate_position = ( location_info_dict[candidate_location]['latitude'], location_info_dict[candidate_location]['longitude']) # user home location home_position = (home_location['latitude'], home_location['longitude']) distance_number = distance.distance(candidate_position, home_position).miles candidate_location_distance_dict[user_name][ candidate_location] = distance_number return candidate_location_distance_dict
def knownUser_unKnownLocation(): # 3884 known users with 100 unknown location new_dic = {} known_user_data = read_file.read_checkins_file("known_user") known_user_list = list(known_user_data.keys()) loc_list = [] pprint.pprint("test") for keyname, valuename in known_user_data.items(): for dailyvalue in valuename: for eachvalue in dailyvalue: eachloc = eachvalue["location"] loc_list.append(eachloc) new_dic[keyname] = loc_list pprint.pprint("test2") ##pprint.pprint(new_dic) pprint.pprint("test3") known_loc = read_file.read_location_file() pprint.pprint("test4") user_dict = {} for keyname, listvalue in new_dic.items(): user_dict[keyname] = {} for locKey in known_loc.keys(): user_dict[keyname][locKey] = 0 for value in listvalue: user_dict[keyname][value] += 1 pprint.pprint("loop2") pprint.pprint(user_dict["9448"]) unknown_loc = read_file.read_candidate_file() knownUser_unknownLoc = (len(known_user_data), len(unknown_loc) ) #(2,3,4)变为3维 maxtrix_knownUser_unknownLoc = np.zeros(knownUser_unknownLoc) #pprint.pprint(np.zeros(knownUser_unknownLoc)) ##pprint.pprint("build zero matrix") '''
def get_person_location_type(): user_data = read_file.read_checkins_file("unknown_user") location_data = read_file.read_location_file() user_location_dic = {} for user_id,user_location in user_data.items(): user_location_dic[user_id] = [] for daily_location in user_location: for location in daily_location: if location["location"] != "?": location_type = location_data[location["location"]]["type"] if "\x1a\x1a" in location_type: location_type = "Café" user_location_dic[user_id].append(location_type) for user_id,user_location_type in user_location_dic.items(): user_location_dic[user_id] = {} for single_location_type in user_location_type: user_location_dic[user_id][single_location_type] = user_location_type.count(single_location_type) #pprint.pprint(user_location_dic) return user_location_dic