I have this java code with some errors that are stumping me lines,26,107,124,134
ID: 3707703 • Letter: I
Question
I have this java code with some errors that are stumping me lines,26,107,124,134,145
public class UserCFRecommender extends Recommender
{
public UserCFRecommender()
{
super();
}
public UserCFRecommender(String ufile, String ifile)
{
super();
read_items(ifile);
read_users(ufile);
}
//computes the Jaccard similarity between two users give the
//indices of both users in the Users[] users list.
public double compute_jaccard_sim(int u1_ind, int u2_ind)
{
User u1 = users[u1_ind];
User u2 = users[u2_ind];
//1. get n_common_items:
int n_common_items = (users[u1_ind]).similarity(users[u2_ind]);
//2. get total_n_items using u1 n_items, u2 n_items & n_common_items
int total_n_items = u1 n_items + u2 n_items - n_common_items;
//3. compute Jaccard sim as n_common_items/total_n_items
int jaccard_sim = n_common_items/total_n_items;
return 0;
}
//finds the neighbors of a user given the user index in the
//Users[] users, and given the number of neighbors
//returns neighbors indices, and neighbors similarity values
public void find_user_neighbors(int u_ind, int n_neighbors, int[] n_ind, double[] n_sim)
{
int i,j;
/*
1. Create an array sim[] with size = n_users to save all similarity
values in
2. For each other user u
compute similarity between u_ind and u by calling the
compute_jaccard_sim method, and save the result in sim[u]
3. Scan sim[] and select the "n_neighbors" users with the highest similarities.
for i = 0 -> n_neighbors -1:
ind = -1
max = -1
for j = 0 -> n_users -1:
if sim[j]>max:
max = sim[j]
ind = j
save max & ind into n_sim[i] and n_ind[i]
set sim[ind] = -2
*/
double sim[] = new double[n_users];
for(i=0; i<n_users; i++) {
sim[i]= compute_jaccard_sim(i,n_ind[i]);
}
for(i=0; i<n_neighbors; i--) {
double ind =-1;
double max = -1;
for(j=0; j<n_users;j--) {
if(sim[j]>max) {
max =sim[j];
ind =j;
n_sim[i] =max;
n_ind[i] = (int) ind;
sim[(int) ind] = -2;
}
}
}
}
//
public void compute_recommendation_scores(int u_ind, int[] n_ind, double[] n_sim, double[] scores, int n_n, int n_m)
{
/* 1. for each movie m :
1.1 compute recommendation score using the equation from slides
compute denominator
compute numerator
scores[m] = numerator/denominator
2. Find the n_m movies with the highest scores:
for i = 0 -> n -1:
ind = -1
max = -1
for j = 0 -> n_items -1:
if scores[j]?max:
max = scores[j]
ind = j
the recommended move now exist at the items[ind]:
print the movie info,
set scores[ind] = -2
*/
for (int i=0; i<n_items; i++)
{
double denom = 0;
for(int j=0; j<n_n; j++)
denom += n_sim[j];
double numer = 0;
for(int j=0; j<n_n; j++) {
numer += n_sim[j] * get_rating(n_ind[j], i);
scores[i] = numer/denom;
}
for(int i=0; i<n_items; i++) {
int ind = -1;
double max = -1;
for(int j=0; j<n_items; j++) {
if(scores[j]> max) {
max = scores[j];
ind =i;
}
}
System.out.println("Movie ID = ");
scores[ind] = -2;
// Select the n_m movies with the highest scores
}
for(int i=0; i<n_m; i++) {
int ind = -1;
double max =-1;
for(int j=0; j<n_items; j++) {
if(scores[j]>max) {
max = scores[j];
ind = j;
}
}
System.out.println("Movie ID = ", ind);
}
private double get_rating(int nbr_ind, int movie_id)
{
User nbr = users[nbr_ind];
int[] movie_ids = nbr.get_items();
double rating = 0;
for(int k=0; k<nbr.get_n_items(); k++) {
if(movie_ids[k] == movie_id) {
return 1.0;
break;
}
}
return 0.0;
}
//
public void make_recommendations()
{
for(int u=0; u<n_users; u++) {
//print "Making Recommendations for user" + i +":"
System.out.println("Making Recommendations for user" + ":");
// specify the recommendation parameters
int n_neighbors = 3;
int n_movies = 3;
// create relevant arrays
int[] ind = new int[n_neighbors];
double[] sim = new double[n_neighbors];
double[] scores = new double[n_items];
find_user_neighbors(u, n_neighbors, ind, sim);
compute_recommendation_scores(u, ind, scores, scores, n_neighbors, n_movies );
}
}
}
Explanation / Answer
public class UserCFRecommender extends Recommender
{
public UserCFRecommender()
{
super();
}
public UserCFRecommender(String ufile, String ifile)
{
super();
read_items(ifile);
read_users(ufile);
}
//computes the Jaccard similarity between two users give the
//indices of both users in the Users[] users list.
public double compute_jaccard_sim(int u1_ind, int u2_ind)
{
User u1 = users[u1_ind];
User u2 = users[u2_ind];
//1. get n_common_items:
int n_common_items = (users[u1_ind]).similarity(users[u2_ind]);
//2. get total_n_items using u1 n_items, u2 n_items & n_common_items
int total_n_items = u1 n_items + u2 n_items - n_common_items;
//3. compute Jaccard sim as n_common_items/total_n_items
int jaccard_sim = n_common_items/total_n_items;
return 0;
}
//finds the neighbors of a user given the user index in the
//Users[] users, and given the number of neighbors
//returns neighbors indices, and neighbors similarity values
public void find_user_neighbors(int u_ind, int n_neighbors, int[] n_ind, double[] n_sim)
{
int i,j;
/*
1. Create an array sim[] with size = n_users to save all similarity
values in
2. For each other user u
compute similarity between u_ind and u by calling the
compute_jaccard_sim method, and save the result in sim[u]
3. Scan sim[] and select the "n_neighbors" users with the highest similarities.
for i = 0 -> n_neighbors -1:
ind = -1
max = -1
for j = 0 -> n_users -1:
if sim[j]>max:
max = sim[j]
ind = j
save max & ind into n_sim[i] and n_ind[i]
set sim[ind] = -2
*/
double sim[] = new double[n_users];
for(i=0; i<n_users; i++) {
sim[i]= compute_jaccard_sim(i,n_ind[i]);
}
for(i=0; i<n_neighbors; i--) {
double ind =-1;
double max = -1;
for(j=0; j<n_users;j--) {
if(sim[j]>max) {
max =sim[j];
ind =j;
n_sim[i] =max;
n_ind[i] = (int) ind;
sim[(int) ind] = -2;
}
}
}
}
//
public void compute_recommendation_scores(int u_ind, int[] n_ind, double[] n_sim, double[] scores, int n_n, int n_m)
{
/* 1. for each movie m :
1.1 compute recommendation score using the equation from slides
compute denominator
compute numerator
scores[m] = numerator/denominator
2. Find the n_m movies with the highest scores:
for i = 0 -> n -1:
ind = -1
max = -1
for j = 0 -> n_items -1:
if scores[j]?max:
max = scores[j]
ind = j
the recommended move now exist at the items[ind]:
print the movie info,
set scores[ind] = -2
*/
for (int i=0; i<n_items; i++)
{
double denom = 0;
for(int j=0; j<n_n; j++)
denom += n_sim[j];
double numer = 0;
for(int j=0; j<n_n; j++) {
numer += n_sim[j] * get_rating(n_ind[j], i);
scores[i] = numer/denom;
}
for(int i=0; i<n_items; i++) {
int ind = -1;
double max = -1;
for(int j=0; j<n_items; j++) {
if(scores[j]> max) {
max = scores[j];
ind =i;
}
}
System.out.println("Movie ID = ");
scores[ind] = -2;
// Select the n_m movies with the highest scores
}
for(int i=0; i<n_m; i++) {
int ind = -1;
double max =-1;
for(int j=0; j<n_items; j++) {
if(scores[j]>max) {
max = scores[j];
ind = j;
}
}
System.out.println("Movie ID = ", ind);
}
private double get_rating(int nbr_ind, int movie_id)
{
User nbr = users[nbr_ind];
int[] movie_ids = nbr.get_items();
double rating = 0;
for(int k=0; k<nbr.get_n_items(); k++) {
if(movie_ids[k] == movie_id) {
return 1.0;
break;
}
}
return 0.0;
}
//
public void make_recommendations()
{
for(int u=0; u<n_users; u++) {
//print "Making Recommendations for user" + i +":"
System.out.println("Making Recommendations for user" + ":");
// specify the recommendation parameters
int n_neighbors = 3;
int n_movies = 3;
// create relevant arrays
int[] ind = new int[n_neighbors];
double[] sim = new double[n_neighbors];
double[] scores = new double[n_items];
find_user_neighbors(u, n_neighbors, ind, sim);
compute_recommendation_scores(u, ind, scores, scores, n_neighbors, n_movies );
}
}
}