Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I need some help with this assignment please I\'m having a hard time getting

ID: 3813948 • Letter: H

Question

Hi, I need some help with this assignment please I'm having a hard time getting it done.

This assignment will provide you with practice using arrays.

Your job is to build a simple Recommender System, similar to the one that Netflix (an online movie-rental service) uses to recommend movies to customers. The basic idea is to find out some movies that a user likes, and then recommend other movies that the user might also like.

On Oct. 2, 2006, Netflix announced a challenge to programmers everywhere to come up with a better way of figuring out how to predict what movies to recommend to users. They offered a prize of $1 million to anyone who could beat their own technique by 10% in prediction accuracy. A team of programmers eventually claimed the prize in 2009. You can read about the challenge on Wikipedia, or Netflix's own page about it. This assignment is a simplified version of the kinds of recommendation techniques used by Netflix, Amazon, and others.

For this assignment, you will need the class MovieFileHelper. The code is available here. This class includes two methods that you will need to use for your assignment, but YOU DO NOT NEED TO UNDERSTAND HOW THEY WORK. The code for these methods involve several things you have not learned yet. Feel free to look at them and try to understand them, but it is not necessary to understand their code to do this assignment.

You will also need to download these two files, movies.txt and ratings.txt. The first one stores the names of 20 Hollywood movies. The second one stores movie ratings from 30 (fictional) users, who rated these 20 movies between 0 and 5 (5 being the best), or -1 if they haven't seen it.

The first method that is useful to you in MovieFileHelper is loadMovieNames(String filename). When you call this method with the String "movies.txt", it will load all of the movie names from that file into an array of Strings, and return that array.

The second method for you to use in MovieFileHelper is loadRatings(String filename). When you call this method with the String "ratings.txt", it will load all of the recommendations from that file into a 2-D array of doubles, and return that array.

Create a Java file called Recommender.java. Your program should behave as follows:

Using the helper class and text files described above, load the 20 movie names and the movie ratings from 30 people into two arrays in memory.

