def get_topbooks(data,user): topbooks = [] top3match = [] profiles = function2.get_by_gender_age(data,user)#Getting the user age and gender user_bookinlist = ' '.join(user["books"])#user will join the books together eg.("human philosophy") user_temporary = user_bookinlist.split(' ')#matching person's will split the books eg. ("human","philosophy") for i in range (len(profiles)): # making use of function 2,all the people by gender and by age bookcounter = 0 # to get the counter to be reset for every person matchingpersons_string = ' '.join(profiles[i]["books"])#matching person's will join the books together eg. ("human philosophy") matchingpersonslist = matchingpersons_string.split(' ')#matching person's will split the books eg. ("human","philosophy") for book in user_temporary: # loop my user books which is in the form of ("human","philosophy") if book in matchingpersonslist:#to check if the word "human" is in the matchingpersonlist bookcounter+= 1 #if the book is found in the list counter +1 if bookcounter > 0 : profiles[i]["bookcounter"] = bookcounter#key and values for my profile dictionary topbooks.append(profiles[i]) sortedlistvalues = sorted(topbooks,key=lambda x:x["bookcounter"]) ##using list then first x is the def(x)second x is the index of counter if len(topbooks) > 3 : # if result is bigger than 3, return top 3 return sortedlistvalues[-3:] else: return sortedlistvalues
def get_by_likes_dislikes(data, user): profiles = function2.get_by_gender_age(data, user) topMatches = [] #loop through all profiles for i in range(len(profiles)): likesCounter, dislikesCounter, total = 0,0,0 #loop through all user's likes for like in user["likes"]: #if what user like, the matching also like if like in profiles[i]["likes"]: likesCounter += 1 total += 1 #if what user like, the matching doesn't like elif like in profiles[i]["dislikes"]: likesCounter -= 1 total -= 1 #loop through all user's dislikes for dislike in user["dislikes"]: #if what user dislike, the matching also dislike if dislike in profiles[i]["dislikes"]: dislikesCounter += 1 total += 1 #if what user dislike, the matching person like elif dislike in profiles[i]["likes"]: dislikesCounter -=1 total -= 1 #if total counter is bigger than 0 #update the profile with the total, likes and dislikes counter #append profile (dictionary) into top possible matches if total > 0 : profiles[i]["likesCounter"] = likesCounter profiles[i]["dislikesCounter"] = dislikesCounter profiles[i]["total"] = total topMatches.append(profiles[i]) #sort result (list) by dictionaary key 'total' in ascending order result = sorted(topMatches, key=lambda x: x['total']) #if result is more than 3, return last 3 which is the top 3 if len(result) > 3: return result[-3:] #if result is lesser than 3, return all else: return result
def get_by_country(data, user): """ getting profiles based on country """ result = [] #making use of funtion2 function #loop through the list of profiles for profile in f2.get_by_gender_age(data, user): #if user acceptable country is not empty if len(user["acceptable_country"]) > 0: #if profile's country is in user acceptable country list #append profile (dictionary) into result if profile["country"] in user["acceptable_country"]: result.append(profile) return result