java HW 5
Movie.txt
1 , Toy story , Adventure Animation
2 , Jumanji , Adventure Fantasy
3 , Grumpy Old Men , Comedy Romance
4 , Waiting to Exhale , Comedy Drama Romance
5 , Father of the Bride Part II , Comedy
6 , Heat , Action Crime Thriller
7 , Sabrina , Comedy Romance
8 , Tom and Huck , Adventure Children
9 , Sudden Death , Action
10 , GoldenEye , Action Adventure Thriller
11 , The American President , Comedy Drama Romance
12 , Dracula: Dead and Loving It , Comedy Horror
13 , Balto , Animation Children
14 , Nixon , Drama
15 , Cutthroat Island , Action Adventure Romance
16 , Casino , Crime Drama
17 , Sense and Sensibility , Comedy Drama Romance
18 , Four Rooms , Comedy Drama Thriller
19 , Ace Ventura: When Nature Calls , Comedy
20 , Money Train , Action Comedy Crime Drama Thriller
item.java
abstract public class Item
{
//data
protected int ID;
protected String name;
protected String description;
//methodspublic Item(){ID = -1;name = "";description = "";}public Item(int ID, String n, String d){this.ID = ID;this.name = n;this.description = d;}public int get_ID(){return ID;}public void set_ID(int i){ID = i;}public String get_name(){return name;}public void set_name(String i){name = i;}public String get_desciption(){return description;}public void set_description(String i){description = i;}}
movie.java
public class Movie extends Item{private String genre;private String director;//methodspublic Movie(int id, String n, String d, String g, String dir){super(id, n, d);genre = g;director = dir;}public void set_genre(String g){genre = g;}public String get_genre(String g){return genre;}public void set_director(String g){director = g;}public String get_director(String g){return director;}}
RecApp.java
public class RecApp{public static void main(String[] args){UserCFRecommender ucf = new UserCFRecommender("", "movies.txt");}}
Recommender.java
import java.util.Scanner;import java.io.*;public abstract class Recommender{protected User[] users = new User[10];protected Item[] items = new Item[10];//=========================//=========================//reads items from a fileprotected void read_items(String file_name){/*1. Open the file2. Read file line by line3. For each line,Extract ID, Name, & GenreCreate a Movie objectSet Item ID, Name & GenreAdd the Movie object into the Items[] items6. Close the file*/try{File f = new File(file_name);Scanner s = new Scanner(f);while(s.hasNext()){String[] vals = (s.nextLine()).split(" , ");for(int i=0; i< vals.length; i++)System.out.println(vals[i]);System.out.println();}}catch (FileNotFoundException e){}}//=========================//=========================protected void read_users(String file_name){/*1. Open the file2. Read file line by line3. For each line,Extract ID, n_itemsFor i = 0 to n_itemsExtract the Movie IDsCreate a User ObjectSet User ID, n_items, and add all items to that user.Add the User object into the Users[] users4. Close the file*/}//=========================//=========================public User[] get_users(){return users;}//=========================//=========================public Item[] get_items(){return items;}//=========================//=========================public abstract void make_recommendations();}
UserCFRecommender.java
public class UserCFRecommender extends Recommender{public UserCFRecommender(){}public UserCFRecommender(String ufile, String ifile){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){//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//3. compute Jaccard sim as n_common_items/total_n_itemsreturn 0;}//finds the nieghbors of a user given the user index in the//Users[] users, and given the number of neighbors//returns neighbors indices, and neighbors similarity valuespublic void find_user_neighbors(int u_ind, int n_neighbors, int[] n_ind, double[] n_sim){/*1. Create an array sim[] with size = n_users to save all similarityvalues in2. For each other user ucompute similarity between u_ind and u by calling thecompute_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 = -1max = -1for j = 0 -> n_users -1:if sim[j]>max:max = sim[j]ind = jsave max & ind into n_sim[i] and n_ind[i]set sim[ind] = -2*/}//public void compute_recommendation_scores(int u_ind, int[] n_ind, double[] n_sim, double[] scores){}//public void make_recommendations(){}}
users.txt
1 4 1 2 10 192 3 1 6 193 3 1 2 74 2 1 105 4 1 5 7 116 6 1 7 11 18 19 207 5 1 2 7 11 178 5 5 9 11 16 199 7 1 2 3 6 9 10 1910 5 1 2 10 18 1911 3 1 2 1612 7 1 2 3 5 10 11 1913 9 1 2 3 5 10 11 13 19 2014 5 2 6 16 17 1915 6 1 3 5 6 10 16
User.java
public class User{//dataprivate int ID;private String name;private int n_items;private int[] items = new int[100];//methodspublic User(){ID = -1;name = "";n_items = 0;}public User(int id, String n){ID = id;name = n;n_items = 0;}public void add_item(int i){items[n_items] = i;n_items++;}//computes the number of common items between this and u.public int similarity(User u){int n = u.get_n_items();int[] uitems = u.get_items();int sim = 0;for(int i=0; i<n; i++)for(int j=0; j<n_items; j++)if(uitems[i] == items[j]){sim++;break;}return sim;}public int get_ID(){return ID;}public int[] get_items(){return items;}public int get_n_items(){return n_items;}public int set_ID(int i){ID = i;}}