Ask the user to enter a rating (between 1 and 5, or -1 if they haven't seen it) for each movie.

Create a method that determines a score for each of the 30 people, which represents how similar that person's tastes are to the current user's tastes. Store these similarity scores in an array of 30 doubles. The similarity scores should be between 0 and 1 each.

Create an array that represents recommended ratings for the user. There should be 20 numbers in this array, one for each movie. The higher the number, the more strongly your program thinks the user will like the movie. The number should be the average over all 30 ratings for the movie that are greater than 0 (only include ratings for users who have actually seen the movie). However, it should be a weighted average: people who are more similar to the current user should have a higher weight than people who are less similar.

Display the name of the top movie (according to the recommended ratings from the previous step) that the user has not yet seen.

Your program is going to try to decide whether or not you might like a movie that you haven't yet seen. It's going to come up with a score, which represents the likelihood that you'll enjoy it. If people who have tastes similar to yours seem to like it, the program will assign that movie a higher score. The question is, How do you determine similarity?

You can come up with your own way of judging how similar two people's ratings are. One suggestion is to compute what's called cosine similarity:

for person 1, compute the square of each movie rating for movies they have seen, and add these up and then take the square root. Store the result in a variable called p1. For example, if person 1 saw 3 movies and rated them 4, 4, and 2, then p1 = sqrt(4*4 + 4*4 + 2*2) = sqrt(36) = 6.

do the same for person 2, and store the result in a variable called p2.

for each movie that both people have seen, compute the product of their ratings. Add up all of these products, and store the result in a variable called both. For example, if person 1 and person 2 both saw movies 7 and 14 (out of 20), and person 1 rated them as 4 for movie 7 and 2 for 14, and person 2 rated them as 2 for movie 7 and 3 for movie 14, then both = 4*2 + 2*3.

The cosine similarity score between person 1 and person 2 is (both / (p1 * p2)).

You can also try instead of recommending a single movie, recommend NUM_RECOMMENDATIONS movies, where NUM_RECOMMENDATIONS is some constant built into your program which is larger than 1.

movies.txt

ratings.txt

Movie File Helper

Explanation / Answer

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class MovieFileHelper
{
    public static double [][] loadRatings(String filename)
    {
        double [][] ret = new double[30][20];

        try {
            Scanner sc = new Scanner(new File(filename));
            String line = null;
            Scanner lineSc = null;
            int i=0;
            while(sc.hasNextLine() && i < ret.length)
                {
                    line = sc.nextLine();
                    lineSc = new Scanner(line);
                    for(int j=0; j<ret[i].length && lineSc.hasNextDouble(); j++)
                        {
                            ret[i][j] = lineSc.nextDouble();
                        }
                    i++;
                }
          
            sc.close();
        } catch (IOException e) {
            System.out.println("Error opening " + filename +
                               " for reading. Quitting ...");
            System.exit(1);
        }
        return ret;
    }

    public static String [] loadMovieNames(String filename) {
        String [] ret = new String[20];
      
        try {
            Scanner sc = new Scanner(new File(filename));
            int i=0;
            while(sc.hasNextLine() && i < ret.length)
                {
                    ret[i] = sc.nextLine();
                    i++;
                }
          
            sc.close();
        } catch (IOException e) {
            System.out.println("Error opening " + filename +
                               " for reading. Quitting ...");
            System.exit(1);
        }
        return ret;
    }
  

   public static void main(String args[]) {
       new Recommender();
   }
}


class Recommender {
  
   String[] movieNames;
   double [][] movieRatings;
   double [] currentUserRatings;
   double [] similarityToOtherUsers;
   double [] recommendedMoviesRanking;
  
   public Recommender() {
       String filePath = "/home/zubair/Desktop/pythonProg/testfiles/";
       movieNames = MovieFileHelper.loadMovieNames(filePath + "movies.txt");
       movieRatings = MovieFileHelper.loadRatings(filePath + "ratings.txt");
       System.out.println(movieRatings.length);;
       getCurrentUserRatings();
       findSimilarities();
       findRecommendedMovies();
       printRecommendedMovie();
   }
  
   private void getCurrentUserRatings() {
       currentUserRatings = new double[movieNames.length];
       Scanner sc = new Scanner(System.in);
      
       for (int i=0; i<movieNames.length; i++) {          
           System.out.print("Enter ratings for " + movieNames[i] + " : ");
           currentUserRatings[i] = sc.nextDouble();
          
           /*
           * Validation of ratings
           * */
           if (currentUserRatings[i] > 5 || (currentUserRatings[i] < 0 && currentUserRatings[i] != -1)) {
               System.out.println("Error : Ratings must be between 0 to 5, ( or -1 if unseen) !! ");
               i--;
           }
       }
       sc.close();
   }
  
   private void findSimilarities() {
       similarityToOtherUsers = new double[movieRatings.length];
      
       double curPerson = 0.0;
       for (int i=0; i<movieNames.length; i++) {
           if (currentUserRatings[i] != -1) {
               curPerson += (currentUserRatings[i] * currentUserRatings[i]);
           }
       }
       curPerson = Math.sqrt(curPerson);

       for (int i=0; i<movieRatings.length; i++) {
           double extPerson = 0.0;
           double bothPerson = 0.0;
          
           for (int j=0; j<movieNames.length; j++) {
               if (movieRatings[i][j] != -1) {
                   extPerson += (movieRatings[i][j] * movieRatings[i][j]);
                  
                   if (currentUserRatings[j] != -1) {
                       bothPerson += movieRatings[i][j] * currentUserRatings[j];
                   }
               }
           }
           extPerson = Math.sqrt(extPerson);
           similarityToOtherUsers[i] = bothPerson/(curPerson * extPerson);
       }
   }
  
   private void findRecommendedMovies() {
       recommendedMoviesRanking = new double[movieNames.length];
       for (int i=0; i<movieNames.length; i++) {
           double totalWeight = 0.0;
           for (int j=0; j<movieRatings.length; j++) {
               if (movieRatings[j][i] != -1) {
                   recommendedMoviesRanking[i] += similarityToOtherUsers[j] * movieRatings[j][i];
                   totalWeight += similarityToOtherUsers[j];
               }
           }
           recommendedMoviesRanking[i] = recommendedMoviesRanking[i] /totalWeight;
       }
   }
  
   private void printRecommendedMovie() {
       double maxRatings = -1;
       int maxIndex = -1;
       for (int i=0; i<recommendedMoviesRanking.length; i++) {
           if (maxRatings < recommendedMoviesRanking[i]) {
               maxIndex = i;
               maxRatings = recommendedMoviesRanking[i];
           }
       }
       System.out.println("Recommended movie is : "+movieNames[maxIndex]);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote