-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfriends.py
64 lines (51 loc) · 2.48 KB
/
friends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Author: Matthew Vallance 001225832
Purpose: Class with methods for processing the friends and accompanying data - F2
Date: 15/01/22
"""
class Friends:
def __init__(self, social_nw):
self.common_friends_matrix = []
self.all_users_friends = social_nw
def get_common_friends(self):
# Run through the list of users and their friends
for user in self.all_users_friends:
common_friends = []
user_to_compare_friends = set(user[1])
# Run through the list of users and their friends again, to work out common friends for each other user
for friend in self.all_users_friends:
user_to_compare_friends_2 = set(friend[1])
# Intersection of the two sets of friends - gets a count of the common friends
common_friends.append(len(user_to_compare_friends & user_to_compare_friends_2))
# Put the commons friends matrix into an appropriate data structure
self.common_friends_matrix.append([user[0], common_friends])
return self.common_friends_matrix
def get_recommended_friends(self, common_friends, user):
# Check to see if the common friends matrix is filled
if len(common_friends) == 0:
return False
# Setup variables
user_friends = []
# Find a match for the given user and get common friends count
for friends in common_friends:
if friends[0] == user:
user_friends = friends[1]
# Split into a sorted/unsorted user_friends array to use later
user_friends_unsorted = user_friends
user_friends = sorted(user_friends)
# Get second-highest number in common friends
user_to_add = user_friends[-2]
# Get a list of users friends, to compare later on
users_current_friends = []
for users_friends_names in self.all_users_friends:
if users_friends_names[0] == user:
users_current_friends = users_friends_names[1]
# Get the name(s) of the recommended friend(s)
recommended_friends = []
count = 0
for number in user_friends_unsorted:
# Make sure we don't recommend any of current friends
if number == user_to_add and common_friends[count][0] != user and common_friends[count][0] not in users_current_friends:
recommended_friends.append(common_friends[count][0])
count += 1
return recommended_friends