java HW 5

profilefoooq55
Lab8attachedFiles.docx

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;
	//methods
	public 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;
	//methods
	public 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 file
	protected void read_items(String file_name)
	{
		/*
		1. Open the file
		2. Read file line by line
		3. For each line, 
			Extract ID, Name, & Genre
			Create a Movie object
			Set Item ID, Name & Genre
			Add the Movie object into the Items[] items
		6. 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 file
		2. Read file line by line
		3. For each line, 
			Extract ID, n_items
			For i = 0 to n_items
				Extract the Movie IDs
			Create a User Object
			Set User ID, n_items, and add all items to that user.
			Add the User object into the Users[] users
		4. 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_items
		return 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 values
	public 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 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
		*/	
	}
	//
	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 19
2 3 1 6 19
3 3 1 2 7
4 2 1 10
5 4 1 5 7 11
6 6 1 7 11 18 19 20
7 5 1 2 7 11 17
8 5 5 9 11 16 19
9 7 1 2 3 6 9 10 19
10 5 1 2 10 18 19
11 3 1 2 16
12 7 1 2 3 5 10 11 19
13 9 1 2 3 5 10 11 13 19 20
14 5 2 6 16 17 19
15 6 1 3 5 6 10 16

User.java

public class User
{
	//data
	private int ID;
	private String name;
	private int n_items;
	private int[] items = new int[100];
	//methods
	public 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;}
	
}