Question
Python code help please, if you could show a picture of the code with alll the indentations it would be a great help. Background A
Python code help please, if you could show a picture of the code with alll the indentations it would be a great help.
Background
A social network such as Facebook consists of a set of users and connections between the users (i.e., there exists a connection between you and everyone who you have befriended). An interesting problem is to write a computer program that can automatically suggest possible new connections (i.e., friends) for each user. For this project you will implement a system that will, for each user, suggest the most probable user to befriend based upon the intersection of your common friends. In other words, the user that you will suggest to Person A is the person who has the most friends in common with Person A, but who currently is not friends with Person A. The algorithm begins with: for each user go through all the other users (another for) and calculate the number of friends they have in common. Then for a given user the friend you will suggest to them is the user in the social network who they are currently not friends with, but have the most friends in common. Intuitively, it makes sense why they might want to be connected as friends. (Note that when you look for your most common friend in this scheme it will be you, i.e. you will have to remember to remove yourself from consideration.)
Description / Specification
1) Two files have been provided for you to run your program with. One is a small test file containing a made-up set of users and connections between them (that file is small_network_data.txt). The other is a subset of a real Facebook dataset, which was obtained from: https://snap.stanford.edu/data/egonets-Facebook.html The format of both files is the same: The first line of the file is an integer representing the number of users in the given network. The following lines are of the form: user_u user_v where user_u and user_v are the IDs of two users who are friends. For example, here is a very small file that has 5 users in the social network: 5 0 1 1 2 1 4 2 3 The above is a representation of a social network that contains 5 users. User ID=0 is friends with User IDs = 1 User ID=1 is friends with User IDs = 0, 2, 4 User ID=2 is friends with User IDs = 1, 3 User ID=3 is friends with User IDs = 2 User ID=4 is friends with User IDs = 1
2) We provide a file with function stubs and one complete function init_matrix(n). You must implement the following functions:
a) open_file() prompts the user to enter a file name. The program will try to open the data file. Appropriate error message should be shown if the data file cannot be opened. This function will loop until it receives proper input and successfully opens the file. It returns a file pointer.
b) read_file(fp) will have the file pointer fp passed into it. The first line of the file contains the value for the variable n, the number of users in the social network. We have provided the code to read n and initialize a list network containing n nested empty lists (e.g., n = 3 would correlate to creating the list [ [], [], [] ]). This list of lists will be used to hold the list of friends for each of the n users. You can then use a for loop to iterate over the remaining lines of the file. For each line of the file you will want to obtain the two user id values on that linewe denote those user ids as u and v. You need to place v into us list and also place u into vs list, since they are mutual friends (hint: use the list append() method to place each user id into a list). Finally you will return network, which is the list of lists that was created.
c) calc_similarity_scores(network) takes the social network as the parameter network and creates a similarity matrix for all the users in the social network. Here the definition of similarity is the number of friends that any pair of users have in common. The first thing you will want to do is initialize a list of lists that is n x n in size to all zeros that will hold the similarity (i.e., number of common friends) for each pairing of users. In other words, you will create a list of lists named similarity_matrix that will have n nested lists, each containing n zeros. (e.g., n=3 gives the following list [ [0,0,0], [0,0,0], [0,0,0] ]). We provide the function init_matrix(n) for you that will create and initialize the matrix. You will then use two nested for loops to iterate over all pairs of users in the network. For each pair of users you can obtain their list of friends from network, and then call the function num_in_common_between_lists(user1_friend_lst, user2_friend_lst), which will return an integer, num_in_common, representing the number of friends those two users have in common. The int num_in_common will need to be stored in locations [user1][user2] and [user2][user1] of the list of lists similarity_matrix. Return similarity_matrix Suggestion, to iterate over all pairs of users use the following: for i in range(n): for j in range(n): # call num_in_common_between_lists using user is list and user js list as arguments
d) num_in_common_between_lists(list1, list2) is a function that calculates the number of common items between two lists. To do this you can have a for loop that iterates over the items of list1 and then an if statement that checks whether or not that item is also in list2. This function is to return an integer, which is the number of items that the two lists had in common (i.e., the number of items that were found in both of the lists). (e.g., if list1 = [1,3,5,6] and list2 = [1, 2, 3], then the value 2 would be returned since they have two items in common (the values 1 and 3)).
e) recommend(user_id,network,similarity_matrix) This user_id will be used as an index into the similarity_matrix which will give you access to the list of similarity scores for that given user (user_id) for all other users. You should then determine the largest value in this list and return its index as the most similar. Of course, you dont want to recommend someone who is already a friend and it also doesnt make sense to recommend the person as his or her own friend. It is the most similar because by having the largest value in the list it means that it is the user who had the most friends in common with user_id. (Hint: while developing this function using the small network file I printed out similarity_matrix[user_id] and network[user_id] so I could check that the correct recommendation was calculated.)
f) main() In the main program you will need to first call open_file(), followed by calling read_file(fp), and then calc_similarity_scores(network). After obtaining the similarity_matrix you will need to have a while loop that will repeatedly ask the user for a user_id that should be in the range of [0,n-1] (i.e., from 0 to n-1, inclusive). Call recommend to get a recommendation.
g) Note that when prompting the user for the user_id you should validate that the input is in the correct range, which is 0 to n. Also, you must use a try-except for the cases when the user inputs a non-integer value (e.g., for the case if they try to input the string hello instead of an int).
h) After displaying the suggested friend for the user that had their id as user_id you should prompt the user if they want to see the suggestion for another user input or if they want to exit. To exit the program you should accept all forms of the string no (i.e., NO, No, nO, and no).
files given:
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/facebook_1000_data.txt
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/function_test1.py
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/function_test2.py
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/function_test3.py
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/function_test4.py
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/run_file.py
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/small_network_data.txt
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/test1.txt
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/test2.txt
http://www.cse.msu.edu/~cse231/Online/Projects/Project07/test3.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started