├── Java Programming Build a Recommendation System Certificate.pdf ├── Java Programming and Software Engineering Fundamentals Certification.pdf ├── README.md ├── Week 1 ├── FirstRatings.class ├── FirstRatings.ctxt ├── FirstRatings.java ├── Movie.class ├── Movie.ctxt ├── Movie.java ├── Rater.class ├── Rater.ctxt ├── Rater.java ├── Rating.class ├── Rating.ctxt ├── Rating.java ├── data │ ├── ratedmovies_short.csv │ ├── ratedmoviesfull.csv │ ├── ratings.csv │ └── ratings_short.csv └── package.bluej ├── Week 2 ├── FirstRatings.class ├── FirstRatings.ctxt ├── FirstRatings.java ├── Movie.class ├── Movie.ctxt ├── Movie.java ├── MovieRunnerAverage.class ├── MovieRunnerAverage.ctxt ├── MovieRunnerAverage.java ├── Rater.class ├── Rater.ctxt ├── Rater.java ├── Rating.class ├── Rating.ctxt ├── Rating.java ├── SecondRatings.class ├── SecondRatings.ctxt ├── SecondRatings.java ├── data │ ├── ratedmovies_short.csv │ ├── ratedmoviesfull.csv │ ├── ratings.csv │ └── ratings_short.csv └── package.bluej ├── Week 3 ├── Assignment 1 │ ├── EfficientRater.class │ ├── EfficientRater.ctxt │ ├── EfficientRater.java │ ├── FirstRatings.class │ ├── FirstRatings.ctxt │ ├── FirstRatings.java │ ├── Movie.class │ ├── Movie.ctxt │ ├── Movie.java │ ├── MovieRunnerAverage.class │ ├── MovieRunnerAverage.ctxt │ ├── MovieRunnerAverage.java │ ├── PlainRater.class │ ├── PlainRater.ctxt │ ├── PlainRater.java │ ├── Rater.class │ ├── Rater.ctxt │ ├── Rater.java │ ├── Rating.class │ ├── Rating.ctxt │ ├── Rating.java │ ├── SecondRatings.class │ ├── SecondRatings.ctxt │ ├── SecondRatings.java │ ├── data │ │ ├── ratedmovies_short.csv │ │ ├── ratedmoviesfull.csv │ │ ├── ratings.csv │ │ └── ratings_short.csv │ └── package.bluej └── Assignment 2 │ ├── AllFilters.class │ ├── AllFilters.ctxt │ ├── AllFilters.java │ ├── DirectorsFilter.class │ ├── DirectorsFilter.ctxt │ ├── DirectorsFilter.java │ ├── EfficientRater.class │ ├── EfficientRater.ctxt │ ├── EfficientRater.java │ ├── Filter.class │ ├── Filter.ctxt │ ├── Filter.java │ ├── FirstRatings.class │ ├── FirstRatings.ctxt │ ├── FirstRatings.java │ ├── GenreFilter.class │ ├── GenreFilter.ctxt │ ├── GenreFilter.java │ ├── MinutesFilter.class │ ├── MinutesFilter.ctxt │ ├── MinutesFilter.java │ ├── Movie.class │ ├── Movie.ctxt │ ├── Movie.java │ ├── MovieDatabase.class │ ├── MovieDatabase.ctxt │ ├── MovieDatabase.java │ ├── MovieRunnerAverage.class │ ├── MovieRunnerAverage.ctxt │ ├── MovieRunnerAverage.java │ ├── MovieRunnerWithFilters.class │ ├── MovieRunnerWithFilters.ctxt │ ├── MovieRunnerWithFilters.java │ ├── PlainRater.class │ ├── PlainRater.ctxt │ ├── PlainRater.java │ ├── Rater.class │ ├── Rater.ctxt │ ├── Rater.java │ ├── Rating.class │ ├── Rating.ctxt │ ├── Rating.java │ ├── SecondRatings.class │ ├── SecondRatings.ctxt │ ├── SecondRatings.java │ ├── ThirdRatings.class │ ├── ThirdRatings.ctxt │ ├── ThirdRatings.java │ ├── TrueFilter.class │ ├── TrueFilter.ctxt │ ├── TrueFilter.java │ ├── YearAfterFilter.class │ ├── YearAfterFilter.ctxt │ ├── YearAfterFilter.java │ ├── data │ ├── ratedmovies_short.csv │ ├── ratedmoviesfull.csv │ ├── ratings.csv │ └── ratings_short.csv │ └── package.bluej └── Week 4 ├── AllFilters.class ├── AllFilters.ctxt ├── AllFilters.java ├── DirectorsFilter.class ├── DirectorsFilter.ctxt ├── DirectorsFilter.java ├── EfficientRater.class ├── EfficientRater.ctxt ├── EfficientRater.java ├── Filter.class ├── Filter.ctxt ├── Filter.java ├── FirstRatings.class ├── FirstRatings.ctxt ├── FirstRatings.java ├── FourthRatings.class ├── FourthRatings.ctxt ├── FourthRatings.java ├── GenreFilter.class ├── GenreFilter.ctxt ├── GenreFilter.java ├── MinutesFilter.class ├── MinutesFilter.ctxt ├── MinutesFilter.java ├── Movie.class ├── Movie.ctxt ├── Movie.java ├── MovieDatabase.class ├── MovieDatabase.ctxt ├── MovieDatabase.java ├── MovieRunnerAverage.class ├── MovieRunnerAverage.ctxt ├── MovieRunnerAverage.java ├── MovieRunnerSimilarRatings.class ├── MovieRunnerSimilarRatings.ctxt ├── MovieRunnerSimilarRatings.java ├── MovieRunnerWithFilters.class ├── MovieRunnerWithFilters.ctxt ├── MovieRunnerWithFilters.java ├── PlainRater.class ├── PlainRater.ctxt ├── PlainRater.java ├── Rater.class ├── Rater.ctxt ├── Rater.java ├── RaterDatabase.class ├── RaterDatabase.ctxt ├── RaterDatabase.java ├── Rating.class ├── Rating.ctxt ├── Rating.java ├── RecommendationRunner.class ├── RecommendationRunner.ctxt ├── RecommendationRunner.java ├── Recommender.class ├── Recommender.ctxt ├── Recommender.java ├── SecondRatings.class ├── SecondRatings.ctxt ├── SecondRatings.java ├── ThirdRatings.class ├── ThirdRatings.ctxt ├── ThirdRatings.java ├── TrueFilter.class ├── TrueFilter.ctxt ├── TrueFilter.java ├── YearAfterFilter.class ├── YearAfterFilter.ctxt ├── YearAfterFilter.java ├── data ├── ratedmovies_short.csv ├── ratedmoviesfull.csv ├── ratings.csv └── ratings_short.csv └── package.bluej /Java Programming Build a Recommendation System Certificate.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Java Programming Build a Recommendation System Certificate.pdf -------------------------------------------------------------------------------- /Java Programming and Software Engineering Fundamentals Certification.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Java Programming and Software Engineering Fundamentals Certification.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java-Programming-Build-a-Recommendation-System 2 | Repository for the capstone project of the "Java Programming and Software Engineering Fundamentals" program by Duke University on Coursera - Java Programming Build a Recommendation System. 3 | -------------------------------------------------------------------------------- /Week 1/FirstRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 1/FirstRatings.class -------------------------------------------------------------------------------- /Week 1/FirstRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=FirstRatings 3 | comment1.params=filename 4 | comment1.target=java.util.ArrayList\ loadMovies(java.lang.String) 5 | comment2.params= 6 | comment2.target=void\ testLoadMovies() 7 | comment3.params=filename 8 | comment3.target=java.util.ArrayList\ loadRaters(java.lang.String) 9 | comment4.params= 10 | comment4.target=void\ testLoadRaters() 11 | numComments=5 12 | -------------------------------------------------------------------------------- /Week 1/FirstRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * loadMovies and loadRaters methods add data from the related csv files into corresponding 4 | * ArrayLists. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 14, 2020) 8 | */ 9 | 10 | import edu.duke.*; 11 | import java.util.*; 12 | import org.apache.commons.csv.*; 13 | 14 | public class FirstRatings { 15 | public ArrayList loadMovies (String filename) { 16 | ArrayList movieData = new ArrayList (); 17 | 18 | FileResource fr = new FileResource("data/" + filename + ".csv"); 19 | CSVParser parser = fr.getCSVParser(); 20 | 21 | for (CSVRecord record: parser) { 22 | String currentID = record.get(0); 23 | String currentTitle = record.get(1); 24 | String currentYear = record.get(2); 25 | String currentCountry = record.get(3); 26 | String currentGenre = record.get(4); 27 | String currentDirector = record.get(5); 28 | int currentMinutes = Integer.parseInt(record.get(6)); 29 | String currentPoster = record.get(7); 30 | 31 | Movie currentMovie = new Movie(currentID, currentTitle, currentYear, currentGenre, currentDirector, 32 | currentCountry, currentPoster, currentMinutes); 33 | 34 | movieData.add(currentMovie); 35 | } 36 | 37 | return movieData; 38 | } 39 | 40 | // test cases for the file 41 | public void testLoadMovies () { 42 | ArrayList movies = loadMovies("ratedmoviesfull"); 43 | 44 | System.out.println("Number of movies: " + movies.size()); 45 | 46 | String countInGenre = "Comedy"; // variable 47 | int countComedies = 0; 48 | 49 | int minutes = 150; // variable 50 | int countMinutes = 0; 51 | 52 | for (Movie movie : movies) { 53 | if (movie.getGenres().contains(countInGenre)) { 54 | countComedies +=1; 55 | } 56 | 57 | if (movie.getMinutes() > minutes) { 58 | countMinutes +=1; 59 | } 60 | } 61 | System.out.println("There are " + countComedies + " comedies in the list"); 62 | System.out.println("There are " + countMinutes + " movies with more than " + minutes + 63 | " minutes in the list"); 64 | 65 | // Create a HashMap with count of how many movies each particular director filmed 66 | HashMap countMoviesByDirector = new HashMap (); 67 | for (Movie movie : movies) { 68 | String[] directors = movie.getDirector().split(","); 69 | 70 | for (String director : directors ) { 71 | director = director.trim(); 72 | if (! countMoviesByDirector.containsKey(director)) { 73 | countMoviesByDirector.put(director, 1); 74 | } else { 75 | countMoviesByDirector.put(director, countMoviesByDirector.get(director) + 1); 76 | } 77 | } 78 | } 79 | 80 | // Count max number of movies directed by a particular director 81 | int maxNumOfMovies = 0; 82 | for (String director : countMoviesByDirector.keySet()) { 83 | if (countMoviesByDirector.get(director) > maxNumOfMovies) { 84 | maxNumOfMovies = countMoviesByDirector.get(director); 85 | } 86 | } 87 | 88 | // Create an ArrayList with directors from the list that directed max number of movies 89 | ArrayList directorsList = new ArrayList (); 90 | for (String director : countMoviesByDirector.keySet()) { 91 | if (countMoviesByDirector.get(director) == maxNumOfMovies) { 92 | directorsList.add(director); 93 | } 94 | } 95 | System.out.println("Max number of movies directed by one director: " + maxNumOfMovies); 96 | System.out.println("Directors who directed that many movies are " + directorsList); 97 | } 98 | 99 | public ArrayList loadRaters (String filename) { 100 | ArrayList ratersData = new ArrayList (); 101 | ArrayList listOfIDs = new ArrayList (); 102 | 103 | FileResource fr = new FileResource("data/" + filename + ".csv"); 104 | CSVParser parser = fr.getCSVParser(); 105 | 106 | for (CSVRecord record : parser) { 107 | String currentRaterID = record.get(0); 108 | String currentMovieID = record.get(1); 109 | double currentMovieRating = Double.parseDouble(record.get(2)); 110 | 111 | if (! listOfIDs.contains(currentRaterID)) { 112 | Rater currentRater = new Rater(currentRaterID); 113 | ratersData.add(currentRater); 114 | currentRater.addRating(currentMovieID, currentMovieRating); 115 | 116 | } else { 117 | for (int k=0; k < ratersData.size(); k++) { 118 | if (ratersData.get(k).getID().equals(currentRaterID)) { 119 | ratersData.get(k).addRating(currentMovieID, currentMovieRating); 120 | } 121 | } 122 | } 123 | 124 | listOfIDs.add(currentRaterID); 125 | } 126 | 127 | return ratersData; 128 | } 129 | 130 | // test cases for the file 131 | public void testLoadRaters () { 132 | ArrayList raters = loadRaters("ratings"); 133 | 134 | System.out.println("Total number of raters: " + raters.size()); 135 | 136 | HashMap> hashmap = new HashMap> (); 137 | for (Rater rater : raters) { 138 | HashMap ratings = new HashMap (); 139 | ArrayList itemsRated = rater.getItemsRated(); 140 | 141 | for (int i=0; i < itemsRated.size(); i++) { 142 | String movieID = itemsRated.get(i); 143 | double movieRating = rater.getRating(movieID); 144 | 145 | ratings.put(movieID, movieRating); 146 | } 147 | hashmap.put(rater.getID(), ratings); 148 | } 149 | 150 | String raterID = "193"; //rater_id 151 | int ratingsSize = hashmap.get(raterID).size(); 152 | System.out.println("Number of ratings for the rater " + raterID + " : " + ratingsSize); 153 | 154 | int maxNumOfRatings = 0; 155 | for (String key : hashmap.keySet()) { 156 | int currAmountOfRatings = hashmap.get(key).size(); 157 | 158 | if (currAmountOfRatings > maxNumOfRatings) { 159 | maxNumOfRatings = currAmountOfRatings; 160 | } 161 | } 162 | System.out.println("Maximum number of ratings by any rater : " + maxNumOfRatings); 163 | 164 | ArrayList raterWithMaxNumOfRatings = new ArrayList (); 165 | for (String key : hashmap.keySet()) { 166 | int currAmountOfRatings = hashmap.get(key).size(); 167 | 168 | if (maxNumOfRatings == currAmountOfRatings) { 169 | raterWithMaxNumOfRatings.add(key); 170 | } 171 | } 172 | System.out.println("Rater(s) with the most number of movies rated : " + raterWithMaxNumOfRatings); 173 | 174 | String movieID = "1798709"; 175 | int numOfRatings = 0; 176 | for (String key : hashmap.keySet()) { 177 | if(hashmap.get(key).containsKey(movieID)) { 178 | numOfRatings +=1; 179 | } 180 | } 181 | System.out.println("Number of ratings movie " + movieID + " has : " + numOfRatings); 182 | 183 | ArrayList uniqueMovies = new ArrayList (); 184 | for (String key : hashmap.keySet()) { 185 | for (String currMovieID : hashmap.get(key).keySet()) { 186 | if (! uniqueMovies.contains(currMovieID)) { 187 | uniqueMovies.add(currMovieID); 188 | } 189 | } 190 | } 191 | System.out.println("Total number of movies that were rated : " + uniqueMovies.size()); 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /Week 1/Movie.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 1/Movie.class -------------------------------------------------------------------------------- /Week 1/Movie.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Movie 3 | comment1.params=anID\ aTitle\ aYear\ theGenres 4 | comment1.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String) 5 | comment10.params= 6 | comment10.target=int\ getMinutes() 7 | comment11.params= 8 | comment11.target=java.lang.String\ toString() 9 | comment2.params=anID\ aTitle\ aYear\ theGenres\ aDirector\ aCountry\ aPoster\ theMinutes 10 | comment2.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ int) 11 | comment3.params= 12 | comment3.target=java.lang.String\ getID() 13 | comment4.params= 14 | comment4.target=java.lang.String\ getTitle() 15 | comment5.params= 16 | comment5.target=int\ getYear() 17 | comment6.params= 18 | comment6.target=java.lang.String\ getGenres() 19 | comment7.params= 20 | comment7.target=java.lang.String\ getCountry() 21 | comment8.params= 22 | comment8.target=java.lang.String\ getDirector() 23 | comment9.params= 24 | comment9.target=java.lang.String\ getPoster() 25 | numComments=12 26 | -------------------------------------------------------------------------------- /Week 1/Movie.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | 4 | // An immutable passive data object (PDO) to represent item data 5 | public class Movie { 6 | private String id; 7 | private String title; 8 | private int year; 9 | private String genres; 10 | private String director; 11 | private String country; 12 | private String poster; 13 | private int minutes; 14 | 15 | public Movie (String anID, String aTitle, String aYear, String theGenres) { 16 | // just in case data file contains extra whitespace 17 | id = anID.trim(); 18 | title = aTitle.trim(); 19 | year = Integer.parseInt(aYear.trim()); 20 | genres = theGenres; 21 | } 22 | 23 | public Movie (String anID, String aTitle, String aYear, String theGenres, String aDirector, 24 | String aCountry, String aPoster, int theMinutes) { 25 | // just in case data file contains extra whitespace 26 | id = anID.trim(); 27 | title = aTitle.trim(); 28 | year = Integer.parseInt(aYear.trim()); 29 | genres = theGenres; 30 | director = aDirector; 31 | country = aCountry; 32 | poster = aPoster; 33 | minutes = theMinutes; 34 | } 35 | 36 | // Returns ID associated with this item 37 | public String getID () { 38 | return id; 39 | } 40 | 41 | // Returns title of this item 42 | public String getTitle () { 43 | return title; 44 | } 45 | 46 | // Returns year in which this item was published 47 | public int getYear () { 48 | return year; 49 | } 50 | 51 | // Returns genres associated with this item 52 | public String getGenres () { 53 | return genres; 54 | } 55 | 56 | public String getCountry(){ 57 | return country; 58 | } 59 | 60 | public String getDirector(){ 61 | return director; 62 | } 63 | 64 | public String getPoster(){ 65 | return poster; 66 | } 67 | 68 | public int getMinutes(){ 69 | return minutes; 70 | } 71 | 72 | // Returns a string of the item's information 73 | public String toString () { 74 | String result = "Movie [id=" + id + ", title=" + title + ", year=" + year; 75 | result += ", genres= " + genres + "]"; 76 | return result; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Week 1/Rater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 1/Rater.class -------------------------------------------------------------------------------- /Week 1/Rater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rater 3 | comment1.params=id 4 | comment1.target=Rater(java.lang.String) 5 | comment2.params=item\ rating 6 | comment2.target=void\ addRating(java.lang.String,\ double) 7 | comment3.params=item 8 | comment3.target=boolean\ hasRating(java.lang.String) 9 | comment4.params= 10 | comment4.target=java.lang.String\ getID() 11 | comment5.params=item 12 | comment5.target=double\ getRating(java.lang.String) 13 | comment6.params= 14 | comment6.target=int\ numRatings() 15 | comment7.params= 16 | comment7.target=java.util.ArrayList\ getItemsRated() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 1/Rater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Rater class contains various methods that are used to add and extract ratings information. 4 | * 5 | */ 6 | 7 | import java.util.*; 8 | 9 | public class Rater { 10 | private String myID; 11 | private ArrayList myRatings; 12 | 13 | public Rater(String id) { 14 | myID = id; 15 | myRatings = new ArrayList(); 16 | } 17 | 18 | public void addRating(String item, double rating) { 19 | myRatings.add(new Rating(item,rating)); 20 | } 21 | 22 | public boolean hasRating(String item) { 23 | for(int k=0; k < myRatings.size(); k++){ 24 | if (myRatings.get(k).getItem().equals(item)){ 25 | return true; 26 | } 27 | } 28 | 29 | return false; 30 | } 31 | 32 | public String getID() { 33 | return myID; 34 | } 35 | 36 | public double getRating(String item) { 37 | for(int k=0; k < myRatings.size(); k++){ 38 | if (myRatings.get(k).getItem().equals(item)){ 39 | return myRatings.get(k).getValue(); 40 | } 41 | } 42 | 43 | return -1; 44 | } 45 | 46 | public int numRatings() { 47 | return myRatings.size(); 48 | } 49 | 50 | public ArrayList getItemsRated() { 51 | ArrayList list = new ArrayList(); 52 | for(int k=0; k < myRatings.size(); k++){ 53 | list.add(myRatings.get(k).getItem()); 54 | } 55 | 56 | return list; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Week 1/Rating.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 1/Rating.class -------------------------------------------------------------------------------- /Week 1/Rating.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rating 3 | comment1.params=anItem\ aValue 4 | comment1.target=Rating(java.lang.String,\ double) 5 | comment2.params= 6 | comment2.target=java.lang.String\ getItem() 7 | comment3.params= 8 | comment3.target=double\ getValue() 9 | comment4.params= 10 | comment4.target=java.lang.String\ toString() 11 | comment5.params=other 12 | comment5.target=int\ compareTo(Rating) 13 | numComments=6 14 | -------------------------------------------------------------------------------- /Week 1/Rating.java: -------------------------------------------------------------------------------- 1 | // An immutable passive data object (PDO) to represent the rating data 2 | public class Rating implements Comparable { 3 | private String item; 4 | private double value; 5 | 6 | public Rating (String anItem, double aValue) { 7 | item = anItem; 8 | value = aValue; 9 | } 10 | 11 | // Returns item being rated 12 | public String getItem () { 13 | return item; 14 | } 15 | 16 | // Returns the value of this rating (as a number so it can be used in calculations) 17 | public double getValue () { 18 | return value; 19 | } 20 | 21 | // Returns a string of all the rating information 22 | public String toString () { 23 | return "[" + getItem() + ", " + getValue() + "]"; 24 | } 25 | 26 | public int compareTo(Rating other) { 27 | if (value < other.value) return -1; 28 | if (value > other.value) return 1; 29 | 30 | return 0; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Week 1/data/ratedmovies_short.csv: -------------------------------------------------------------------------------- 1 | id,title,year,country,genre,director,minutes,poster 2 | 0006414,"Behind the Screen",1916,"USA","Short, Comedy, Romance","Charles Chaplin",30,"http://ia.media-imdb.com/images/M/MV5BMTkyNDYyNTczNF5BMl5BanBnXkFtZTgwMDU2MzAwMzE@._V1_SX300.jpg" 3 | 0068646,"The Godfather",1972,"USA","Crime, Drama","Francis Ford Coppola",175,"http://ia.media-imdb.com/images/M/MV5BMjEyMjcyNDI4MF5BMl5BanBnXkFtZTcwMDA5Mzg3OA@@._V1_SX300.jpg" 4 | 0113277,"Heat",1995,"USA","Action, Crime, Drama","Michael Mann",170,"http://ia.media-imdb.com/images/M/MV5BMTM1NDc4ODkxNV5BMl5BanBnXkFtZTcwNTI4ODE3MQ@@._V1_SX300.jpg" 5 | 1798709,"Her",2013,"USA","Drama, Romance, Sci-Fi","Spike Jonze",126,"http://ia.media-imdb.com/images/M/MV5BMjA1Nzk0OTM2OF5BMl5BanBnXkFtZTgwNjU2NjEwMDE@._V1_SX300.jpg" 6 | 0790636,"Dallas Buyers Club",2013,"USA","Biography, Drama","Jean-Marc Vallée",117,"N/A" 7 | -------------------------------------------------------------------------------- /Week 1/data/ratings_short.csv: -------------------------------------------------------------------------------- 1 | rater_id,movie_id,rating,time 2 | 1,0068646,10,1381620027 3 | 1,0113277,10,1379466669 4 | 2,1798709,10,1389948338 5 | 2,0790636,7,1389963947 6 | 2,0068646,9,1382460093 7 | 3,1798709,9,1388641438 8 | 4,0068646,8,1362440416 9 | 4,1798709,6,1398043318 10 | 5,0068646,9,1364834910 11 | 5,1798709,8,1404338202 12 | -------------------------------------------------------------------------------- /Week 1/package.bluej: -------------------------------------------------------------------------------- 1 | #BlueJ package file 2 | dependency1.from=FirstRatings 3 | dependency1.to=Movie 4 | dependency1.type=UsesDependency 5 | dependency2.from=FirstRatings 6 | dependency2.to=Rater 7 | dependency2.type=UsesDependency 8 | dependency3.from=Rater 9 | dependency3.to=Rating 10 | dependency3.type=UsesDependency 11 | editor.fx.0.height=1038 12 | editor.fx.0.width=976 13 | editor.fx.0.x=952 14 | editor.fx.0.y=0 15 | objectbench.height=178 16 | objectbench.width=934 17 | package.divider.horizontal=0.6 18 | package.divider.vertical=0.7986942328618063 19 | package.editor.height=727 20 | package.editor.width=828 21 | package.editor.x=952 22 | package.editor.y=0 23 | package.frame.height=1038 24 | package.frame.width=976 25 | package.numDependencies=3 26 | package.numTargets=4 27 | package.showExtends=true 28 | package.showUses=true 29 | project.charset=UTF-8 30 | readme.height=58 31 | readme.name=@README 32 | readme.width=47 33 | readme.x=10 34 | readme.y=10 35 | target1.height=50 36 | target1.name=Movie 37 | target1.showInterface=false 38 | target1.type=ClassTarget 39 | target1.typeParameters= 40 | target1.width=80 41 | target1.x=280 42 | target1.y=60 43 | target2.height=50 44 | target2.name=FirstRatings 45 | target2.showInterface=false 46 | target2.type=ClassTarget 47 | target2.typeParameters= 48 | target2.width=100 49 | target2.x=430 50 | target2.y=150 51 | target3.height=50 52 | target3.name=Rater 53 | target3.showInterface=false 54 | target3.type=ClassTarget 55 | target3.typeParameters= 56 | target3.width=80 57 | target3.x=140 58 | target3.y=390 59 | target4.height=50 60 | target4.name=Rating 61 | target4.showInterface=false 62 | target4.type=ClassTarget 63 | target4.typeParameters= 64 | target4.width=80 65 | target4.x=50 66 | target4.y=110 67 | -------------------------------------------------------------------------------- /Week 2/FirstRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 2/FirstRatings.class -------------------------------------------------------------------------------- /Week 2/FirstRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=FirstRatings 3 | comment1.params=filename 4 | comment1.target=java.util.ArrayList\ loadMovies(java.lang.String) 5 | comment2.params=filename 6 | comment2.target=java.util.ArrayList\ loadRaters(java.lang.String) 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 2/FirstRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * loadMovies and loadRaters methods add data from the related csv files into corresponding 4 | * ArrayLists. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 14, 2020) 8 | */ 9 | 10 | import edu.duke.*; 11 | import java.util.*; 12 | import org.apache.commons.csv.*; 13 | 14 | public class FirstRatings { 15 | public ArrayList loadMovies (String filename) { 16 | ArrayList movieData = new ArrayList (); 17 | 18 | FileResource fr = new FileResource("data/" + filename + ".csv"); 19 | CSVParser parser = fr.getCSVParser(); 20 | 21 | for (CSVRecord record: parser) { 22 | String currentID = record.get(0); 23 | String currentTitle = record.get(1); 24 | String currentYear = record.get(2); 25 | String currentCountry = record.get(3); 26 | String currentGenre = record.get(4); 27 | String currentDirector = record.get(5); 28 | int currentMinutes = Integer.parseInt(record.get(6)); 29 | String currentPoster = record.get(7); 30 | 31 | Movie currentMovie = new Movie(currentID, currentTitle, currentYear, currentGenre, currentDirector, 32 | currentCountry, currentPoster, currentMinutes); 33 | 34 | movieData.add(currentMovie); 35 | } 36 | 37 | return movieData; 38 | } 39 | 40 | public ArrayList loadRaters (String filename) { 41 | ArrayList ratersData = new ArrayList (); 42 | ArrayList listOfIDs = new ArrayList (); 43 | 44 | FileResource fr = new FileResource("data/" + filename + ".csv"); 45 | CSVParser parser = fr.getCSVParser(); 46 | 47 | for (CSVRecord record : parser) { 48 | String currentRaterID = record.get(0); 49 | String currentMovieID = record.get(1); 50 | double currentMovieRating = Double.parseDouble(record.get(2)); 51 | 52 | if (! listOfIDs.contains(currentRaterID)) { 53 | Rater currentRater = new Rater(currentRaterID); 54 | ratersData.add(currentRater); 55 | currentRater.addRating(currentMovieID, currentMovieRating); 56 | 57 | } else { 58 | for (int k=0; k < ratersData.size(); k++) { 59 | if (ratersData.get(k).getID().equals(currentRaterID)) { 60 | ratersData.get(k).addRating(currentMovieID, currentMovieRating); 61 | } 62 | } 63 | } 64 | 65 | listOfIDs.add(currentRaterID); 66 | } 67 | 68 | return ratersData; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Week 2/Movie.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 2/Movie.class -------------------------------------------------------------------------------- /Week 2/Movie.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Movie 3 | comment1.params=anID\ aTitle\ aYear\ theGenres 4 | comment1.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String) 5 | comment10.params= 6 | comment10.target=int\ getMinutes() 7 | comment11.params= 8 | comment11.target=java.lang.String\ toString() 9 | comment2.params=anID\ aTitle\ aYear\ theGenres\ aDirector\ aCountry\ aPoster\ theMinutes 10 | comment2.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ int) 11 | comment3.params= 12 | comment3.target=java.lang.String\ getID() 13 | comment4.params= 14 | comment4.target=java.lang.String\ getTitle() 15 | comment5.params= 16 | comment5.target=int\ getYear() 17 | comment6.params= 18 | comment6.target=java.lang.String\ getGenres() 19 | comment7.params= 20 | comment7.target=java.lang.String\ getCountry() 21 | comment8.params= 22 | comment8.target=java.lang.String\ getDirector() 23 | comment9.params= 24 | comment9.target=java.lang.String\ getPoster() 25 | numComments=12 26 | -------------------------------------------------------------------------------- /Week 2/Movie.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | 4 | // An immutable passive data object (PDO) to represent item data 5 | public class Movie { 6 | private String id; 7 | private String title; 8 | private int year; 9 | private String genres; 10 | private String director; 11 | private String country; 12 | private String poster; 13 | private int minutes; 14 | 15 | public Movie (String anID, String aTitle, String aYear, String theGenres) { 16 | // just in case data file contains extra whitespace 17 | id = anID.trim(); 18 | title = aTitle.trim(); 19 | year = Integer.parseInt(aYear.trim()); 20 | genres = theGenres; 21 | } 22 | 23 | public Movie (String anID, String aTitle, String aYear, String theGenres, String aDirector, 24 | String aCountry, String aPoster, int theMinutes) { 25 | // just in case data file contains extra whitespace 26 | id = anID.trim(); 27 | title = aTitle.trim(); 28 | year = Integer.parseInt(aYear.trim()); 29 | genres = theGenres; 30 | director = aDirector; 31 | country = aCountry; 32 | poster = aPoster; 33 | minutes = theMinutes; 34 | } 35 | 36 | // Returns ID associated with this item 37 | public String getID () { 38 | return id; 39 | } 40 | 41 | // Returns title of this item 42 | public String getTitle () { 43 | return title; 44 | } 45 | 46 | // Returns year in which this item was published 47 | public int getYear () { 48 | return year; 49 | } 50 | 51 | // Returns genres associated with this item 52 | public String getGenres () { 53 | return genres; 54 | } 55 | 56 | public String getCountry(){ 57 | return country; 58 | } 59 | 60 | public String getDirector(){ 61 | return director; 62 | } 63 | 64 | public String getPoster(){ 65 | return poster; 66 | } 67 | 68 | public int getMinutes(){ 69 | return minutes; 70 | } 71 | 72 | // Returns a string of the item's information 73 | public String toString () { 74 | String result = "Movie [id=" + id + ", title=" + title + ", year=" + year; 75 | result += ", genres= " + genres + "]"; 76 | return result; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Week 2/MovieRunnerAverage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 2/MovieRunnerAverage.class -------------------------------------------------------------------------------- /Week 2/MovieRunnerAverage.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MovieRunnerAverage 3 | comment1.params= 4 | comment1.target=void\ printAverageRatings() 5 | comment2.params= 6 | comment2.target=void\ getAverageRatingOneMovie() 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 2/MovieRunnerAverage.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * MovieRunnerAverage class contains tests for the getAverageRatings method from the 4 | * secondRatings class. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 18, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class MovieRunnerAverage { 13 | public void printAverageRatings () { 14 | SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings"); 15 | 16 | System.out.println("Total number of movies : " + secondRatings.getMovieSize()); 17 | System.out.println("Total number of raters : " + secondRatings.getRaterSize()); 18 | 19 | int MinNumOfRatings = 20; // variable 20 | ArrayList averageRatings = secondRatings.getAverageRatings(MinNumOfRatings); 21 | Collections.sort(averageRatings); 22 | for (Rating rating : averageRatings) { 23 | System.out.println(rating.getValue() + " " + secondRatings.getTitle(rating.getItem())); 24 | } 25 | System.out.println("There are " + averageRatings.size() + " movies with " + 26 | MinNumOfRatings + " or more ratings"); 27 | } 28 | 29 | public void getAverageRatingOneMovie () { 30 | SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings"); 31 | 32 | String title = "Vacation"; // variable 33 | int MinNumOfRatings = 1; // variable 34 | 35 | String movieID = secondRatings.getID(title); 36 | ArrayList averageRatings = secondRatings.getAverageRatings(MinNumOfRatings); 37 | for (Rating rating : averageRatings) { 38 | if (rating.getItem().equals(movieID)) { 39 | System.out.println("For movie \"" + title + "\" the average rating is " 40 | + rating.getValue()); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Week 2/Rater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 2/Rater.class -------------------------------------------------------------------------------- /Week 2/Rater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rater 3 | comment1.params=id 4 | comment1.target=Rater(java.lang.String) 5 | comment2.params=item\ rating 6 | comment2.target=void\ addRating(java.lang.String,\ double) 7 | comment3.params=item 8 | comment3.target=boolean\ hasRating(java.lang.String) 9 | comment4.params= 10 | comment4.target=java.lang.String\ getID() 11 | comment5.params=item 12 | comment5.target=double\ getRating(java.lang.String) 13 | comment6.params= 14 | comment6.target=int\ numRatings() 15 | comment7.params= 16 | comment7.target=java.util.ArrayList\ getItemsRated() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 2/Rater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Rater class contains various methods that are used to add and extract ratings information. 4 | * 5 | */ 6 | 7 | import java.util.*; 8 | 9 | public class Rater { 10 | private String myID; 11 | private ArrayList myRatings; 12 | 13 | public Rater(String id) { 14 | myID = id; 15 | myRatings = new ArrayList(); 16 | } 17 | 18 | public void addRating(String item, double rating) { 19 | myRatings.add(new Rating(item,rating)); 20 | } 21 | 22 | public boolean hasRating(String item) { 23 | for(int k=0; k < myRatings.size(); k++){ 24 | if (myRatings.get(k).getItem().equals(item)){ 25 | return true; 26 | } 27 | } 28 | 29 | return false; 30 | } 31 | 32 | public String getID() { 33 | return myID; 34 | } 35 | 36 | public double getRating(String item) { 37 | for(int k=0; k < myRatings.size(); k++){ 38 | if (myRatings.get(k).getItem().equals(item)){ 39 | return myRatings.get(k).getValue(); 40 | } 41 | } 42 | 43 | return -1; 44 | } 45 | 46 | public int numRatings() { 47 | return myRatings.size(); 48 | } 49 | 50 | public ArrayList getItemsRated() { 51 | ArrayList list = new ArrayList(); 52 | for(int k=0; k < myRatings.size(); k++){ 53 | list.add(myRatings.get(k).getItem()); 54 | } 55 | 56 | return list; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Week 2/Rating.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 2/Rating.class -------------------------------------------------------------------------------- /Week 2/Rating.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rating 3 | comment1.params=anItem\ aValue 4 | comment1.target=Rating(java.lang.String,\ double) 5 | comment2.params= 6 | comment2.target=java.lang.String\ getItem() 7 | comment3.params= 8 | comment3.target=double\ getValue() 9 | comment4.params= 10 | comment4.target=java.lang.String\ toString() 11 | comment5.params=other 12 | comment5.target=int\ compareTo(Rating) 13 | numComments=6 14 | -------------------------------------------------------------------------------- /Week 2/Rating.java: -------------------------------------------------------------------------------- 1 | // An immutable passive data object (PDO) to represent the rating data 2 | public class Rating implements Comparable { 3 | private String item; 4 | private double value; 5 | 6 | public Rating (String anItem, double aValue) { 7 | item = anItem; 8 | value = aValue; 9 | } 10 | 11 | // Returns item being rated 12 | public String getItem () { 13 | return item; 14 | } 15 | 16 | // Returns the value of this rating (as a number so it can be used in calculations) 17 | public double getValue () { 18 | return value; 19 | } 20 | 21 | // Returns a string of all the rating information 22 | public String toString () { 23 | return "[" + getItem() + ", " + getValue() + "]"; 24 | } 25 | 26 | public int compareTo(Rating other) { 27 | if (value < other.value) { 28 | return -1; 29 | } 30 | if (value > other.value) { 31 | return 1; 32 | } 33 | return 0; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Week 2/SecondRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 2/SecondRatings.class -------------------------------------------------------------------------------- /Week 2/SecondRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=SecondRatings 3 | comment1.params= 4 | comment1.target=SecondRatings() 5 | comment2.params=moviefile\ ratingsfile 6 | comment2.target=SecondRatings(java.lang.String,\ java.lang.String) 7 | comment3.params= 8 | comment3.target=int\ getMovieSize() 9 | comment4.params= 10 | comment4.target=int\ getRaterSize() 11 | comment5.params=id\ minimalRaters 12 | comment5.target=double\ getAverageByID(java.lang.String,\ int) 13 | comment6.params=minimalRaters 14 | comment6.target=java.util.ArrayList\ getAverageRatings(int) 15 | comment7.params=id 16 | comment7.target=java.lang.String\ getTitle(java.lang.String) 17 | comment8.params=title 18 | comment8.target=java.lang.String\ getID(java.lang.String) 19 | numComments=9 20 | -------------------------------------------------------------------------------- /Week 2/SecondRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * SecondRatings class contains various methods to extract information from the corresponding 4 | * ArrayLists. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 18, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class SecondRatings { 13 | private ArrayList myMovies; 14 | private ArrayList myRaters; 15 | 16 | public SecondRatings() { 17 | // default constructor 18 | this("ratedmoviesfull.csv", "ratings.csv"); 19 | } 20 | 21 | public SecondRatings (String moviefile, String ratingsfile) { 22 | FirstRatings firstRatings = new FirstRatings (); 23 | myMovies = firstRatings.loadMovies(moviefile); 24 | myRaters = firstRatings.loadRaters(ratingsfile); 25 | } 26 | 27 | public int getMovieSize () { 28 | return myMovies.size(); 29 | } 30 | 31 | public int getRaterSize () { 32 | return myRaters.size(); 33 | } 34 | 35 | private double getAverageByID (String id, int minimalRaters) { 36 | double sum = 0.0; 37 | int count = 0; 38 | 39 | for (Rater rater : myRaters) { 40 | if (rater.hasRating(id)) { 41 | sum += rater.getRating(id); 42 | count += 1; 43 | } 44 | } 45 | 46 | if (count >= minimalRaters) { 47 | return sum / count; 48 | } else { 49 | return 0.0; 50 | } 51 | } 52 | 53 | public ArrayList getAverageRatings (int minimalRaters) { 54 | ArrayList averageRatings = new ArrayList (); 55 | 56 | for (Movie movie : myMovies) { 57 | String movieID = movie.getID(); 58 | //double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 59 | double average = getAverageByID(movieID, minimalRaters); 60 | if (average != 0.0) { 61 | Rating rating = new Rating (movieID, average); 62 | averageRatings.add(rating); 63 | } 64 | } 65 | 66 | return averageRatings; 67 | } 68 | 69 | public String getTitle (String id) { 70 | String title = null; 71 | 72 | for (Movie movie : myMovies) { 73 | if (movie.getID().equals(id)) { 74 | title = movie.getTitle(); 75 | } 76 | } 77 | 78 | if (title != null) { 79 | return title; 80 | } else { 81 | return "No movie with such ID was found."; 82 | } 83 | } 84 | 85 | public String getID (String title) { 86 | String movieID = null; 87 | 88 | for (Movie movie : myMovies) { 89 | if (movie.getTitle().equals(title)) { 90 | movieID = movie.getID(); 91 | } 92 | } 93 | 94 | if (movieID != null) { 95 | return movieID; 96 | } else { 97 | return "NO SUCH TITLE."; 98 | } 99 | } 100 | 101 | 102 | } 103 | -------------------------------------------------------------------------------- /Week 2/data/ratedmovies_short.csv: -------------------------------------------------------------------------------- 1 | id,title,year,country,genre,director,minutes,poster 2 | 0006414,"Behind the Screen",1916,"USA","Short, Comedy, Romance","Charles Chaplin",30,"http://ia.media-imdb.com/images/M/MV5BMTkyNDYyNTczNF5BMl5BanBnXkFtZTgwMDU2MzAwMzE@._V1_SX300.jpg" 3 | 0068646,"The Godfather",1972,"USA","Crime, Drama","Francis Ford Coppola",175,"http://ia.media-imdb.com/images/M/MV5BMjEyMjcyNDI4MF5BMl5BanBnXkFtZTcwMDA5Mzg3OA@@._V1_SX300.jpg" 4 | 0113277,"Heat",1995,"USA","Action, Crime, Drama","Michael Mann",170,"http://ia.media-imdb.com/images/M/MV5BMTM1NDc4ODkxNV5BMl5BanBnXkFtZTcwNTI4ODE3MQ@@._V1_SX300.jpg" 5 | 1798709,"Her",2013,"USA","Drama, Romance, Sci-Fi","Spike Jonze",126,"http://ia.media-imdb.com/images/M/MV5BMjA1Nzk0OTM2OF5BMl5BanBnXkFtZTgwNjU2NjEwMDE@._V1_SX300.jpg" 6 | 0790636,"Dallas Buyers Club",2013,"USA","Biography, Drama","Jean-Marc Vallée",117,"N/A" 7 | -------------------------------------------------------------------------------- /Week 2/data/ratings_short.csv: -------------------------------------------------------------------------------- 1 | rater_id,movie_id,rating,time 2 | 1,0068646,10,1381620027 3 | 1,0113277,10,1379466669 4 | 2,1798709,10,1389948338 5 | 2,0790636,7,1389963947 6 | 2,0068646,9,1382460093 7 | 3,1798709,9,1388641438 8 | 4,0068646,8,1362440416 9 | 4,1798709,6,1398043318 10 | 5,0068646,9,1364834910 11 | 5,1798709,8,1404338202 12 | -------------------------------------------------------------------------------- /Week 2/package.bluej: -------------------------------------------------------------------------------- 1 | #BlueJ package file 2 | dependency1.from=Rater 3 | dependency1.to=Rating 4 | dependency1.type=UsesDependency 5 | dependency2.from=MovieRunnerAverage 6 | dependency2.to=SecondRatings 7 | dependency2.type=UsesDependency 8 | dependency3.from=MovieRunnerAverage 9 | dependency3.to=Rating 10 | dependency3.type=UsesDependency 11 | dependency4.from=SecondRatings 12 | dependency4.to=Movie 13 | dependency4.type=UsesDependency 14 | dependency5.from=SecondRatings 15 | dependency5.to=Rater 16 | dependency5.type=UsesDependency 17 | dependency6.from=SecondRatings 18 | dependency6.to=FirstRatings 19 | dependency6.type=UsesDependency 20 | dependency7.from=SecondRatings 21 | dependency7.to=Rating 22 | dependency7.type=UsesDependency 23 | dependency8.from=FirstRatings 24 | dependency8.to=Movie 25 | dependency8.type=UsesDependency 26 | dependency9.from=FirstRatings 27 | dependency9.to=Rater 28 | dependency9.type=UsesDependency 29 | editor.fx.0.height=1038 30 | editor.fx.0.width=976 31 | editor.fx.0.x=-8 32 | editor.fx.0.y=0 33 | objectbench.height=92 34 | objectbench.width=934 35 | package.divider.horizontal=0.6 36 | package.divider.vertical=0.8922742110990207 37 | package.editor.height=813 38 | package.editor.width=828 39 | package.editor.x=-8 40 | package.editor.y=0 41 | package.frame.height=1038 42 | package.frame.width=976 43 | package.numDependencies=9 44 | package.numTargets=6 45 | package.showExtends=true 46 | package.showUses=true 47 | project.charset=UTF-8 48 | readme.height=58 49 | readme.name=@README 50 | readme.width=47 51 | readme.x=10 52 | readme.y=10 53 | target1.height=50 54 | target1.name=Movie 55 | target1.showInterface=false 56 | target1.type=ClassTarget 57 | target1.typeParameters= 58 | target1.width=80 59 | target1.x=390 60 | target1.y=90 61 | target2.height=50 62 | target2.name=FirstRatings 63 | target2.showInterface=false 64 | target2.type=ClassTarget 65 | target2.typeParameters= 66 | target2.width=100 67 | target2.x=450 68 | target2.y=320 69 | target3.height=50 70 | target3.name=SecondRatings 71 | target3.showInterface=false 72 | target3.type=ClassTarget 73 | target3.typeParameters= 74 | target3.width=120 75 | target3.x=670 76 | target3.y=10 77 | target4.height=50 78 | target4.name=Rater 79 | target4.showInterface=false 80 | target4.type=ClassTarget 81 | target4.typeParameters= 82 | target4.width=80 83 | target4.x=260 84 | target4.y=630 85 | target5.height=50 86 | target5.name=MovieRunnerAverage 87 | target5.showInterface=false 88 | target5.type=ClassTarget 89 | target5.typeParameters= 90 | target5.width=160 91 | target5.x=80 92 | target5.y=160 93 | target6.height=50 94 | target6.name=Rating 95 | target6.showInterface=false 96 | target6.type=ClassTarget 97 | target6.typeParameters= 98 | target6.width=80 99 | target6.x=50 100 | target6.y=460 101 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/EfficientRater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 1/EfficientRater.class -------------------------------------------------------------------------------- /Week 3/Assignment 1/EfficientRater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=EfficientRater 3 | comment1.params=id 4 | comment1.target=EfficientRater(java.lang.String) 5 | comment2.params=item\ rating 6 | comment2.target=void\ addRating(java.lang.String,\ double) 7 | comment3.params=item 8 | comment3.target=boolean\ hasRating(java.lang.String) 9 | comment4.params= 10 | comment4.target=java.lang.String\ getID() 11 | comment5.params=item 12 | comment5.target=double\ getRating(java.lang.String) 13 | comment6.params= 14 | comment6.target=int\ numRatings() 15 | comment7.params= 16 | comment7.target=java.util.ArrayList\ getItemsRated() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/EfficientRater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * EfficientRater class contains various methods that are used to add and extract ratings 4 | * from the ratings HashMap. 5 | * 6 | * @author (your name) 7 | * @version (a version number or a date) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class EfficientRater implements Rater { 13 | private String myID; 14 | private HashMap myRatings; 15 | 16 | public EfficientRater(String id) { 17 | myID = id; 18 | myRatings = new HashMap (); 19 | } 20 | 21 | public void addRating(String item, double rating) { 22 | myRatings.put(item, new Rating(item,rating)); 23 | } 24 | 25 | public boolean hasRating(String item) { 26 | if (myRatings.containsKey(item)) { 27 | return true; 28 | } 29 | 30 | return false; 31 | } 32 | 33 | public String getID() { 34 | return myID; 35 | } 36 | 37 | public double getRating(String item) { 38 | for (String movieID : myRatings.keySet()) { 39 | if (movieID.equals(item)) { 40 | return myRatings.get(movieID).getValue(); 41 | } 42 | } 43 | 44 | return -1; 45 | } 46 | 47 | public int numRatings() { 48 | return myRatings.size(); 49 | } 50 | 51 | public ArrayList getItemsRated() { 52 | ArrayList list = new ArrayList (); 53 | for (String movieID : myRatings.keySet()) { 54 | list.add(myRatings.get(movieID).getItem()); 55 | } 56 | 57 | return list; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/FirstRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 1/FirstRatings.class -------------------------------------------------------------------------------- /Week 3/Assignment 1/FirstRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=FirstRatings 3 | comment1.params=filename 4 | comment1.target=java.util.ArrayList\ loadMovies(java.lang.String) 5 | comment2.params=filename 6 | comment2.target=java.util.ArrayList\ loadRaters(java.lang.String) 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/FirstRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * loadMovies and loadRaters methods add data from the related csv files into corresponding 4 | * ArrayLists. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 14, 2020) 8 | */ 9 | 10 | import edu.duke.*; 11 | import java.util.*; 12 | import org.apache.commons.csv.*; 13 | 14 | public class FirstRatings { 15 | public ArrayList loadMovies (String filename) { 16 | ArrayList movieData = new ArrayList (); 17 | 18 | FileResource fr = new FileResource("data/" + filename + ".csv"); 19 | CSVParser parser = fr.getCSVParser(); 20 | 21 | for (CSVRecord record: parser) { 22 | String currentID = record.get(0); 23 | String currentTitle = record.get(1); 24 | String currentYear = record.get(2); 25 | String currentCountry = record.get(3); 26 | String currentGenre = record.get(4); 27 | String currentDirector = record.get(5); 28 | int currentMinutes = Integer.parseInt(record.get(6)); 29 | String currentPoster = record.get(7); 30 | 31 | Movie currentMovie = new Movie(currentID, currentTitle, currentYear, currentGenre, currentDirector, 32 | currentCountry, currentPoster, currentMinutes); 33 | 34 | movieData.add(currentMovie); 35 | } 36 | 37 | return movieData; 38 | } 39 | 40 | public ArrayList loadRaters (String filename) { 41 | ArrayList ratersData = new ArrayList (); 42 | ArrayList listOfIDs = new ArrayList (); 43 | 44 | FileResource fr = new FileResource("data/" + filename + ".csv"); 45 | CSVParser parser = fr.getCSVParser(); 46 | 47 | for (CSVRecord record : parser) { 48 | String currentRaterID = record.get(0); 49 | String currentMovieID = record.get(1); 50 | double currentMovieRating = Double.parseDouble(record.get(2)); 51 | 52 | if (! listOfIDs.contains(currentRaterID)) { 53 | //Rater currentRater = new PlainRater(currentRaterID); 54 | Rater currentRater = new EfficientRater(currentRaterID); 55 | ratersData.add(currentRater); 56 | currentRater.addRating(currentMovieID, currentMovieRating); 57 | 58 | } else { 59 | for (int k=0; k < ratersData.size(); k++) { 60 | if (ratersData.get(k).getID().equals(currentRaterID)) { 61 | ratersData.get(k).addRating(currentMovieID, currentMovieRating); 62 | } 63 | } 64 | } 65 | 66 | listOfIDs.add(currentRaterID); 67 | } 68 | 69 | return ratersData; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/Movie.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 1/Movie.class -------------------------------------------------------------------------------- /Week 3/Assignment 1/Movie.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Movie 3 | comment1.params=anID\ aTitle\ aYear\ theGenres 4 | comment1.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String) 5 | comment10.params= 6 | comment10.target=int\ getMinutes() 7 | comment11.params= 8 | comment11.target=java.lang.String\ toString() 9 | comment2.params=anID\ aTitle\ aYear\ theGenres\ aDirector\ aCountry\ aPoster\ theMinutes 10 | comment2.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ int) 11 | comment3.params= 12 | comment3.target=java.lang.String\ getID() 13 | comment4.params= 14 | comment4.target=java.lang.String\ getTitle() 15 | comment5.params= 16 | comment5.target=int\ getYear() 17 | comment6.params= 18 | comment6.target=java.lang.String\ getGenres() 19 | comment7.params= 20 | comment7.target=java.lang.String\ getCountry() 21 | comment8.params= 22 | comment8.target=java.lang.String\ getDirector() 23 | comment9.params= 24 | comment9.target=java.lang.String\ getPoster() 25 | numComments=12 26 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/Movie.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | 4 | // An immutable passive data object (PDO) to represent item data 5 | public class Movie { 6 | private String id; 7 | private String title; 8 | private int year; 9 | private String genres; 10 | private String director; 11 | private String country; 12 | private String poster; 13 | private int minutes; 14 | 15 | public Movie (String anID, String aTitle, String aYear, String theGenres) { 16 | // just in case data file contains extra whitespace 17 | id = anID.trim(); 18 | title = aTitle.trim(); 19 | year = Integer.parseInt(aYear.trim()); 20 | genres = theGenres; 21 | } 22 | 23 | public Movie (String anID, String aTitle, String aYear, String theGenres, String aDirector, 24 | String aCountry, String aPoster, int theMinutes) { 25 | // just in case data file contains extra whitespace 26 | id = anID.trim(); 27 | title = aTitle.trim(); 28 | year = Integer.parseInt(aYear.trim()); 29 | genres = theGenres; 30 | director = aDirector; 31 | country = aCountry; 32 | poster = aPoster; 33 | minutes = theMinutes; 34 | } 35 | 36 | // Returns ID associated with this item 37 | public String getID () { 38 | return id; 39 | } 40 | 41 | // Returns title of this item 42 | public String getTitle () { 43 | return title; 44 | } 45 | 46 | // Returns year in which this item was published 47 | public int getYear () { 48 | return year; 49 | } 50 | 51 | // Returns genres associated with this item 52 | public String getGenres () { 53 | return genres; 54 | } 55 | 56 | public String getCountry(){ 57 | return country; 58 | } 59 | 60 | public String getDirector(){ 61 | return director; 62 | } 63 | 64 | public String getPoster(){ 65 | return poster; 66 | } 67 | 68 | public int getMinutes(){ 69 | return minutes; 70 | } 71 | 72 | // Returns a string of the item's information 73 | public String toString () { 74 | String result = "Movie [id=" + id + ", title=" + title + ", year=" + year; 75 | result += ", genres= " + genres + "]"; 76 | return result; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/MovieRunnerAverage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 1/MovieRunnerAverage.class -------------------------------------------------------------------------------- /Week 3/Assignment 1/MovieRunnerAverage.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MovieRunnerAverage 3 | comment1.params= 4 | comment1.target=void\ printAverageRatings() 5 | comment2.params= 6 | comment2.target=void\ getAverageRatingOneMovie() 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/MovieRunnerAverage.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * MovieRunnerAverage class contains tests for the getAverageRatings method from the 4 | * secondRatings class. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 18, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class MovieRunnerAverage { 13 | public void printAverageRatings () { 14 | SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings"); 15 | 16 | System.out.println("Total number of movies : " + secondRatings.getMovieSize()); 17 | System.out.println("Total number of raters : " + secondRatings.getRaterSize()); 18 | 19 | int MinNumOfRatings = 20; // variable 20 | ArrayList averageRatings = secondRatings.getAverageRatings(MinNumOfRatings); 21 | Collections.sort(averageRatings); 22 | for (Rating rating : averageRatings) { 23 | System.out.println(rating.getValue() + " " + secondRatings.getTitle(rating.getItem())); 24 | } 25 | System.out.println("There are " + averageRatings.size() + " movies with " + 26 | MinNumOfRatings + " or more ratings"); 27 | } 28 | 29 | public void getAverageRatingOneMovie () { 30 | SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings"); 31 | 32 | String title = "Vacation"; // variable 33 | int MinNumOfRatings = 1; // variable 34 | 35 | String movieID = secondRatings.getID(title); 36 | ArrayList averageRatings = secondRatings.getAverageRatings(MinNumOfRatings); 37 | for (Rating rating : averageRatings) { 38 | if (rating.getItem().equals(movieID)) { 39 | System.out.println("For movie \"" + title + "\" the average rating is " 40 | + rating.getValue()); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/PlainRater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 1/PlainRater.class -------------------------------------------------------------------------------- /Week 3/Assignment 1/PlainRater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=PlainRater 3 | comment1.params=id 4 | comment1.target=PlainRater(java.lang.String) 5 | comment2.params=item\ rating 6 | comment2.target=void\ addRating(java.lang.String,\ double) 7 | comment3.params=item 8 | comment3.target=boolean\ hasRating(java.lang.String) 9 | comment4.params= 10 | comment4.target=java.lang.String\ getID() 11 | comment5.params=item 12 | comment5.target=double\ getRating(java.lang.String) 13 | comment6.params= 14 | comment6.target=int\ numRatings() 15 | comment7.params= 16 | comment7.target=java.util.ArrayList\ getItemsRated() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/PlainRater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * PlainRater class contains various methods that are used to add and extract ratings information. 4 | * 5 | */ 6 | 7 | import java.util.*; 8 | 9 | public class PlainRater implements Rater { 10 | private String myID; 11 | private ArrayList myRatings; 12 | 13 | public PlainRater(String id) { 14 | myID = id; 15 | myRatings = new ArrayList(); 16 | } 17 | 18 | public void addRating(String item, double rating) { 19 | myRatings.add(new Rating(item,rating)); 20 | } 21 | 22 | public boolean hasRating(String item) { 23 | for(int k=0; k < myRatings.size(); k++){ 24 | if (myRatings.get(k).getItem().equals(item)){ 25 | return true; 26 | } 27 | } 28 | 29 | return false; 30 | } 31 | 32 | public String getID() { 33 | return myID; 34 | } 35 | 36 | public double getRating(String item) { 37 | for(int k=0; k < myRatings.size(); k++){ 38 | if (myRatings.get(k).getItem().equals(item)){ 39 | return myRatings.get(k).getValue(); 40 | } 41 | } 42 | 43 | return -1; 44 | } 45 | 46 | public int numRatings() { 47 | return myRatings.size(); 48 | } 49 | 50 | public ArrayList getItemsRated() { 51 | ArrayList list = new ArrayList(); 52 | for(int k=0; k < myRatings.size(); k++){ 53 | list.add(myRatings.get(k).getItem()); 54 | } 55 | 56 | return list; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/Rater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 1/Rater.class -------------------------------------------------------------------------------- /Week 3/Assignment 1/Rater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rater 3 | comment1.params=item\ rating 4 | comment1.target=void\ addRating(java.lang.String,\ double) 5 | comment2.params=item 6 | comment2.target=boolean\ hasRating(java.lang.String) 7 | comment3.params= 8 | comment3.target=java.lang.String\ getID() 9 | comment4.params=item 10 | comment4.target=double\ getRating(java.lang.String) 11 | comment5.params= 12 | comment5.target=int\ numRatings() 13 | comment6.params= 14 | comment6.target=java.util.ArrayList\ getItemsRated() 15 | numComments=7 16 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/Rater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Rater is an interface for PlainRater and EfficientRater classes. 4 | * 5 | * @ Konstantin Krumin 6 | * @ Version: 1.0 (February 19, 2020) 7 | */ 8 | 9 | import java.util.*; 10 | 11 | public interface Rater { 12 | public void addRating(String item, double rating); 13 | 14 | public boolean hasRating(String item); 15 | 16 | public String getID(); 17 | 18 | public double getRating(String item); 19 | 20 | public int numRatings(); 21 | 22 | public ArrayList getItemsRated(); 23 | } 24 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/Rating.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 1/Rating.class -------------------------------------------------------------------------------- /Week 3/Assignment 1/Rating.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rating 3 | comment1.params=anItem\ aValue 4 | comment1.target=Rating(java.lang.String,\ double) 5 | comment2.params= 6 | comment2.target=java.lang.String\ getItem() 7 | comment3.params= 8 | comment3.target=double\ getValue() 9 | comment4.params= 10 | comment4.target=java.lang.String\ toString() 11 | comment5.params=other 12 | comment5.target=int\ compareTo(Rating) 13 | numComments=6 14 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/Rating.java: -------------------------------------------------------------------------------- 1 | // An immutable passive data object (PDO) to represent the rating data 2 | public class Rating implements Comparable { 3 | private String item; 4 | private double value; 5 | 6 | public Rating (String anItem, double aValue) { 7 | item = anItem; 8 | value = aValue; 9 | } 10 | 11 | // Returns item being rated 12 | public String getItem () { 13 | return item; 14 | } 15 | 16 | // Returns the value of this rating (as a number so it can be used in calculations) 17 | public double getValue () { 18 | return value; 19 | } 20 | 21 | // Returns a string of all the rating information 22 | public String toString () { 23 | return "[" + getItem() + ", " + getValue() + "]"; 24 | } 25 | 26 | public int compareTo(Rating other) { 27 | if (value < other.value) { 28 | return -1; 29 | } 30 | if (value > other.value) { 31 | return 1; 32 | } 33 | return 0; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/SecondRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 1/SecondRatings.class -------------------------------------------------------------------------------- /Week 3/Assignment 1/SecondRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=SecondRatings 3 | comment1.params= 4 | comment1.target=SecondRatings() 5 | comment2.params=moviefile\ ratingsfile 6 | comment2.target=SecondRatings(java.lang.String,\ java.lang.String) 7 | comment3.params= 8 | comment3.target=int\ getMovieSize() 9 | comment4.params= 10 | comment4.target=int\ getRaterSize() 11 | comment5.params=id\ minimalRaters 12 | comment5.target=double\ getAverageByID(java.lang.String,\ int) 13 | comment6.params=minimalRaters 14 | comment6.target=java.util.ArrayList\ getAverageRatings(int) 15 | comment7.params=id 16 | comment7.target=java.lang.String\ getTitle(java.lang.String) 17 | comment8.params=title 18 | comment8.target=java.lang.String\ getID(java.lang.String) 19 | numComments=9 20 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/SecondRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * SecondRatings class contains various methods to extract information from the corresponding 4 | * ArrayLists. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 18, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class SecondRatings { 13 | private ArrayList myMovies; 14 | private ArrayList myRaters; 15 | 16 | public SecondRatings() { 17 | // default constructor 18 | this("ratedmoviesfull.csv", "ratings.csv"); 19 | } 20 | 21 | public SecondRatings (String moviefile, String ratingsfile) { 22 | FirstRatings firstRatings = new FirstRatings (); 23 | myMovies = firstRatings.loadMovies(moviefile); 24 | myRaters = firstRatings.loadRaters(ratingsfile); 25 | } 26 | 27 | public int getMovieSize () { 28 | return myMovies.size(); 29 | } 30 | 31 | public int getRaterSize () { 32 | return myRaters.size(); 33 | } 34 | 35 | private double getAverageByID (String id, int minimalRaters) { 36 | double sum = 0.0; 37 | int count = 0; 38 | 39 | for (Rater rater : myRaters) { 40 | if (rater.hasRating(id)) { 41 | sum += rater.getRating(id); 42 | count += 1; 43 | } 44 | } 45 | 46 | if (count >= minimalRaters) { 47 | return sum / count; 48 | } else { 49 | return 0.0; 50 | } 51 | } 52 | 53 | public ArrayList getAverageRatings (int minimalRaters) { 54 | ArrayList averageRatings = new ArrayList (); 55 | 56 | for (Movie movie : myMovies) { 57 | String movieID = movie.getID(); 58 | double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 59 | if (average != 0.0) { 60 | Rating rating = new Rating (movieID, average); 61 | averageRatings.add(rating); 62 | } 63 | } 64 | 65 | return averageRatings; 66 | } 67 | 68 | public String getTitle (String id) { 69 | String title = null; 70 | 71 | for (Movie movie : myMovies) { 72 | if (movie.getID().equals(id)) { 73 | title = movie.getTitle(); 74 | } 75 | } 76 | 77 | if (title != null) { 78 | return title; 79 | } else { 80 | return "No movie with such ID was found."; 81 | } 82 | } 83 | 84 | public String getID (String title) { 85 | String movieID = null; 86 | 87 | for (Movie movie : myMovies) { 88 | if (movie.getTitle().equals(title)) { 89 | movieID = movie.getID(); 90 | } 91 | } 92 | 93 | if (movieID != null) { 94 | return movieID; 95 | } else { 96 | return "NO SUCH TITLE."; 97 | } 98 | } 99 | 100 | 101 | } 102 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/data/ratedmovies_short.csv: -------------------------------------------------------------------------------- 1 | id,title,year,country,genre,director,minutes,poster 2 | 0006414,"Behind the Screen",1916,"USA","Short, Comedy, Romance","Charles Chaplin",30,"http://ia.media-imdb.com/images/M/MV5BMTkyNDYyNTczNF5BMl5BanBnXkFtZTgwMDU2MzAwMzE@._V1_SX300.jpg" 3 | 0068646,"The Godfather",1972,"USA","Crime, Drama","Francis Ford Coppola",175,"http://ia.media-imdb.com/images/M/MV5BMjEyMjcyNDI4MF5BMl5BanBnXkFtZTcwMDA5Mzg3OA@@._V1_SX300.jpg" 4 | 0113277,"Heat",1995,"USA","Action, Crime, Drama","Michael Mann",170,"http://ia.media-imdb.com/images/M/MV5BMTM1NDc4ODkxNV5BMl5BanBnXkFtZTcwNTI4ODE3MQ@@._V1_SX300.jpg" 5 | 1798709,"Her",2013,"USA","Drama, Romance, Sci-Fi","Spike Jonze",126,"http://ia.media-imdb.com/images/M/MV5BMjA1Nzk0OTM2OF5BMl5BanBnXkFtZTgwNjU2NjEwMDE@._V1_SX300.jpg" 6 | 0790636,"Dallas Buyers Club",2013,"USA","Biography, Drama","Jean-Marc Vallée",117,"N/A" 7 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/data/ratings_short.csv: -------------------------------------------------------------------------------- 1 | rater_id,movie_id,rating,time 2 | 1,0068646,10,1381620027 3 | 1,0113277,10,1379466669 4 | 2,1798709,10,1389948338 5 | 2,0790636,7,1389963947 6 | 2,0068646,9,1382460093 7 | 3,1798709,9,1388641438 8 | 4,0068646,8,1362440416 9 | 4,1798709,6,1398043318 10 | 5,0068646,9,1364834910 11 | 5,1798709,8,1404338202 12 | -------------------------------------------------------------------------------- /Week 3/Assignment 1/package.bluej: -------------------------------------------------------------------------------- 1 | #BlueJ package file 2 | dependency1.from=PlainRater 3 | dependency1.to=Rating 4 | dependency1.type=UsesDependency 5 | dependency10.from=FirstRatings 6 | dependency10.to=Rater 7 | dependency10.type=UsesDependency 8 | dependency11.from=FirstRatings 9 | dependency11.to=EfficientRater 10 | dependency11.type=UsesDependency 11 | dependency2.from=EfficientRater 12 | dependency2.to=Rating 13 | dependency2.type=UsesDependency 14 | dependency3.from=SecondRatings 15 | dependency3.to=Movie 16 | dependency3.type=UsesDependency 17 | dependency4.from=SecondRatings 18 | dependency4.to=Rater 19 | dependency4.type=UsesDependency 20 | dependency5.from=SecondRatings 21 | dependency5.to=FirstRatings 22 | dependency5.type=UsesDependency 23 | dependency6.from=SecondRatings 24 | dependency6.to=Rating 25 | dependency6.type=UsesDependency 26 | dependency7.from=MovieRunnerAverage 27 | dependency7.to=SecondRatings 28 | dependency7.type=UsesDependency 29 | dependency8.from=MovieRunnerAverage 30 | dependency8.to=Rating 31 | dependency8.type=UsesDependency 32 | dependency9.from=FirstRatings 33 | dependency9.to=Movie 34 | dependency9.type=UsesDependency 35 | editor.fx.0.height=1038 36 | editor.fx.0.width=976 37 | editor.fx.0.x=-8 38 | editor.fx.0.y=0 39 | objectbench.height=111 40 | objectbench.width=934 41 | package.divider.horizontal=0.6 42 | package.divider.vertical=0.8715995647442872 43 | package.editor.height=794 44 | package.editor.width=828 45 | package.editor.x=-8 46 | package.editor.y=0 47 | package.frame.height=1038 48 | package.frame.width=976 49 | package.numDependencies=11 50 | package.numTargets=8 51 | package.showExtends=true 52 | package.showUses=true 53 | project.charset=UTF-8 54 | readme.height=58 55 | readme.name=@README 56 | readme.width=47 57 | readme.x=10 58 | readme.y=10 59 | target1.height=50 60 | target1.name=EfficientRater 61 | target1.showInterface=false 62 | target1.type=ClassTarget 63 | target1.typeParameters= 64 | target1.width=110 65 | target1.x=260 66 | target1.y=390 67 | target2.height=50 68 | target2.name=PlainRater 69 | target2.showInterface=false 70 | target2.type=ClassTarget 71 | target2.typeParameters= 72 | target2.width=90 73 | target2.x=190 74 | target2.y=110 75 | target3.height=50 76 | target3.name=Movie 77 | target3.showInterface=false 78 | target3.type=ClassTarget 79 | target3.typeParameters= 80 | target3.width=80 81 | target3.x=550 82 | target3.y=300 83 | target4.height=50 84 | target4.name=FirstRatings 85 | target4.showInterface=false 86 | target4.type=ClassTarget 87 | target4.typeParameters= 88 | target4.width=100 89 | target4.x=440 90 | target4.y=490 91 | target5.height=50 92 | target5.name=SecondRatings 93 | target5.showInterface=false 94 | target5.type=ClassTarget 95 | target5.typeParameters= 96 | target5.width=120 97 | target5.x=670 98 | target5.y=10 99 | target6.height=50 100 | target6.name=Rater 101 | target6.showInterface=false 102 | target6.type=InterfaceTarget 103 | target6.typeParameters= 104 | target6.width=80 105 | target6.x=360 106 | target6.y=210 107 | target7.height=50 108 | target7.name=MovieRunnerAverage 109 | target7.showInterface=false 110 | target7.type=ClassTarget 111 | target7.typeParameters= 112 | target7.width=160 113 | target7.x=10 114 | target7.y=230 115 | target8.height=50 116 | target8.name=Rating 117 | target8.showInterface=false 118 | target8.type=ClassTarget 119 | target8.typeParameters= 120 | target8.width=80 121 | target8.x=120 122 | target8.y=610 123 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/AllFilters.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/AllFilters.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/AllFilters.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=AllFilters 3 | comment1.params= 4 | comment1.target=AllFilters() 5 | comment2.params=f 6 | comment2.target=void\ addFilter(Filter) 7 | comment3.params=id 8 | comment3.target=boolean\ satisfies(java.lang.String) 9 | numComments=4 10 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/AllFilters.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class AllFilters implements Filter { 4 | ArrayList filters; 5 | 6 | public AllFilters() { 7 | filters = new ArrayList(); 8 | } 9 | 10 | public void addFilter(Filter f) { 11 | filters.add(f); 12 | } 13 | 14 | @Override 15 | public boolean satisfies(String id) { 16 | for(Filter f : filters) { 17 | if (! f.satisfies(id)) { 18 | return false; 19 | } 20 | } 21 | 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/DirectorsFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/DirectorsFilter.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/DirectorsFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=DirectorsFilter 3 | comment1.params=directors 4 | comment1.target=DirectorsFilter(java.lang.String) 5 | comment2.params=id 6 | comment2.target=boolean\ satisfies(java.lang.String) 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/DirectorsFilter.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * DirectorsFilter can be used to extract movies directed by either of the specified directors 4 | * in the method parameter. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 20, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class DirectorsFilter implements Filter { 13 | String directorsList; 14 | 15 | public DirectorsFilter (String directors) { 16 | directorsList = directors; 17 | } 18 | 19 | @Override 20 | public boolean satisfies(String id) { 21 | String[] directorsSplit = directorsList.split(","); 22 | for (int i=0; i < directorsSplit.length; i++) { 23 | if (MovieDatabase.getDirector(id).indexOf(directorsSplit[i]) != -1) { 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/EfficientRater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/EfficientRater.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/EfficientRater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=EfficientRater 3 | comment1.params=id 4 | comment1.target=EfficientRater(java.lang.String) 5 | comment2.params=item\ rating 6 | comment2.target=void\ addRating(java.lang.String,\ double) 7 | comment3.params=item 8 | comment3.target=boolean\ hasRating(java.lang.String) 9 | comment4.params= 10 | comment4.target=java.lang.String\ getID() 11 | comment5.params=item 12 | comment5.target=double\ getRating(java.lang.String) 13 | comment6.params= 14 | comment6.target=int\ numRatings() 15 | comment7.params= 16 | comment7.target=java.util.ArrayList\ getItemsRated() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/EfficientRater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * EfficientRater class contains various methods that are used to add and extract ratings 4 | * from the ratings HashMap. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class EfficientRater implements Rater { 13 | private String myID; 14 | private HashMap myRatings; 15 | 16 | public EfficientRater(String id) { 17 | myID = id; 18 | myRatings = new HashMap (); 19 | } 20 | 21 | public void addRating(String item, double rating) { 22 | myRatings.put(item, new Rating(item,rating)); 23 | } 24 | 25 | public boolean hasRating(String item) { 26 | if (myRatings.containsKey(item)) { 27 | return true; 28 | } 29 | 30 | return false; 31 | } 32 | 33 | public String getID() { 34 | return myID; 35 | } 36 | 37 | public double getRating(String item) { 38 | for (String movieID : myRatings.keySet()) { 39 | if (movieID.equals(item)) { 40 | return myRatings.get(movieID).getValue(); 41 | } 42 | } 43 | 44 | return -1; 45 | } 46 | 47 | public int numRatings() { 48 | return myRatings.size(); 49 | } 50 | 51 | public ArrayList getItemsRated() { 52 | ArrayList list = new ArrayList (); 53 | for (String movieID : myRatings.keySet()) { 54 | list.add(myRatings.get(movieID).getItem()); 55 | } 56 | 57 | return list; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/Filter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/Filter.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/Filter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Filter 3 | comment1.params=id 4 | comment1.target=boolean\ satisfies(java.lang.String) 5 | numComments=2 6 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/Filter.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Filter { 3 | public boolean satisfies(String id); 4 | } 5 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/FirstRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/FirstRatings.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/FirstRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=FirstRatings 3 | comment1.params=filename 4 | comment1.target=java.util.ArrayList\ loadMovies(java.lang.String) 5 | comment2.params=filename 6 | comment2.target=java.util.ArrayList\ loadRaters(java.lang.String) 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/FirstRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * loadMovies and loadRaters methods add data from the related csv files into corresponding 4 | * ArrayLists. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 14, 2020) 8 | */ 9 | 10 | import edu.duke.*; 11 | import java.util.*; 12 | import org.apache.commons.csv.*; 13 | 14 | public class FirstRatings { 15 | public ArrayList loadMovies (String filename) { 16 | ArrayList movieData = new ArrayList (); 17 | 18 | FileResource fr = new FileResource("data/" + filename + ".csv"); 19 | CSVParser parser = fr.getCSVParser(); 20 | 21 | for (CSVRecord record: parser) { 22 | String currentID = record.get(0); 23 | String currentTitle = record.get(1); 24 | String currentYear = record.get(2); 25 | String currentCountry = record.get(3); 26 | String currentGenre = record.get(4); 27 | String currentDirector = record.get(5); 28 | int currentMinutes = Integer.parseInt(record.get(6)); 29 | String currentPoster = record.get(7); 30 | 31 | Movie currentMovie = new Movie(currentID, currentTitle, currentYear, currentGenre, currentDirector, 32 | currentCountry, currentPoster, currentMinutes); 33 | 34 | movieData.add(currentMovie); 35 | } 36 | 37 | return movieData; 38 | } 39 | 40 | public ArrayList loadRaters (String filename) { 41 | ArrayList ratersData = new ArrayList (); 42 | ArrayList listOfIDs = new ArrayList (); 43 | 44 | FileResource fr = new FileResource("data/" + filename + ".csv"); 45 | CSVParser parser = fr.getCSVParser(); 46 | 47 | for (CSVRecord record : parser) { 48 | String currentRaterID = record.get(0); 49 | String currentMovieID = record.get(1); 50 | double currentMovieRating = Double.parseDouble(record.get(2)); 51 | 52 | if (! listOfIDs.contains(currentRaterID)) { 53 | //Rater currentRater = new PlainRater(currentRaterID); 54 | Rater currentRater = new EfficientRater(currentRaterID); 55 | ratersData.add(currentRater); 56 | currentRater.addRating(currentMovieID, currentMovieRating); 57 | 58 | } else { 59 | for (int k=0; k < ratersData.size(); k++) { 60 | if (ratersData.get(k).getID().equals(currentRaterID)) { 61 | ratersData.get(k).addRating(currentMovieID, currentMovieRating); 62 | } 63 | } 64 | } 65 | 66 | listOfIDs.add(currentRaterID); 67 | } 68 | 69 | return ratersData; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/GenreFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/GenreFilter.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/GenreFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=GenreFilter 3 | comment0.text=\r\n\ GenreFilter\ can\ be\ used\ to\ extract\ movies\ in\ specified\ genre\ in\ the\ method\ parameter.\r\n\ \r\n\ @\ Konstantin\ Krumin\ \r\n\ @\ Version\:\ 1.0\ (February\ 20,\ 2020)\r\n 4 | comment1.params=genre 5 | comment1.target=GenreFilter(java.lang.String) 6 | comment2.params=id 7 | comment2.target=boolean\ satisfies(java.lang.String) 8 | numComments=3 9 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/GenreFilter.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * GenreFilter can be used to extract movies in specified genre in the method parameter. 4 | * 5 | * @ Konstantin Krumin 6 | * @ Version: 1.0 (February 20, 2020) 7 | */ 8 | 9 | public class GenreFilter implements Filter { 10 | private String myGenre; 11 | 12 | public GenreFilter (String genre) { 13 | myGenre = genre; 14 | } 15 | 16 | @Override 17 | public boolean satisfies(String id) { 18 | return MovieDatabase.getGenres(id).contains(myGenre); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/MinutesFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/MinutesFilter.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/MinutesFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MinutesFilter 3 | comment0.text=\r\n\ MinutesFilter\ can\ be\ used\ to\ extract\ movies\ with\ length\ in\ between\ minimum\ and\ maximum\r\n\ specified\ minutes\ specified\ in\ the\ method\ parameter.\r\n\ \r\n\ @\ Konstantin\ Krumin\r\n\ @\ Version\:\ 1.0\ (February\ 20,\ 2020)\r\n 4 | comment1.params=min\ max 5 | comment1.target=MinutesFilter(int,\ int) 6 | comment2.params=id 7 | comment2.target=boolean\ satisfies(java.lang.String) 8 | numComments=3 9 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/MinutesFilter.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * MinutesFilter can be used to extract movies with length in between minimum and maximum 4 | * specified minutes specified in the method parameter. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 20, 2020) 8 | */ 9 | public class MinutesFilter implements Filter { 10 | private int minMin; 11 | private int maxMin; 12 | 13 | public MinutesFilter (int min, int max) { 14 | minMin = min; 15 | maxMin = max; 16 | } 17 | 18 | @Override 19 | public boolean satisfies(String id) { 20 | return MovieDatabase.getMinutes(id) >= minMin && MovieDatabase.getMinutes(id) <= maxMin; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/Movie.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/Movie.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/Movie.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Movie 3 | comment1.params=anID\ aTitle\ aYear\ theGenres 4 | comment1.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String) 5 | comment10.params= 6 | comment10.target=int\ getMinutes() 7 | comment11.params= 8 | comment11.target=java.lang.String\ toString() 9 | comment2.params=anID\ aTitle\ aYear\ theGenres\ aDirector\ aCountry\ aPoster\ theMinutes 10 | comment2.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ int) 11 | comment3.params= 12 | comment3.target=java.lang.String\ getID() 13 | comment4.params= 14 | comment4.target=java.lang.String\ getTitle() 15 | comment5.params= 16 | comment5.target=int\ getYear() 17 | comment6.params= 18 | comment6.target=java.lang.String\ getGenres() 19 | comment7.params= 20 | comment7.target=java.lang.String\ getCountry() 21 | comment8.params= 22 | comment8.target=java.lang.String\ getDirector() 23 | comment9.params= 24 | comment9.target=java.lang.String\ getPoster() 25 | numComments=12 26 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/Movie.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | 4 | // An immutable passive data object (PDO) to represent item data 5 | public class Movie { 6 | private String id; 7 | private String title; 8 | private int year; 9 | private String genres; 10 | private String director; 11 | private String country; 12 | private String poster; 13 | private int minutes; 14 | 15 | public Movie (String anID, String aTitle, String aYear, String theGenres) { 16 | // just in case data file contains extra whitespace 17 | id = anID.trim(); 18 | title = aTitle.trim(); 19 | year = Integer.parseInt(aYear.trim()); 20 | genres = theGenres; 21 | } 22 | 23 | public Movie (String anID, String aTitle, String aYear, String theGenres, String aDirector, 24 | String aCountry, String aPoster, int theMinutes) { 25 | // just in case data file contains extra whitespace 26 | id = anID.trim(); 27 | title = aTitle.trim(); 28 | year = Integer.parseInt(aYear.trim()); 29 | genres = theGenres; 30 | director = aDirector; 31 | country = aCountry; 32 | poster = aPoster; 33 | minutes = theMinutes; 34 | } 35 | 36 | // Returns ID associated with this item 37 | public String getID () { 38 | return id; 39 | } 40 | 41 | // Returns title of this item 42 | public String getTitle () { 43 | return title; 44 | } 45 | 46 | // Returns year in which this item was published 47 | public int getYear () { 48 | return year; 49 | } 50 | 51 | // Returns genres associated with this item 52 | public String getGenres () { 53 | return genres; 54 | } 55 | 56 | public String getCountry(){ 57 | return country; 58 | } 59 | 60 | public String getDirector(){ 61 | return director; 62 | } 63 | 64 | public String getPoster(){ 65 | return poster; 66 | } 67 | 68 | public int getMinutes(){ 69 | return minutes; 70 | } 71 | 72 | // Returns a string of the item's information 73 | public String toString () { 74 | String result = "Movie [id=" + id + ", title=" + title + ", year=" + year; 75 | result += ", genres= " + genres + "]"; 76 | return result; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/MovieDatabase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/MovieDatabase.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/MovieDatabase.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MovieDatabase 3 | comment1.params=moviefile 4 | comment1.target=void\ initialize(java.lang.String) 5 | comment10.params=id 6 | comment10.target=int\ getMinutes(java.lang.String) 7 | comment11.params=id 8 | comment11.target=java.lang.String\ getCountry(java.lang.String) 9 | comment12.params=id 10 | comment12.target=java.lang.String\ getDirector(java.lang.String) 11 | comment13.params= 12 | comment13.target=int\ size() 13 | comment14.params=f 14 | comment14.target=java.util.ArrayList\ filterBy(Filter) 15 | comment2.params= 16 | comment2.target=void\ initialize() 17 | comment3.params=filename 18 | comment3.target=void\ loadMovies(java.lang.String) 19 | comment4.params=id 20 | comment4.target=boolean\ containsID(java.lang.String) 21 | comment5.params=id 22 | comment5.target=int\ getYear(java.lang.String) 23 | comment6.params=id 24 | comment6.target=java.lang.String\ getGenres(java.lang.String) 25 | comment7.params=id 26 | comment7.target=java.lang.String\ getTitle(java.lang.String) 27 | comment8.params=id 28 | comment8.target=Movie\ getMovie(java.lang.String) 29 | comment9.params=id 30 | comment9.target=java.lang.String\ getPoster(java.lang.String) 31 | numComments=15 32 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/MovieDatabase.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * MovieDatabase class sets a HashMap with data from the corresponding csv table and contains 5 | * several methods to add and extract data from this HashMap. 6 | * 7 | */ 8 | 9 | import java.util.*; 10 | import org.apache.commons.csv.*; 11 | import edu.duke.FileResource; 12 | 13 | public class MovieDatabase { 14 | private static HashMap ourMovies; 15 | 16 | public static void initialize(String moviefile) { 17 | if (ourMovies == null) { 18 | ourMovies = new HashMap(); 19 | loadMovies(moviefile); 20 | } 21 | } 22 | 23 | private static void initialize() { 24 | if (ourMovies == null) { 25 | ourMovies = new HashMap(); 26 | loadMovies("ratedmoviesfull"); 27 | } 28 | } 29 | 30 | private static void loadMovies(String filename) { 31 | FirstRatings fr = new FirstRatings(); 32 | ArrayList list = fr.loadMovies(filename); 33 | for (Movie m : list) { 34 | ourMovies.put(m.getID(), m); 35 | } 36 | } 37 | 38 | public static boolean containsID(String id) { 39 | initialize(); 40 | return ourMovies.containsKey(id); 41 | } 42 | 43 | public static int getYear(String id) { 44 | initialize(); 45 | return ourMovies.get(id).getYear(); 46 | } 47 | 48 | public static String getGenres(String id) { 49 | initialize(); 50 | return ourMovies.get(id).getGenres(); 51 | } 52 | 53 | public static String getTitle(String id) { 54 | initialize(); 55 | return ourMovies.get(id).getTitle(); 56 | } 57 | 58 | public static Movie getMovie(String id) { 59 | initialize(); 60 | return ourMovies.get(id); 61 | } 62 | 63 | public static String getPoster(String id) { 64 | initialize(); 65 | return ourMovies.get(id).getPoster(); 66 | } 67 | 68 | public static int getMinutes(String id) { 69 | initialize(); 70 | return ourMovies.get(id).getMinutes(); 71 | } 72 | 73 | public static String getCountry(String id) { 74 | initialize(); 75 | return ourMovies.get(id).getCountry(); 76 | } 77 | 78 | public static String getDirector(String id) { 79 | initialize(); 80 | return ourMovies.get(id).getDirector(); 81 | } 82 | 83 | public static int size() { 84 | return ourMovies.size(); 85 | } 86 | 87 | public static ArrayList filterBy(Filter f) { 88 | initialize(); 89 | ArrayList list = new ArrayList(); 90 | for(String id : ourMovies.keySet()) { 91 | if (f.satisfies(id)) { 92 | list.add(id); 93 | } 94 | } 95 | 96 | return list; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/MovieRunnerAverage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/MovieRunnerAverage.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/MovieRunnerAverage.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MovieRunnerAverage 3 | comment1.params= 4 | comment1.target=void\ printAverageRatings() 5 | comment2.params= 6 | comment2.target=void\ getAverageRatingOneMovie() 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/MovieRunnerAverage.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * MovieRunnerAverage class contains tests for the getAverageRatings method from the 4 | * secondRatings class. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 18, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class MovieRunnerAverage { 13 | public void printAverageRatings () { 14 | SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings"); 15 | 16 | System.out.println("Total number of movies : " + secondRatings.getMovieSize()); 17 | System.out.println("Total number of raters : " + secondRatings.getRaterSize()); 18 | 19 | int MinNumOfRatings = 20; // variable 20 | ArrayList averageRatings = secondRatings.getAverageRatings(MinNumOfRatings); 21 | Collections.sort(averageRatings); 22 | for (Rating rating : averageRatings) { 23 | System.out.println(rating.getValue() + " " + secondRatings.getTitle(rating.getItem())); 24 | } 25 | System.out.println("There are " + averageRatings.size() + " movies with " + 26 | MinNumOfRatings + " or more ratings"); 27 | } 28 | 29 | public void getAverageRatingOneMovie () { 30 | SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings"); 31 | 32 | String title = "Vacation"; // variable 33 | int MinNumOfRatings = 1; // variable 34 | 35 | String movieID = secondRatings.getID(title); 36 | ArrayList averageRatings = secondRatings.getAverageRatings(MinNumOfRatings); 37 | for (Rating rating : averageRatings) { 38 | if (rating.getItem().equals(movieID)) { 39 | System.out.println("For movie \"" + title + "\" the average rating is " 40 | + rating.getValue()); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/MovieRunnerWithFilters.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/MovieRunnerWithFilters.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/MovieRunnerWithFilters.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MovieRunnerWithFilters 3 | comment1.params= 4 | comment1.target=void\ printAverageRatings() 5 | comment2.params= 6 | comment2.target=void\ printAverageRatingsByYearAfter() 7 | comment3.params= 8 | comment3.target=void\ printAverageRatingsByGenre() 9 | comment4.params= 10 | comment4.target=void\ printAverageRatingsByMinutes() 11 | comment5.params= 12 | comment5.target=void\ printAverageRatingsByDirectors() 13 | comment6.params= 14 | comment6.target=void\ printAverageRatingsByYearAfterAndGenre() 15 | comment7.params= 16 | comment7.target=void\ printAverageRatingsByDirectorsAndMinutes() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/MovieRunnerWithFilters.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * MovieRunnerWithFilters contains tests for ThirdRatings class. 4 | * 5 | * @ Konstantin Krumin 6 | * @ Version: 1.0 (February 19, 2020) 7 | */ 8 | 9 | import java.util.*; 10 | 11 | public class MovieRunnerWithFilters { 12 | public void printAverageRatings () { 13 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 14 | MovieDatabase.initialize("ratedmoviesfull"); 15 | 16 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 17 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 18 | 19 | int minNumOfRatings = 35; // variable 20 | ArrayList averageRatings = thirdRatings.getAverageRatings(minNumOfRatings); 21 | System.out.println("There are " + averageRatings.size() + " movies with " + 22 | minNumOfRatings + " or more rating(s) :"); 23 | 24 | Collections.sort(averageRatings); 25 | for (Rating rating : averageRatings) { 26 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 27 | } 28 | } 29 | 30 | public void printAverageRatingsByYearAfter () { 31 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 32 | MovieDatabase.initialize("ratedmoviesfull"); 33 | 34 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 35 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 36 | 37 | int year = 2000; // variable 38 | YearAfterFilter yaf = new YearAfterFilter (year); 39 | 40 | int minNumOfRatings = 20; // variable 41 | ArrayList averageRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, yaf); 42 | System.out.println("There are " + averageRatings.size() + " movies released after " 43 | + year + " with " + minNumOfRatings + " or more rating(s) : "); 44 | 45 | Collections.sort(averageRatings); 46 | for (Rating rating : averageRatings) { 47 | System.out.println(rating.getValue() + " " + MovieDatabase.getYear(rating.getItem()) 48 | + " " + MovieDatabase.getTitle(rating.getItem())); 49 | } 50 | } 51 | 52 | public void printAverageRatingsByGenre () { 53 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 54 | MovieDatabase.initialize("ratedmoviesfull"); 55 | 56 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 57 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 58 | 59 | String genre = "Comedy"; // variable 60 | GenreFilter gf = new GenreFilter (genre); 61 | 62 | int minNumOfRatings = 20; // variable 63 | ArrayList averageRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, gf); 64 | System.out.println("There are " + averageRatings.size() + " movies in genre of \"" 65 | + genre + "\" with " + minNumOfRatings + " or more rating(s) :"); 66 | 67 | Collections.sort(averageRatings); 68 | for (Rating rating : averageRatings) { 69 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 70 | System.out.println("Genre(s) : " + MovieDatabase.getGenres(rating.getItem())); 71 | } 72 | } 73 | 74 | public void printAverageRatingsByMinutes () { 75 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 76 | MovieDatabase.initialize("ratedmoviesfull"); 77 | 78 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 79 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 80 | 81 | int minMinutes = 105; // variable 82 | int maxMinutes = 135; // variable 83 | MinutesFilter mf = new MinutesFilter (minMinutes, maxMinutes); 84 | 85 | int minNumOfRatings = 5; // variable 86 | ArrayList averageRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, mf); 87 | System.out.println("There are " + averageRatings.size() + " movies that are between " 88 | + minMinutes + " and " + maxMinutes + " length with " + minNumOfRatings + " or more rating(s) :"); 89 | 90 | Collections.sort(averageRatings); 91 | for (Rating rating : averageRatings) { 92 | System.out.println(rating.getValue() + " Time: " + MovieDatabase.getMinutes(rating.getItem()) 93 | + " " + MovieDatabase.getTitle(rating.getItem())); 94 | } 95 | } 96 | 97 | public void printAverageRatingsByDirectors () { 98 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 99 | MovieDatabase.initialize("ratedmoviesfull"); 100 | 101 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 102 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 103 | 104 | String directorsList = "Clint Eastwood,Joel Coen,Martin Scorsese,Roman Polanski,Nora Ephron,Ridley Scott,Sydney Pollack"; // variable 105 | DirectorsFilter df = new DirectorsFilter (directorsList); 106 | 107 | int minNumOfRatings = 4; // variable 108 | ArrayList averageRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, df); 109 | System.out.println("There are " + averageRatings.size() + " movies that were directed " 110 | + "by either of those directors : " + directorsList + ", with " + minNumOfRatings 111 | + " or more rating(s) :"); 112 | 113 | Collections.sort(averageRatings); 114 | for (Rating rating : averageRatings) { 115 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 116 | System.out.println("Directed by : " + MovieDatabase.getDirector(rating.getItem())); 117 | } 118 | } 119 | 120 | public void printAverageRatingsByYearAfterAndGenre () { 121 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 122 | MovieDatabase.initialize("ratedmoviesfull"); 123 | 124 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 125 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 126 | 127 | int year = 1990; // variable 128 | YearAfterFilter yaf = new YearAfterFilter (year); 129 | 130 | String genre = "Drama"; // variable 131 | GenreFilter gf = new GenreFilter (genre); 132 | 133 | AllFilters af = new AllFilters(); 134 | af.addFilter(yaf); 135 | af.addFilter(gf); 136 | 137 | int minNumOfRatings = 8; // variable 138 | ArrayList avgRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, af); 139 | System.out.println("There is(are) " + avgRatings.size() + " movie(s) in genre of \"" 140 | + genre + "\" that was(were) directed after " + year + " with " + minNumOfRatings 141 | + " or more rating(s) :"); 142 | 143 | Collections.sort(avgRatings); 144 | for (Rating rating : avgRatings) { 145 | System.out.println(rating.getValue() + " " + MovieDatabase.getYear(rating.getItem()) 146 | + " " + MovieDatabase.getTitle(rating.getItem())); 147 | System.out.println("Genre : " + MovieDatabase.getGenres(rating.getItem())); 148 | } 149 | } 150 | 151 | public void printAverageRatingsByDirectorsAndMinutes () { 152 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 153 | MovieDatabase.initialize("ratedmoviesfull"); 154 | 155 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 156 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 157 | 158 | String directorsList = "Clint Eastwood,Joel Coen,Tim Burton,Ron Howard,Nora Ephron,Sydney Pollack"; // variable 159 | DirectorsFilter df = new DirectorsFilter (directorsList); 160 | 161 | int minMinutes = 90; // variable 162 | int maxMinutes = 180; // variable 163 | MinutesFilter mf = new MinutesFilter (minMinutes, maxMinutes); 164 | 165 | AllFilters af = new AllFilters(); 166 | af.addFilter(df); 167 | af.addFilter(mf); 168 | 169 | int minNumOfRatings = 3; // variable 170 | ArrayList avgRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, af); 171 | System.out.println("There is(are) " + avgRatings.size() + " movie(s) that were filmed by" 172 | + " either one of these directors : " + directorsList + "; and between " 173 | + minMinutes + " and " + maxMinutes + " in length, with " + minNumOfRatings 174 | + " or more rating(s) :"); 175 | 176 | Collections.sort(avgRatings); 177 | for (Rating rating : avgRatings) { 178 | System.out.println(rating.getValue() + " Time: " + MovieDatabase.getMinutes(rating.getItem()) 179 | + " " + MovieDatabase.getTitle(rating.getItem())); 180 | System.out.println("Directed by : " + MovieDatabase.getDirector(rating.getItem())); 181 | } 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/PlainRater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/PlainRater.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/PlainRater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=PlainRater 3 | comment1.params=id 4 | comment1.target=PlainRater(java.lang.String) 5 | comment2.params=item\ rating 6 | comment2.target=void\ addRating(java.lang.String,\ double) 7 | comment3.params=item 8 | comment3.target=boolean\ hasRating(java.lang.String) 9 | comment4.params= 10 | comment4.target=java.lang.String\ getID() 11 | comment5.params=item 12 | comment5.target=double\ getRating(java.lang.String) 13 | comment6.params= 14 | comment6.target=int\ numRatings() 15 | comment7.params= 16 | comment7.target=java.util.ArrayList\ getItemsRated() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/PlainRater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * PlainRater class contains various methods that are used to add and extract ratings information. 4 | * 5 | */ 6 | 7 | import java.util.*; 8 | 9 | public class PlainRater implements Rater { 10 | private String myID; 11 | private ArrayList myRatings; 12 | 13 | public PlainRater(String id) { 14 | myID = id; 15 | myRatings = new ArrayList(); 16 | } 17 | 18 | public void addRating(String item, double rating) { 19 | myRatings.add(new Rating(item,rating)); 20 | } 21 | 22 | public boolean hasRating(String item) { 23 | for(int k=0; k < myRatings.size(); k++){ 24 | if (myRatings.get(k).getItem().equals(item)){ 25 | return true; 26 | } 27 | } 28 | 29 | return false; 30 | } 31 | 32 | public String getID() { 33 | return myID; 34 | } 35 | 36 | public double getRating(String item) { 37 | for(int k=0; k < myRatings.size(); k++){ 38 | if (myRatings.get(k).getItem().equals(item)){ 39 | return myRatings.get(k).getValue(); 40 | } 41 | } 42 | 43 | return -1; 44 | } 45 | 46 | public int numRatings() { 47 | return myRatings.size(); 48 | } 49 | 50 | public ArrayList getItemsRated() { 51 | ArrayList list = new ArrayList(); 52 | for(int k=0; k < myRatings.size(); k++){ 53 | list.add(myRatings.get(k).getItem()); 54 | } 55 | 56 | return list; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/Rater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/Rater.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/Rater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rater 3 | comment1.params=item\ rating 4 | comment1.target=void\ addRating(java.lang.String,\ double) 5 | comment2.params=item 6 | comment2.target=boolean\ hasRating(java.lang.String) 7 | comment3.params= 8 | comment3.target=java.lang.String\ getID() 9 | comment4.params=item 10 | comment4.target=double\ getRating(java.lang.String) 11 | comment5.params= 12 | comment5.target=int\ numRatings() 13 | comment6.params= 14 | comment6.target=java.util.ArrayList\ getItemsRated() 15 | numComments=7 16 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/Rater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Rater is an interface for PlainRater and EfficientRater classes. 4 | * 5 | * @ Konstantin Krumin 6 | * @ Version: 1.0 (February 19, 2020) 7 | */ 8 | 9 | import java.util.*; 10 | 11 | public interface Rater { 12 | public void addRating(String item, double rating); 13 | 14 | public boolean hasRating(String item); 15 | 16 | public String getID(); 17 | 18 | public double getRating(String item); 19 | 20 | public int numRatings(); 21 | 22 | public ArrayList getItemsRated(); 23 | } 24 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/Rating.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/Rating.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/Rating.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rating 3 | comment1.params=anItem\ aValue 4 | comment1.target=Rating(java.lang.String,\ double) 5 | comment2.params= 6 | comment2.target=java.lang.String\ getItem() 7 | comment3.params= 8 | comment3.target=double\ getValue() 9 | comment4.params= 10 | comment4.target=java.lang.String\ toString() 11 | comment5.params=other 12 | comment5.target=int\ compareTo(Rating) 13 | numComments=6 14 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/Rating.java: -------------------------------------------------------------------------------- 1 | // An immutable passive data object (PDO) to represent the rating data 2 | public class Rating implements Comparable { 3 | private String item; 4 | private double value; 5 | 6 | public Rating (String anItem, double aValue) { 7 | item = anItem; 8 | value = aValue; 9 | } 10 | 11 | // Returns item being rated 12 | public String getItem () { 13 | return item; 14 | } 15 | 16 | // Returns the value of this rating (as a number so it can be used in calculations) 17 | public double getValue () { 18 | return value; 19 | } 20 | 21 | // Returns a string of all the rating information 22 | public String toString () { 23 | return "[" + getItem() + ", " + getValue() + "]"; 24 | } 25 | 26 | public int compareTo(Rating other) { 27 | if (value < other.value) { 28 | return -1; 29 | } 30 | if (value > other.value) { 31 | return 1; 32 | } 33 | return 0; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/SecondRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/SecondRatings.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/SecondRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=SecondRatings 3 | comment1.params= 4 | comment1.target=SecondRatings() 5 | comment2.params=moviefile\ ratingsfile 6 | comment2.target=SecondRatings(java.lang.String,\ java.lang.String) 7 | comment3.params= 8 | comment3.target=int\ getMovieSize() 9 | comment4.params= 10 | comment4.target=int\ getRaterSize() 11 | comment5.params=id\ minimalRaters 12 | comment5.target=double\ getAverageByID(java.lang.String,\ int) 13 | comment6.params=minimalRaters 14 | comment6.target=java.util.ArrayList\ getAverageRatings(int) 15 | comment7.params=id 16 | comment7.target=java.lang.String\ getTitle(java.lang.String) 17 | comment8.params=title 18 | comment8.target=java.lang.String\ getID(java.lang.String) 19 | numComments=9 20 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/SecondRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * SecondRatings class contains various methods to extract information from the corresponding 4 | * ArrayLists. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 18, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class SecondRatings { 13 | private ArrayList myMovies; 14 | private ArrayList myRaters; 15 | 16 | public SecondRatings() { 17 | // default constructor 18 | this("ratedmoviesfull.csv", "ratings.csv"); 19 | } 20 | 21 | public SecondRatings (String moviefile, String ratingsfile) { 22 | FirstRatings firstRatings = new FirstRatings (); 23 | myMovies = firstRatings.loadMovies(moviefile); 24 | myRaters = firstRatings.loadRaters(ratingsfile); 25 | } 26 | 27 | public int getMovieSize () { 28 | return myMovies.size(); 29 | } 30 | 31 | public int getRaterSize () { 32 | return myRaters.size(); 33 | } 34 | 35 | private double getAverageByID (String id, int minimalRaters) { 36 | double sum = 0.0; 37 | int count = 0; 38 | 39 | for (Rater rater : myRaters) { 40 | if (rater.hasRating(id)) { 41 | sum += rater.getRating(id); 42 | count += 1; 43 | } 44 | } 45 | 46 | if (count >= minimalRaters) { 47 | return sum / count; 48 | } else { 49 | return 0.0; 50 | } 51 | } 52 | 53 | public ArrayList getAverageRatings (int minimalRaters) { 54 | ArrayList averageRatings = new ArrayList (); 55 | 56 | for (Movie movie : myMovies) { 57 | String movieID = movie.getID(); 58 | double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 59 | if (average != 0.0) { 60 | Rating rating = new Rating (movieID, average); 61 | averageRatings.add(rating); 62 | } 63 | } 64 | 65 | return averageRatings; 66 | } 67 | 68 | public String getTitle (String id) { 69 | String title = null; 70 | 71 | for (Movie movie : myMovies) { 72 | if (movie.getID().equals(id)) { 73 | title = movie.getTitle(); 74 | } 75 | } 76 | 77 | if (title != null) { 78 | return title; 79 | } else { 80 | return "No movie with such ID was found."; 81 | } 82 | } 83 | 84 | public String getID (String title) { 85 | String movieID = null; 86 | 87 | for (Movie movie : myMovies) { 88 | if (movie.getTitle().equals(title)) { 89 | movieID = movie.getID(); 90 | } 91 | } 92 | 93 | if (movieID != null) { 94 | return movieID; 95 | } else { 96 | return "NO SUCH TITLE."; 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/ThirdRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/ThirdRatings.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/ThirdRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=ThirdRatings 3 | comment1.params= 4 | comment1.target=ThirdRatings() 5 | comment2.params=ratingsfile 6 | comment2.target=ThirdRatings(java.lang.String) 7 | comment3.params= 8 | comment3.target=int\ getRaterSize() 9 | comment4.params=id\ minimalRaters 10 | comment4.target=double\ getAverageByID(java.lang.String,\ int) 11 | comment5.params=minimalRaters 12 | comment5.target=java.util.ArrayList\ getAverageRatings(int) 13 | comment6.params=minimalRaters\ filterCriteria 14 | comment6.target=java.util.ArrayList\ getAverageRatingsByFilter(int,\ Filter) 15 | numComments=7 16 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/ThirdRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * ThirdRatings class contains 2 methods that can be used to get average ratings 4 | * (with/without filters) as well as additional helper methods. 5 | * 6 | * * getAverageRatings and getAverageRatingsByFilter are used to find movies that have required 7 | * * number of ratings, and in case with getAverageRatingsByFilter can be filtered using a range 8 | * * of filters provided in this program. 9 | * 10 | * @ Konstantin Krumin 11 | * @ Version: 1.0 (February 19, 2020) 12 | */ 13 | 14 | import java.util.*; 15 | 16 | public class ThirdRatings { 17 | private ArrayList myRaters; 18 | 19 | public ThirdRatings () { 20 | // default constructor 21 | this("ratings.csv"); 22 | } 23 | 24 | public ThirdRatings (String ratingsfile) { 25 | FirstRatings firstRatings = new FirstRatings (); 26 | myRaters = firstRatings.loadRaters(ratingsfile); 27 | } 28 | 29 | public int getRaterSize () { 30 | return myRaters.size(); 31 | } 32 | 33 | private double getAverageByID (String id, int minimalRaters) { 34 | double sum = 0.0; 35 | int count = 0; 36 | 37 | for (Rater rater : myRaters) { 38 | if (rater.hasRating(id)) { 39 | sum += rater.getRating(id); 40 | count += 1; 41 | } 42 | } 43 | 44 | if (count >= minimalRaters) { 45 | return sum / count; 46 | } else { 47 | return 0.0; 48 | } 49 | } 50 | 51 | public ArrayList getAverageRatings (int minimalRaters) { 52 | ArrayList movies = MovieDatabase.filterBy(new TrueFilter()); 53 | ArrayList averageRatings = new ArrayList (); 54 | 55 | for (String movieID : movies) { 56 | double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 57 | if (average != 0.0) { 58 | Rating rating = new Rating (movieID, average); 59 | averageRatings.add(rating); 60 | } 61 | } 62 | 63 | return averageRatings; 64 | } 65 | 66 | public ArrayList getAverageRatingsByFilter (int minimalRaters, Filter filterCriteria) { 67 | ArrayList averageRatings = new ArrayList (); 68 | ArrayList filteredMovies = MovieDatabase.filterBy(filterCriteria); 69 | 70 | for (String movieID : filteredMovies) { 71 | double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 72 | if (average != 0.0) { 73 | Rating rating = new Rating (movieID, average); 74 | averageRatings.add(rating); 75 | } 76 | } 77 | 78 | return averageRatings; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/TrueFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/TrueFilter.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/TrueFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=TrueFilter 3 | comment1.params=id 4 | comment1.target=boolean\ satisfies(java.lang.String) 5 | numComments=2 6 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/TrueFilter.java: -------------------------------------------------------------------------------- 1 | 2 | public class TrueFilter implements Filter { 3 | @Override 4 | public boolean satisfies(String id) { 5 | return true; 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/YearAfterFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 3/Assignment 2/YearAfterFilter.class -------------------------------------------------------------------------------- /Week 3/Assignment 2/YearAfterFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=YearAfterFilter 3 | comment0.text=\r\n\ \r\n\ YearAfterFilter\ can\ be\ used\ to\ extract\ only\ movies\ released\ after\ a\ particular\ year\ specified\r\n\ in\ the\ method\ parameter.\r\n\ \r\n 4 | comment1.params=year 5 | comment1.target=YearAfterFilter(int) 6 | comment2.params=id 7 | comment2.target=boolean\ satisfies(java.lang.String) 8 | numComments=3 9 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/YearAfterFilter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * YearAfterFilter can be used to extract only movies released after a particular year specified 4 | * in the method parameter. 5 | * 6 | */ 7 | 8 | public class YearAfterFilter implements Filter { 9 | private int myYear; 10 | 11 | public YearAfterFilter(int year) { 12 | myYear = year; 13 | } 14 | 15 | @Override 16 | public boolean satisfies(String id) { 17 | return MovieDatabase.getYear(id) >= myYear; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/data/ratedmovies_short.csv: -------------------------------------------------------------------------------- 1 | id,title,year,country,genre,director,minutes,poster 2 | 0006414,"Behind the Screen",1916,"USA","Short, Comedy, Romance","Charles Chaplin",30,"http://ia.media-imdb.com/images/M/MV5BMTkyNDYyNTczNF5BMl5BanBnXkFtZTgwMDU2MzAwMzE@._V1_SX300.jpg" 3 | 0068646,"The Godfather",1972,"USA","Crime, Drama","Francis Ford Coppola",175,"http://ia.media-imdb.com/images/M/MV5BMjEyMjcyNDI4MF5BMl5BanBnXkFtZTcwMDA5Mzg3OA@@._V1_SX300.jpg" 4 | 0113277,"Heat",1995,"USA","Action, Crime, Drama","Michael Mann",170,"http://ia.media-imdb.com/images/M/MV5BMTM1NDc4ODkxNV5BMl5BanBnXkFtZTcwNTI4ODE3MQ@@._V1_SX300.jpg" 5 | 1798709,"Her",2013,"USA","Drama, Romance, Sci-Fi","Spike Jonze",126,"http://ia.media-imdb.com/images/M/MV5BMjA1Nzk0OTM2OF5BMl5BanBnXkFtZTgwNjU2NjEwMDE@._V1_SX300.jpg" 6 | 0790636,"Dallas Buyers Club",2013,"USA","Biography, Drama","Jean-Marc Vallée",117,"N/A" 7 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/data/ratings_short.csv: -------------------------------------------------------------------------------- 1 | rater_id,movie_id,rating,time 2 | 1,0068646,10,1381620027 3 | 1,0113277,10,1379466669 4 | 2,1798709,10,1389948338 5 | 2,0790636,7,1389963947 6 | 2,0068646,9,1382460093 7 | 3,1798709,9,1388641438 8 | 4,0068646,8,1362440416 9 | 4,1798709,6,1398043318 10 | 5,0068646,9,1364834910 11 | 5,1798709,8,1404338202 12 | -------------------------------------------------------------------------------- /Week 3/Assignment 2/package.bluej: -------------------------------------------------------------------------------- 1 | #BlueJ package file 2 | dependency1.from=AllFilters 3 | dependency1.to=Filter 4 | dependency1.type=UsesDependency 5 | dependency10.from=EfficientRater 6 | dependency10.to=Rating 7 | dependency10.type=UsesDependency 8 | dependency11.from=PlainRater 9 | dependency11.to=Rating 10 | dependency11.type=UsesDependency 11 | dependency12.from=GenreFilter 12 | dependency12.to=MovieDatabase 13 | dependency12.type=UsesDependency 14 | dependency13.from=YearAfterFilter 15 | dependency13.to=MovieDatabase 16 | dependency13.type=UsesDependency 17 | dependency14.from=FirstRatings 18 | dependency14.to=Movie 19 | dependency14.type=UsesDependency 20 | dependency15.from=FirstRatings 21 | dependency15.to=Rater 22 | dependency15.type=UsesDependency 23 | dependency16.from=FirstRatings 24 | dependency16.to=EfficientRater 25 | dependency16.type=UsesDependency 26 | dependency17.from=MovieRunnerAverage 27 | dependency17.to=SecondRatings 28 | dependency17.type=UsesDependency 29 | dependency18.from=MovieRunnerAverage 30 | dependency18.to=Rating 31 | dependency18.type=UsesDependency 32 | dependency19.from=SecondRatings 33 | dependency19.to=Movie 34 | dependency19.type=UsesDependency 35 | dependency2.from=DirectorsFilter 36 | dependency2.to=MovieDatabase 37 | dependency2.type=UsesDependency 38 | dependency20.from=SecondRatings 39 | dependency20.to=Rater 40 | dependency20.type=UsesDependency 41 | dependency21.from=SecondRatings 42 | dependency21.to=FirstRatings 43 | dependency21.type=UsesDependency 44 | dependency22.from=SecondRatings 45 | dependency22.to=Rating 46 | dependency22.type=UsesDependency 47 | dependency23.from=MovieDatabase 48 | dependency23.to=Movie 49 | dependency23.type=UsesDependency 50 | dependency24.from=MovieDatabase 51 | dependency24.to=FirstRatings 52 | dependency24.type=UsesDependency 53 | dependency25.from=MovieDatabase 54 | dependency25.to=Filter 55 | dependency25.type=UsesDependency 56 | dependency26.from=MovieRunnerWithFilters 57 | dependency26.to=ThirdRatings 58 | dependency26.type=UsesDependency 59 | dependency27.from=MovieRunnerWithFilters 60 | dependency27.to=Rating 61 | dependency27.type=UsesDependency 62 | dependency28.from=MovieRunnerWithFilters 63 | dependency28.to=YearAfterFilter 64 | dependency28.type=UsesDependency 65 | dependency29.from=MovieRunnerWithFilters 66 | dependency29.to=GenreFilter 67 | dependency29.type=UsesDependency 68 | dependency3.from=MinutesFilter 69 | dependency3.to=MovieDatabase 70 | dependency3.type=UsesDependency 71 | dependency30.from=MovieRunnerWithFilters 72 | dependency30.to=MinutesFilter 73 | dependency30.type=UsesDependency 74 | dependency31.from=MovieRunnerWithFilters 75 | dependency31.to=DirectorsFilter 76 | dependency31.type=UsesDependency 77 | dependency32.from=MovieRunnerWithFilters 78 | dependency32.to=AllFilters 79 | dependency32.type=UsesDependency 80 | dependency33.from=MovieRunnerWithFilters 81 | dependency33.to=MovieDatabase 82 | dependency33.type=UsesDependency 83 | dependency4.from=ThirdRatings 84 | dependency4.to=Rater 85 | dependency4.type=UsesDependency 86 | dependency5.from=ThirdRatings 87 | dependency5.to=FirstRatings 88 | dependency5.type=UsesDependency 89 | dependency6.from=ThirdRatings 90 | dependency6.to=Rating 91 | dependency6.type=UsesDependency 92 | dependency7.from=ThirdRatings 93 | dependency7.to=TrueFilter 94 | dependency7.type=UsesDependency 95 | dependency8.from=ThirdRatings 96 | dependency8.to=Filter 97 | dependency8.type=UsesDependency 98 | dependency9.from=ThirdRatings 99 | dependency9.to=MovieDatabase 100 | dependency9.type=UsesDependency 101 | editor.fx.0.height=1038 102 | editor.fx.0.width=976 103 | editor.fx.0.x=-8 104 | editor.fx.0.y=0 105 | objectbench.height=91 106 | objectbench.width=934 107 | package.divider.horizontal=0.6 108 | package.divider.vertical=0.8933623503808488 109 | package.editor.height=814 110 | package.editor.width=828 111 | package.editor.x=-8 112 | package.editor.y=0 113 | package.frame.height=1038 114 | package.frame.width=976 115 | package.numDependencies=33 116 | package.numTargets=18 117 | package.showExtends=true 118 | package.showUses=true 119 | project.charset=UTF-8 120 | readme.height=58 121 | readme.name=@README 122 | readme.width=47 123 | readme.x=10 124 | readme.y=10 125 | target1.height=50 126 | target1.name=EfficientRater 127 | target1.showInterface=false 128 | target1.type=ClassTarget 129 | target1.typeParameters= 130 | target1.width=110 131 | target1.x=140 132 | target1.y=260 133 | target10.height=50 134 | target10.name=MovieRunnerWithFilters 135 | target10.showInterface=false 136 | target10.type=ClassTarget 137 | target10.typeParameters= 138 | target10.width=180 139 | target10.x=430 140 | target10.y=750 141 | target11.height=50 142 | target11.name=SecondRatings 143 | target11.showInterface=false 144 | target11.type=ClassTarget 145 | target11.typeParameters= 146 | target11.width=120 147 | target11.x=90 148 | target11.y=730 149 | target12.height=50 150 | target12.name=Filter 151 | target12.showInterface=false 152 | target12.type=InterfaceTarget 153 | target12.typeParameters= 154 | target12.width=80 155 | target12.x=710 156 | target12.y=200 157 | target13.height=50 158 | target13.name=ThirdRatings 159 | target13.showInterface=false 160 | target13.type=ClassTarget 161 | target13.typeParameters= 162 | target13.width=100 163 | target13.x=20 164 | target13.y=150 165 | target14.height=50 166 | target14.name=AllFilters 167 | target14.showInterface=false 168 | target14.type=ClassTarget 169 | target14.typeParameters= 170 | target14.width=80 171 | target14.x=570 172 | target14.y=90 173 | target15.height=50 174 | target15.name=MovieDatabase 175 | target15.showInterface=false 176 | target15.type=ClassTarget 177 | target15.typeParameters= 178 | target15.width=120 179 | target15.x=230 180 | target15.y=680 181 | target16.height=50 182 | target16.name=MinutesFilter 183 | target16.showInterface=false 184 | target16.type=ClassTarget 185 | target16.typeParameters= 186 | target16.width=110 187 | target16.x=600 188 | target16.y=20 189 | target17.height=50 190 | target17.name=TrueFilter 191 | target17.showInterface=false 192 | target17.type=ClassTarget 193 | target17.typeParameters= 194 | target17.width=80 195 | target17.x=380 196 | target17.y=100 197 | target18.height=50 198 | target18.name=YearAfterFilter 199 | target18.showInterface=false 200 | target18.type=ClassTarget 201 | target18.typeParameters= 202 | target18.width=120 203 | target18.x=570 204 | target18.y=390 205 | target2.height=50 206 | target2.name=Movie 207 | target2.showInterface=false 208 | target2.type=ClassTarget 209 | target2.typeParameters= 210 | target2.width=80 211 | target2.x=10 212 | target2.y=430 213 | target3.height=50 214 | target3.name=Rater 215 | target3.showInterface=false 216 | target3.type=InterfaceTarget 217 | target3.typeParameters= 218 | target3.width=80 219 | target3.x=240 220 | target3.y=380 221 | target4.height=50 222 | target4.name=MovieRunnerAverage 223 | target4.showInterface=false 224 | target4.type=ClassTarget 225 | target4.typeParameters= 226 | target4.width=160 227 | target4.x=470 228 | target4.y=540 229 | target5.height=50 230 | target5.name=Rating 231 | target5.showInterface=false 232 | target5.type=ClassTarget 233 | target5.typeParameters= 234 | target5.width=80 235 | target5.x=710 236 | target5.y=610 237 | target6.height=50 238 | target6.name=DirectorsFilter 239 | target6.showInterface=false 240 | target6.type=ClassTarget 241 | target6.typeParameters= 242 | target6.width=110 243 | target6.x=370 244 | target6.y=20 245 | target7.height=50 246 | target7.name=GenreFilter 247 | target7.showInterface=false 248 | target7.type=ClassTarget 249 | target7.typeParameters= 250 | target7.width=90 251 | target7.x=560 252 | target7.y=310 253 | target8.height=50 254 | target8.name=PlainRater 255 | target8.showInterface=false 256 | target8.type=ClassTarget 257 | target8.typeParameters= 258 | target8.width=90 259 | target8.x=410 260 | target8.y=340 261 | target9.height=50 262 | target9.name=FirstRatings 263 | target9.showInterface=false 264 | target9.type=ClassTarget 265 | target9.typeParameters= 266 | target9.width=100 267 | target9.x=420 268 | target9.y=470 269 | -------------------------------------------------------------------------------- /Week 4/AllFilters.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/AllFilters.class -------------------------------------------------------------------------------- /Week 4/AllFilters.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=AllFilters 3 | comment1.params= 4 | comment1.target=AllFilters() 5 | comment2.params=f 6 | comment2.target=void\ addFilter(Filter) 7 | comment3.params=id 8 | comment3.target=boolean\ satisfies(java.lang.String) 9 | numComments=4 10 | -------------------------------------------------------------------------------- /Week 4/AllFilters.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class AllFilters implements Filter { 4 | ArrayList filters; 5 | 6 | public AllFilters() { 7 | filters = new ArrayList(); 8 | } 9 | 10 | public void addFilter(Filter f) { 11 | filters.add(f); 12 | } 13 | 14 | @Override 15 | public boolean satisfies(String id) { 16 | for(Filter f : filters) { 17 | if (! f.satisfies(id)) { 18 | return false; 19 | } 20 | } 21 | 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Week 4/DirectorsFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/DirectorsFilter.class -------------------------------------------------------------------------------- /Week 4/DirectorsFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=DirectorsFilter 3 | comment1.params=directors 4 | comment1.target=DirectorsFilter(java.lang.String) 5 | comment2.params=id 6 | comment2.target=boolean\ satisfies(java.lang.String) 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 4/DirectorsFilter.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * DirectorsFilter can be used to extract movies directed by either of the specified directors 4 | * in the method parameter. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 20, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class DirectorsFilter implements Filter { 13 | String directorsList; 14 | 15 | public DirectorsFilter (String directors) { 16 | directorsList = directors; 17 | } 18 | 19 | @Override 20 | public boolean satisfies(String id) { 21 | String[] directorsSplit = directorsList.split(","); 22 | for (int i=0; i < directorsSplit.length; i++) { 23 | if (MovieDatabase.getDirector(id).indexOf(directorsSplit[i]) != -1) { 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Week 4/EfficientRater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/EfficientRater.class -------------------------------------------------------------------------------- /Week 4/EfficientRater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=EfficientRater 3 | comment1.params=id 4 | comment1.target=EfficientRater(java.lang.String) 5 | comment2.params=item\ rating 6 | comment2.target=void\ addRating(java.lang.String,\ double) 7 | comment3.params=item 8 | comment3.target=boolean\ hasRating(java.lang.String) 9 | comment4.params= 10 | comment4.target=java.lang.String\ getID() 11 | comment5.params=item 12 | comment5.target=double\ getRating(java.lang.String) 13 | comment6.params= 14 | comment6.target=int\ numRatings() 15 | comment7.params= 16 | comment7.target=java.util.ArrayList\ getItemsRated() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 4/EfficientRater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * EfficientRater class contains various methods that are used to add and extract ratings 4 | * from the ratings HashMap. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class EfficientRater implements Rater { 13 | private String myID; 14 | private HashMap myRatings; 15 | 16 | public EfficientRater(String id) { 17 | myID = id; 18 | myRatings = new HashMap (); 19 | } 20 | 21 | public void addRating(String item, double rating) { 22 | myRatings.put(item, new Rating(item,rating)); 23 | } 24 | 25 | public boolean hasRating(String item) { 26 | if (myRatings.containsKey(item)) { 27 | return true; 28 | } 29 | 30 | return false; 31 | } 32 | 33 | public String getID() { 34 | return myID; 35 | } 36 | 37 | public double getRating(String item) { 38 | for (String movieID : myRatings.keySet()) { 39 | if (movieID.equals(item)) { 40 | return myRatings.get(movieID).getValue(); 41 | } 42 | } 43 | 44 | return -1; 45 | } 46 | 47 | public int numRatings() { 48 | return myRatings.size(); 49 | } 50 | 51 | public ArrayList getItemsRated() { 52 | ArrayList list = new ArrayList (); 53 | for (String movieID : myRatings.keySet()) { 54 | list.add(myRatings.get(movieID).getItem()); 55 | } 56 | 57 | return list; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Week 4/Filter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/Filter.class -------------------------------------------------------------------------------- /Week 4/Filter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Filter 3 | comment1.params=id 4 | comment1.target=boolean\ satisfies(java.lang.String) 5 | numComments=2 6 | -------------------------------------------------------------------------------- /Week 4/Filter.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Filter { 3 | public boolean satisfies(String id); 4 | } 5 | -------------------------------------------------------------------------------- /Week 4/FirstRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/FirstRatings.class -------------------------------------------------------------------------------- /Week 4/FirstRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=FirstRatings 3 | comment1.params=filename 4 | comment1.target=java.util.ArrayList\ loadMovies(java.lang.String) 5 | comment2.params=filename 6 | comment2.target=java.util.ArrayList\ loadRaters(java.lang.String) 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 4/FirstRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * loadMovies and loadRaters methods add data from the related csv files into corresponding 4 | * ArrayLists. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 14, 2020) 8 | */ 9 | 10 | import edu.duke.*; 11 | import java.util.*; 12 | import org.apache.commons.csv.*; 13 | 14 | public class FirstRatings { 15 | public ArrayList loadMovies (String filename) { 16 | ArrayList movieData = new ArrayList (); 17 | 18 | FileResource fr = new FileResource("data/" + filename + ".csv"); 19 | CSVParser parser = fr.getCSVParser(); 20 | 21 | for (CSVRecord record: parser) { 22 | String currentID = record.get(0); 23 | String currentTitle = record.get(1); 24 | String currentYear = record.get(2); 25 | String currentCountry = record.get(3); 26 | String currentGenre = record.get(4); 27 | String currentDirector = record.get(5); 28 | int currentMinutes = Integer.parseInt(record.get(6)); 29 | String currentPoster = record.get(7); 30 | 31 | Movie currentMovie = new Movie(currentID, currentTitle, currentYear, currentGenre, currentDirector, 32 | currentCountry, currentPoster, currentMinutes); 33 | 34 | movieData.add(currentMovie); 35 | } 36 | 37 | return movieData; 38 | } 39 | 40 | public ArrayList loadRaters (String filename) { 41 | ArrayList ratersData = new ArrayList (); 42 | ArrayList listOfIDs = new ArrayList (); 43 | 44 | FileResource fr = new FileResource("data/" + filename + ".csv"); 45 | CSVParser parser = fr.getCSVParser(); 46 | 47 | for (CSVRecord record : parser) { 48 | String currentRaterID = record.get(0); 49 | String currentMovieID = record.get(1); 50 | double currentMovieRating = Double.parseDouble(record.get(2)); 51 | 52 | if (! listOfIDs.contains(currentRaterID)) { 53 | //Rater currentRater = new PlainRater(currentRaterID); 54 | Rater currentRater = new EfficientRater(currentRaterID); 55 | ratersData.add(currentRater); 56 | currentRater.addRating(currentMovieID, currentMovieRating); 57 | 58 | } else { 59 | for (int k=0; k < ratersData.size(); k++) { 60 | if (ratersData.get(k).getID().equals(currentRaterID)) { 61 | ratersData.get(k).addRating(currentMovieID, currentMovieRating); 62 | } 63 | } 64 | } 65 | 66 | listOfIDs.add(currentRaterID); 67 | } 68 | 69 | return ratersData; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Week 4/FourthRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/FourthRatings.class -------------------------------------------------------------------------------- /Week 4/FourthRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=FourthRatings 3 | comment1.params= 4 | comment1.target=FourthRatings() 5 | comment2.params=ratingsfile 6 | comment2.target=FourthRatings(java.lang.String) 7 | comment3.params=id\ minimalRaters 8 | comment3.target=double\ getAverageByID(java.lang.String,\ int) 9 | comment4.params=minimalRaters 10 | comment4.target=java.util.ArrayList\ getAverageRatings(int) 11 | comment5.params=minimalRaters\ filterCriteria 12 | comment5.target=java.util.ArrayList\ getAverageRatingsByFilter(int,\ Filter) 13 | comment6.params=me\ r 14 | comment6.target=double\ dotProduct(Rater,\ Rater) 15 | comment7.params=id 16 | comment7.target=java.util.ArrayList\ getSimilarities(java.lang.String) 17 | comment8.params=id\ numSimilarRaters\ minimalRaters 18 | comment8.target=java.util.ArrayList\ getSimilarRatings(java.lang.String,\ int,\ int) 19 | comment9.params=id\ numSimilarRaters\ minimalRaters\ filterCriteria 20 | comment9.target=java.util.ArrayList\ getSimilarRatingsByFilter(java.lang.String,\ int,\ int,\ Filter) 21 | numComments=10 22 | -------------------------------------------------------------------------------- /Week 4/FourthRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * FourthRatings class contains various methods that can be used to get average ratings 4 | * (with/without filters) as well as additional helper methods: 5 | * 6 | * * getAverageRatings and getAverageRatingsByFilter are used to find movies that have required 7 | * * number of ratings, and in case with getAverageRatingsByFilter can be filtered using a range 8 | * * of filters provided in this program. 9 | * 10 | * * getSimilarRatings and getSimilarRatingsByFilter work similarly to the methods mentioned 11 | * * above but additionally ratings are provided using weighted averages. A user will get more 12 | * * personalized recommendations because movies that were highly rated by only similar raters 13 | * * will be returned. numSimilarRaters method parameter can be changed in order to modify 14 | * * the precision of the provided recommendations. 15 | * 16 | * @ Konstantin Krumin 17 | * @ Version: 1.0 (February 20, 2020) 18 | */ 19 | 20 | import java.util.*; 21 | 22 | public class FourthRatings { 23 | public FourthRatings () { 24 | // default constructor 25 | this("ratings.csv"); 26 | } 27 | 28 | public FourthRatings (String ratingsfile) { 29 | RaterDatabase.initialize(ratingsfile); 30 | } 31 | 32 | private double getAverageByID (String id, int minimalRaters) { 33 | double sum = 0.0; 34 | int count = 0; 35 | 36 | for (Rater rater : RaterDatabase.getRaters()) { 37 | if (rater.hasRating(id)) { 38 | sum += rater.getRating(id); 39 | count += 1; 40 | } 41 | } 42 | 43 | if (count >= minimalRaters) { 44 | return sum / count; 45 | } else { 46 | return 0.0; 47 | } 48 | } 49 | 50 | public ArrayList getAverageRatings (int minimalRaters) { 51 | ArrayList averageRatings = new ArrayList (); 52 | ArrayList movies = MovieDatabase.filterBy(new TrueFilter()); 53 | 54 | for (String movieID : movies) { 55 | double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 56 | if (average != 0.0) { 57 | Rating rating = new Rating (movieID, average); 58 | averageRatings.add(rating); 59 | } 60 | } 61 | 62 | return averageRatings; 63 | } 64 | 65 | public ArrayList getAverageRatingsByFilter (int minimalRaters, Filter filterCriteria) { 66 | ArrayList averageRatings = new ArrayList (); 67 | ArrayList filteredMovies = MovieDatabase.filterBy(filterCriteria); 68 | 69 | for (String movieID : filteredMovies) { 70 | double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 71 | if (average != 0.0) { 72 | Rating rating = new Rating (movieID, average); 73 | averageRatings.add(rating); 74 | } 75 | } 76 | 77 | return averageRatings; 78 | } 79 | 80 | private double dotProduct (Rater me, Rater r) { 81 | double dotProduct = 0.0; 82 | ArrayList itemsRatedMe = me.getItemsRated(); 83 | 84 | for (String item : itemsRatedMe) { 85 | if (r.getItemsRated().contains(item)) { 86 | double currRatingR = r.getRating(item); 87 | double currRatingMe = me.getRating(item); 88 | 89 | dotProduct += (currRatingR - 5.0) * (currRatingMe - 5.0); 90 | } 91 | } 92 | return dotProduct; 93 | } 94 | 95 | private ArrayList getSimilarities (String id) { 96 | ArrayList similarities = new ArrayList (); 97 | Rater me = RaterDatabase.getRater(id); 98 | 99 | for (Rater currRater : RaterDatabase.getRaters()) { 100 | if (! currRater.getID().equals(id)) { 101 | double dotProduct = dotProduct(me, currRater); 102 | if (dotProduct >= 0) { 103 | similarities.add(new Rating(currRater.getID(), dotProduct)); 104 | } 105 | } 106 | } 107 | 108 | Collections.sort(similarities, Collections.reverseOrder()); 109 | return similarities; 110 | } 111 | 112 | public ArrayList getSimilarRatings (String id, int numSimilarRaters, int minimalRaters) { 113 | ArrayList weightedRatings = new ArrayList (); 114 | ArrayList similarRaters = getSimilarities(id); 115 | ArrayList movies = MovieDatabase.filterBy(new TrueFilter()); 116 | 117 | HashMap accumulatedRating = new HashMap (); 118 | HashMap accumulatedCount = new HashMap (); 119 | 120 | for (String movieID : movies) { 121 | double currRating = 0.0; 122 | int currCount = 0; 123 | 124 | for (int k=0; k < numSimilarRaters; k++) { 125 | Rating r = similarRaters.get(k); 126 | String raterID = r.getItem(); 127 | double weight = r.getValue(); 128 | 129 | Rater rater = RaterDatabase.getRater(raterID); 130 | 131 | if (rater.hasRating(movieID)) { 132 | double rating = rater.getRating(movieID) * weight; 133 | currRating += rating; 134 | currCount += 1; 135 | } 136 | } 137 | 138 | if (currCount >= minimalRaters) { 139 | accumulatedRating.put(movieID, currRating); 140 | accumulatedCount.put(movieID, currCount); 141 | } 142 | } 143 | 144 | for (String movieID : accumulatedRating.keySet()) { 145 | double weightedRating = Math.round((accumulatedRating.get(movieID) / accumulatedCount.get(movieID)) * 100.0) / 100.0; 146 | Rating rating = new Rating (movieID, weightedRating); 147 | weightedRatings.add(rating); 148 | } 149 | 150 | Collections.sort(weightedRatings, Collections.reverseOrder()); 151 | return weightedRatings; 152 | } 153 | 154 | public ArrayList getSimilarRatingsByFilter 155 | (String id, int numSimilarRaters, int minimalRaters, Filter filterCriteria) { 156 | ArrayList weightedRatings = new ArrayList (); 157 | ArrayList similarRaters = getSimilarities(id); 158 | ArrayList filteredMovies = MovieDatabase.filterBy(filterCriteria); 159 | 160 | HashMap accumulatedRating = new HashMap (); 161 | HashMap accumulatedCount = new HashMap (); 162 | 163 | for (String movieID : filteredMovies) { 164 | double currRating = 0.0; 165 | int currCount = 0; 166 | 167 | for (int k=0; k < numSimilarRaters; k++) { 168 | Rating r = similarRaters.get(k); 169 | String raterID = r.getItem(); 170 | double weight = r.getValue(); 171 | 172 | Rater rater = RaterDatabase.getRater(raterID); 173 | 174 | if (rater.hasRating(movieID)) { 175 | double rating = rater.getRating(movieID) * weight; 176 | currRating += rating; 177 | currCount += 1; 178 | } 179 | } 180 | 181 | if (currCount >= minimalRaters) { 182 | accumulatedRating.put(movieID, currRating); 183 | accumulatedCount.put(movieID, currCount); 184 | } 185 | } 186 | 187 | for (String movieID : accumulatedRating.keySet()) { 188 | double weightedRating = Math.round((accumulatedRating.get(movieID) / accumulatedCount.get(movieID)) * 100.0) / 100.0; 189 | Rating rating = new Rating (movieID, weightedRating); 190 | weightedRatings.add(rating); 191 | } 192 | 193 | Collections.sort(weightedRatings, Collections.reverseOrder()); 194 | return weightedRatings; 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /Week 4/GenreFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/GenreFilter.class -------------------------------------------------------------------------------- /Week 4/GenreFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=GenreFilter 3 | comment0.text=\r\n\ GenreFilter\ can\ be\ used\ to\ extract\ movies\ in\ specified\ genre\ in\ the\ method\ parameter.\r\n\ \r\n\ @\ Konstantin\ Krumin\ \r\n\ @\ Version\:\ 1.0\ (February\ 20,\ 2020)\r\n 4 | comment1.params=genre 5 | comment1.target=GenreFilter(java.lang.String) 6 | comment2.params=id 7 | comment2.target=boolean\ satisfies(java.lang.String) 8 | numComments=3 9 | -------------------------------------------------------------------------------- /Week 4/GenreFilter.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * GenreFilter can be used to extract movies in specified genre in the method parameter. 4 | * 5 | * @ Konstantin Krumin 6 | * @ Version: 1.0 (February 20, 2020) 7 | */ 8 | 9 | public class GenreFilter implements Filter { 10 | private String myGenre; 11 | 12 | public GenreFilter (String genre) { 13 | myGenre = genre; 14 | } 15 | 16 | @Override 17 | public boolean satisfies(String id) { 18 | return MovieDatabase.getGenres(id).contains(myGenre); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Week 4/MinutesFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/MinutesFilter.class -------------------------------------------------------------------------------- /Week 4/MinutesFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MinutesFilter 3 | comment0.text=\r\n\ MinutesFilter\ can\ be\ used\ to\ extract\ movies\ with\ length\ in\ between\ minimum\ and\ maximum\r\n\ specified\ minutes\ specified\ in\ the\ method\ parameter.\r\n\ \r\n\ @\ Konstantin\ Krumin\r\n\ @\ Version\:\ 1.0\ (February\ 20,\ 2020)\r\n 4 | comment1.params=min\ max 5 | comment1.target=MinutesFilter(int,\ int) 6 | comment2.params=id 7 | comment2.target=boolean\ satisfies(java.lang.String) 8 | numComments=3 9 | -------------------------------------------------------------------------------- /Week 4/MinutesFilter.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * MinutesFilter can be used to extract movies with length in between minimum and maximum 4 | * specified minutes specified in the method parameter. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 20, 2020) 8 | */ 9 | 10 | public class MinutesFilter implements Filter { 11 | private int minMin; 12 | private int maxMin; 13 | 14 | public MinutesFilter (int min, int max) { 15 | minMin = min; 16 | maxMin = max; 17 | } 18 | 19 | @Override 20 | public boolean satisfies(String id) { 21 | return MovieDatabase.getMinutes(id) >= minMin && MovieDatabase.getMinutes(id) <= maxMin; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week 4/Movie.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/Movie.class -------------------------------------------------------------------------------- /Week 4/Movie.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Movie 3 | comment1.params=anID\ aTitle\ aYear\ theGenres 4 | comment1.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String) 5 | comment10.params= 6 | comment10.target=int\ getMinutes() 7 | comment11.params= 8 | comment11.target=java.lang.String\ toString() 9 | comment2.params=anID\ aTitle\ aYear\ theGenres\ aDirector\ aCountry\ aPoster\ theMinutes 10 | comment2.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ int) 11 | comment3.params= 12 | comment3.target=java.lang.String\ getID() 13 | comment4.params= 14 | comment4.target=java.lang.String\ getTitle() 15 | comment5.params= 16 | comment5.target=int\ getYear() 17 | comment6.params= 18 | comment6.target=java.lang.String\ getGenres() 19 | comment7.params= 20 | comment7.target=java.lang.String\ getCountry() 21 | comment8.params= 22 | comment8.target=java.lang.String\ getDirector() 23 | comment9.params= 24 | comment9.target=java.lang.String\ getPoster() 25 | numComments=12 26 | -------------------------------------------------------------------------------- /Week 4/Movie.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | 4 | // An immutable passive data object (PDO) to represent item data 5 | public class Movie { 6 | private String id; 7 | private String title; 8 | private int year; 9 | private String genres; 10 | private String director; 11 | private String country; 12 | private String poster; 13 | private int minutes; 14 | 15 | public Movie (String anID, String aTitle, String aYear, String theGenres) { 16 | // just in case data file contains extra whitespace 17 | id = anID.trim(); 18 | title = aTitle.trim(); 19 | year = Integer.parseInt(aYear.trim()); 20 | genres = theGenres; 21 | } 22 | 23 | public Movie (String anID, String aTitle, String aYear, String theGenres, String aDirector, 24 | String aCountry, String aPoster, int theMinutes) { 25 | // just in case data file contains extra whitespace 26 | id = anID.trim(); 27 | title = aTitle.trim(); 28 | year = Integer.parseInt(aYear.trim()); 29 | genres = theGenres; 30 | director = aDirector; 31 | country = aCountry; 32 | poster = aPoster; 33 | minutes = theMinutes; 34 | } 35 | 36 | // Returns ID associated with this item 37 | public String getID () { 38 | return id; 39 | } 40 | 41 | // Returns title of this item 42 | public String getTitle () { 43 | return title; 44 | } 45 | 46 | // Returns year in which this item was published 47 | public int getYear () { 48 | return year; 49 | } 50 | 51 | // Returns genres associated with this item 52 | public String getGenres () { 53 | return genres; 54 | } 55 | 56 | public String getCountry(){ 57 | return country; 58 | } 59 | 60 | public String getDirector(){ 61 | return director; 62 | } 63 | 64 | public String getPoster(){ 65 | return poster; 66 | } 67 | 68 | public int getMinutes(){ 69 | return minutes; 70 | } 71 | 72 | // Returns a string of the item's information 73 | public String toString () { 74 | String result = "Movie [id=" + id + ", title=" + title + ", year=" + year; 75 | result += ", genres= " + genres + "]"; 76 | return result; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Week 4/MovieDatabase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/MovieDatabase.class -------------------------------------------------------------------------------- /Week 4/MovieDatabase.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MovieDatabase 3 | comment1.params=moviefile 4 | comment1.target=void\ initialize(java.lang.String) 5 | comment10.params=id 6 | comment10.target=int\ getMinutes(java.lang.String) 7 | comment11.params=id 8 | comment11.target=java.lang.String\ getCountry(java.lang.String) 9 | comment12.params=id 10 | comment12.target=java.lang.String\ getDirector(java.lang.String) 11 | comment13.params= 12 | comment13.target=int\ size() 13 | comment14.params=f 14 | comment14.target=java.util.ArrayList\ filterBy(Filter) 15 | comment2.params= 16 | comment2.target=void\ initialize() 17 | comment3.params=filename 18 | comment3.target=void\ loadMovies(java.lang.String) 19 | comment4.params=id 20 | comment4.target=boolean\ containsID(java.lang.String) 21 | comment5.params=id 22 | comment5.target=int\ getYear(java.lang.String) 23 | comment6.params=id 24 | comment6.target=java.lang.String\ getGenres(java.lang.String) 25 | comment7.params=id 26 | comment7.target=java.lang.String\ getTitle(java.lang.String) 27 | comment8.params=id 28 | comment8.target=Movie\ getMovie(java.lang.String) 29 | comment9.params=id 30 | comment9.target=java.lang.String\ getPoster(java.lang.String) 31 | numComments=15 32 | -------------------------------------------------------------------------------- /Week 4/MovieDatabase.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * MovieDatabase class sets a HashMap with data from the corresponding csv table and contains 5 | * several methods to add and extract data from this HashMap. 6 | * 7 | */ 8 | 9 | import java.util.*; 10 | import org.apache.commons.csv.*; 11 | import edu.duke.FileResource; 12 | 13 | public class MovieDatabase { 14 | private static HashMap ourMovies; 15 | 16 | public static void initialize(String moviefile) { 17 | if (ourMovies == null) { 18 | ourMovies = new HashMap(); 19 | loadMovies(moviefile); 20 | } 21 | } 22 | 23 | private static void initialize() { 24 | if (ourMovies == null) { 25 | ourMovies = new HashMap(); 26 | loadMovies("ratedmoviesfull"); 27 | } 28 | } 29 | 30 | private static void loadMovies(String filename) { 31 | FirstRatings fr = new FirstRatings(); 32 | ArrayList list = fr.loadMovies(filename); 33 | for (Movie m : list) { 34 | ourMovies.put(m.getID(), m); 35 | } 36 | } 37 | 38 | public static boolean containsID(String id) { 39 | initialize(); 40 | return ourMovies.containsKey(id); 41 | } 42 | 43 | public static int getYear(String id) { 44 | initialize(); 45 | return ourMovies.get(id).getYear(); 46 | } 47 | 48 | public static String getGenres(String id) { 49 | initialize(); 50 | return ourMovies.get(id).getGenres(); 51 | } 52 | 53 | public static String getTitle(String id) { 54 | initialize(); 55 | return ourMovies.get(id).getTitle(); 56 | } 57 | 58 | public static Movie getMovie(String id) { 59 | initialize(); 60 | return ourMovies.get(id); 61 | } 62 | 63 | public static String getPoster(String id) { 64 | initialize(); 65 | return ourMovies.get(id).getPoster(); 66 | } 67 | 68 | public static int getMinutes(String id) { 69 | initialize(); 70 | return ourMovies.get(id).getMinutes(); 71 | } 72 | 73 | public static String getCountry(String id) { 74 | initialize(); 75 | return ourMovies.get(id).getCountry(); 76 | } 77 | 78 | public static String getDirector(String id) { 79 | initialize(); 80 | return ourMovies.get(id).getDirector(); 81 | } 82 | 83 | public static int size() { 84 | return ourMovies.size(); 85 | } 86 | 87 | public static ArrayList filterBy(Filter f) { 88 | initialize(); 89 | ArrayList list = new ArrayList(); 90 | for(String id : ourMovies.keySet()) { 91 | if (f.satisfies(id)) { 92 | list.add(id); 93 | } 94 | } 95 | 96 | return list; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /Week 4/MovieRunnerAverage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/MovieRunnerAverage.class -------------------------------------------------------------------------------- /Week 4/MovieRunnerAverage.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MovieRunnerAverage 3 | comment1.params= 4 | comment1.target=void\ printAverageRatings() 5 | comment2.params= 6 | comment2.target=void\ getAverageRatingOneMovie() 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 4/MovieRunnerAverage.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * MovieRunnerAverage class contains tests for the getAverageRatings method from the 4 | * secondRatings class. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 18, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class MovieRunnerAverage { 13 | public void printAverageRatings () { 14 | SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings"); 15 | 16 | System.out.println("Total number of movies : " + secondRatings.getMovieSize()); 17 | System.out.println("Total number of raters : " + secondRatings.getRaterSize()); 18 | 19 | int MinNumOfRatings = 20; // variable 20 | ArrayList averageRatings = secondRatings.getAverageRatings(MinNumOfRatings); 21 | Collections.sort(averageRatings); 22 | for (Rating rating : averageRatings) { 23 | System.out.println(rating.getValue() + " " + secondRatings.getTitle(rating.getItem())); 24 | } 25 | System.out.println("There are " + averageRatings.size() + " movies with " + 26 | MinNumOfRatings + " or more ratings"); 27 | } 28 | 29 | public void getAverageRatingOneMovie () { 30 | SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings"); 31 | 32 | String title = "Vacation"; // variable 33 | int MinNumOfRatings = 1; // variable 34 | 35 | String movieID = secondRatings.getID(title); 36 | ArrayList averageRatings = secondRatings.getAverageRatings(MinNumOfRatings); 37 | for (Rating rating : averageRatings) { 38 | if (rating.getItem().equals(movieID)) { 39 | System.out.println("For movie \"" + title + "\" the average rating is " 40 | + rating.getValue()); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Week 4/MovieRunnerSimilarRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/MovieRunnerSimilarRatings.class -------------------------------------------------------------------------------- /Week 4/MovieRunnerSimilarRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MovieRunnerSimilarRatings 3 | comment1.params= 4 | comment1.target=void\ printAverageRatings() 5 | comment2.params= 6 | comment2.target=void\ printAverageRatingsByYearAfterAndGenre() 7 | comment3.params= 8 | comment3.target=void\ printSimilarRatings() 9 | comment4.params= 10 | comment4.target=void\ printSimilarRatingsByGenre() 11 | comment5.params= 12 | comment5.target=void\ printSimilarRatingsByDirector() 13 | comment6.params= 14 | comment6.target=void\ printSimilarRatingsByGenreAndMinutes() 15 | comment7.params= 16 | comment7.target=void\ printSimilarRatingsByYearAfterAndMinutes() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 4/MovieRunnerSimilarRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * MovieRunnerSimilarRatings class contains various tests for the methods from FourthRatings. 4 | * 5 | * @ Konstantin Krumin 6 | * @ Version: 1.0 (February 20, 2020) 7 | */ 8 | 9 | import java.util.*; 10 | 11 | public class MovieRunnerSimilarRatings { 12 | public void printAverageRatings () { 13 | FourthRatings fourthRatings = new FourthRatings ("ratings"); 14 | MovieDatabase.initialize("ratedmoviesfull"); 15 | 16 | System.out.println("Read data for " + RaterDatabase.size() + " raters"); 17 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 18 | 19 | int minNumOfRatings = 35; // variable 20 | ArrayList averageRatings = fourthRatings.getAverageRatings(minNumOfRatings); 21 | System.out.println("There are " + averageRatings.size() + " movies with " + 22 | minNumOfRatings + " or more rating(s) :"); 23 | 24 | Collections.sort(averageRatings); 25 | for (Rating rating : averageRatings) { 26 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 27 | } 28 | } 29 | 30 | public void printAverageRatingsByYearAfterAndGenre () { 31 | FourthRatings fourthRatings = new FourthRatings ("ratings"); 32 | MovieDatabase.initialize("ratedmoviesfull"); 33 | 34 | System.out.println("Read data for " + RaterDatabase.size() + " raters"); 35 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 36 | 37 | int year = 1990; // variable 38 | YearAfterFilter yaf = new YearAfterFilter (year); 39 | 40 | String genre = "Drama"; // variable 41 | GenreFilter gf = new GenreFilter (genre); 42 | 43 | AllFilters af = new AllFilters(); 44 | af.addFilter(yaf); 45 | af.addFilter(gf); 46 | 47 | int minNumOfRatings = 8; // variable 48 | ArrayList avgRatings = fourthRatings.getAverageRatingsByFilter(minNumOfRatings, af); 49 | System.out.println("There is(are) " + avgRatings.size() + " movie(s) in genre of \"" 50 | + genre + "\" that was(were) directed after " + year + " with " + minNumOfRatings 51 | + " or more rating(s) :"); 52 | 53 | Collections.sort(avgRatings); 54 | for (Rating rating : avgRatings) { 55 | System.out.println(rating.getValue() + " " + MovieDatabase.getYear(rating.getItem()) 56 | + " " + MovieDatabase.getTitle(rating.getItem())); 57 | System.out.println("Genre : " + MovieDatabase.getGenres(rating.getItem())); 58 | } 59 | } 60 | 61 | public void printSimilarRatings () { 62 | FourthRatings fourthRatings = new FourthRatings ("ratings"); 63 | MovieDatabase.initialize("ratedmoviesfull"); 64 | 65 | System.out.println("Read data for " + RaterDatabase.size() + " raters"); 66 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 67 | 68 | String id = "71"; // variable 69 | int numSimilarRaters = 20; // variable 70 | int minimalRaters = 5; // variable 71 | ArrayList similarRatings = fourthRatings.getSimilarRatings(id, numSimilarRaters, minimalRaters); 72 | System.out.println("There is(are) " + similarRatings.size() + " movie(s) that is(are) " 73 | + "recommended for the rater with ID " + id + " with " + minimalRaters 74 | + " or more rating(s). " + numSimilarRaters + " closest raters were considered."); 75 | 76 | for (Rating rating : similarRatings) { 77 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 78 | } 79 | } 80 | 81 | public void printSimilarRatingsByGenre () { 82 | FourthRatings fourthRatings = new FourthRatings ("ratings"); 83 | MovieDatabase.initialize("ratedmoviesfull"); 84 | 85 | System.out.println("Read data for " + RaterDatabase.size() + " raters"); 86 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 87 | 88 | String genre = "Mystery"; // variable 89 | GenreFilter gf = new GenreFilter(genre); 90 | 91 | String id = "964"; // variable 92 | int numSimilarRaters = 20; // variable 93 | int minimalRaters = 5; // variable 94 | ArrayList similarRatings = fourthRatings.getSimilarRatingsByFilter 95 | (id, numSimilarRaters, minimalRaters, gf); 96 | System.out.println("There is(are) " + similarRatings.size() + " movie(s) that is(are) " 97 | + "recommended for the rater with ID " + id + " and with " + minimalRaters 98 | + " or more rating(s), in \"" + genre + "\" genre. " + numSimilarRaters + " closest raters were considered."); 99 | 100 | for (Rating rating : similarRatings) { 101 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 102 | System.out.println("Genre: " + MovieDatabase.getGenres(rating.getItem())); 103 | } 104 | } 105 | 106 | public void printSimilarRatingsByDirector () { 107 | FourthRatings fourthRatings = new FourthRatings ("ratings"); 108 | MovieDatabase.initialize("ratedmoviesfull"); 109 | 110 | System.out.println("Read data for " + RaterDatabase.size() + " raters"); 111 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 112 | 113 | String directors = "Clint Eastwood,J.J. Abrams,Alfred Hitchcock,Sydney Pollack,David Cronenberg,Oliver Stone,Mike Leigh"; // variable 114 | DirectorsFilter df = new DirectorsFilter(directors); 115 | 116 | String id = "120"; // variable 117 | int numSimilarRaters = 10; // variable 118 | int minimalRaters = 2; // variable 119 | ArrayList similarRatings = fourthRatings.getSimilarRatingsByFilter 120 | (id, numSimilarRaters, minimalRaters, df); 121 | System.out.println("There is(are) " + similarRatings.size() + " movie(s) that is(are) " 122 | + "recommended for the rater with ID " + id + " and with " + minimalRaters 123 | + " or more rating(s), that was(were) directed by either of the following directors: " 124 | + directors + ". " + numSimilarRaters + " closest raters were considered."); 125 | 126 | for (Rating rating : similarRatings) { 127 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 128 | System.out.println("Directed by : " + MovieDatabase.getDirector(rating.getItem())); 129 | } 130 | } 131 | 132 | public void printSimilarRatingsByGenreAndMinutes () { 133 | FourthRatings fourthRatings = new FourthRatings ("ratings"); 134 | MovieDatabase.initialize("ratedmoviesfull"); 135 | 136 | System.out.println("Read data for " + RaterDatabase.size() + " raters"); 137 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 138 | 139 | String genre = "Drama"; // variable 140 | GenreFilter gf = new GenreFilter (genre); 141 | 142 | int minMin = 80; // variable 143 | int maxMin = 160; // variable 144 | MinutesFilter mf = new MinutesFilter (minMin, maxMin); 145 | 146 | AllFilters af = new AllFilters(); 147 | af.addFilter(gf); 148 | af.addFilter(mf); 149 | 150 | String id = "168"; // variable 151 | int numSimilarRaters = 10; // variable 152 | int minimalRaters = 3; // variable 153 | ArrayList similarRatings = fourthRatings.getSimilarRatingsByFilter 154 | (id, numSimilarRaters, minimalRaters, af); 155 | System.out.println("There is(are) " + similarRatings.size() + " movie(s) that is(are) " 156 | + "recommended for the rater with ID " + id + " and with " + minimalRaters 157 | + " or more rating(s), in \"" + genre + "\" genre, that is(are) between " + minMin 158 | + " and " + maxMin + " minutes in length. " + numSimilarRaters + " closest raters were considered."); 159 | 160 | for (Rating rating : similarRatings) { 161 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem()) 162 | + " Time: " + MovieDatabase.getMinutes(rating.getItem())); 163 | System.out.println("Genre: " + MovieDatabase.getGenres(rating.getItem())); 164 | } 165 | } 166 | 167 | public void printSimilarRatingsByYearAfterAndMinutes () { 168 | FourthRatings fourthRatings = new FourthRatings ("ratings"); 169 | MovieDatabase.initialize("ratedmoviesfull"); 170 | 171 | System.out.println("Read data for " + RaterDatabase.size() + " raters"); 172 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 173 | 174 | int year = 1975; // variable 175 | YearAfterFilter yaf = new YearAfterFilter (year); 176 | 177 | int minMin = 70; // variable 178 | int maxMin = 200; // variable 179 | MinutesFilter mf = new MinutesFilter (minMin, maxMin); 180 | 181 | AllFilters af = new AllFilters(); 182 | af.addFilter(yaf); 183 | af.addFilter(mf); 184 | 185 | String id = "314"; // variable 186 | int numSimilarRaters = 10; // variable 187 | int minimalRaters = 5; // variable 188 | ArrayList similarRatings = fourthRatings.getSimilarRatingsByFilter 189 | (id, numSimilarRaters, minimalRaters, af); 190 | System.out.println("There is(are) " + similarRatings.size() + " movie(s) that is(are) " 191 | + "recommended for the rater with ID " + id + " and with " + minimalRaters 192 | + " or more rating(s), that is(are) between " + minMin + " and " + maxMin 193 | + " minutes in length and released after year " + year + ". " + numSimilarRaters 194 | + " closest raters were considered."); 195 | 196 | for (Rating rating : similarRatings) { 197 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem()) 198 | + " Year: " + MovieDatabase.getYear(rating.getItem()) + " Time: " 199 | + MovieDatabase.getMinutes(rating.getItem())); 200 | } 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /Week 4/MovieRunnerWithFilters.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/MovieRunnerWithFilters.class -------------------------------------------------------------------------------- /Week 4/MovieRunnerWithFilters.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=MovieRunnerWithFilters 3 | comment1.params= 4 | comment1.target=void\ printAverageRatings() 5 | comment2.params= 6 | comment2.target=void\ printAverageRatingsByYearAfter() 7 | comment3.params= 8 | comment3.target=void\ printAverageRatingsByGenre() 9 | comment4.params= 10 | comment4.target=void\ printAverageRatingsByMinutes() 11 | comment5.params= 12 | comment5.target=void\ printAverageRatingsByDirectors() 13 | comment6.params= 14 | comment6.target=void\ printAverageRatingsByYearAfterAndGenre() 15 | comment7.params= 16 | comment7.target=void\ printAverageRatingsByDirectorsAndMinutes() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 4/MovieRunnerWithFilters.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * MovieRunnerWithFilters contains tests for ThirdRatings class. 4 | * 5 | * @ Konstantin Krumin 6 | * @ Version: 1.0 (February 19, 2020) 7 | */ 8 | 9 | import java.util.*; 10 | 11 | public class MovieRunnerWithFilters { 12 | public void printAverageRatings () { 13 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 14 | MovieDatabase.initialize("ratedmoviesfull"); 15 | 16 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 17 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 18 | 19 | int minNumOfRatings = 35; // variable 20 | ArrayList averageRatings = thirdRatings.getAverageRatings(minNumOfRatings); 21 | System.out.println("There are " + averageRatings.size() + " movies with " + 22 | minNumOfRatings + " or more rating(s) :"); 23 | 24 | Collections.sort(averageRatings); 25 | for (Rating rating : averageRatings) { 26 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 27 | } 28 | } 29 | 30 | public void printAverageRatingsByYearAfter () { 31 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 32 | MovieDatabase.initialize("ratedmoviesfull"); 33 | 34 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 35 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 36 | 37 | int year = 2000; // variable 38 | YearAfterFilter yaf = new YearAfterFilter (year); 39 | 40 | int minNumOfRatings = 20; // variable 41 | ArrayList averageRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, yaf); 42 | System.out.println("There are " + averageRatings.size() + " movies released after " 43 | + year + " with " + minNumOfRatings + " or more rating(s) : "); 44 | 45 | Collections.sort(averageRatings); 46 | for (Rating rating : averageRatings) { 47 | System.out.println(rating.getValue() + " " + MovieDatabase.getYear(rating.getItem()) 48 | + " " + MovieDatabase.getTitle(rating.getItem())); 49 | } 50 | } 51 | 52 | public void printAverageRatingsByGenre () { 53 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 54 | MovieDatabase.initialize("ratedmoviesfull"); 55 | 56 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 57 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 58 | 59 | String genre = "Comedy"; // variable 60 | GenreFilter gf = new GenreFilter (genre); 61 | 62 | int minNumOfRatings = 20; // variable 63 | ArrayList averageRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, gf); 64 | System.out.println("There are " + averageRatings.size() + " movies in genre of \"" 65 | + genre + "\" with " + minNumOfRatings + " or more rating(s) :"); 66 | 67 | Collections.sort(averageRatings); 68 | for (Rating rating : averageRatings) { 69 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 70 | System.out.println("Genre(s) : " + MovieDatabase.getGenres(rating.getItem())); 71 | } 72 | } 73 | 74 | public void printAverageRatingsByMinutes () { 75 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 76 | MovieDatabase.initialize("ratedmoviesfull"); 77 | 78 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 79 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 80 | 81 | int minMinutes = 105; // variable 82 | int maxMinutes = 135; // variable 83 | MinutesFilter mf = new MinutesFilter (minMinutes, maxMinutes); 84 | 85 | int minNumOfRatings = 5; // variable 86 | ArrayList averageRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, mf); 87 | System.out.println("There are " + averageRatings.size() + " movies that are between " 88 | + minMinutes + " and " + maxMinutes + " length with " + minNumOfRatings + " or more rating(s) :"); 89 | 90 | Collections.sort(averageRatings); 91 | for (Rating rating : averageRatings) { 92 | System.out.println(rating.getValue() + " Time: " + MovieDatabase.getMinutes(rating.getItem()) 93 | + " " + MovieDatabase.getTitle(rating.getItem())); 94 | } 95 | } 96 | 97 | public void printAverageRatingsByDirectors () { 98 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 99 | MovieDatabase.initialize("ratedmoviesfull"); 100 | 101 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 102 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 103 | 104 | String directorsList = "Clint Eastwood,Joel Coen,Martin Scorsese,Roman Polanski,Nora Ephron,Ridley Scott,Sydney Pollack"; // variable 105 | DirectorsFilter df = new DirectorsFilter (directorsList); 106 | 107 | int minNumOfRatings = 4; // variable 108 | ArrayList averageRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, df); 109 | System.out.println("There are " + averageRatings.size() + " movies that were directed " 110 | + "by either of those directors : " + directorsList + ", with " + minNumOfRatings 111 | + " or more rating(s) :"); 112 | 113 | Collections.sort(averageRatings); 114 | for (Rating rating : averageRatings) { 115 | System.out.println(rating.getValue() + " " + MovieDatabase.getTitle(rating.getItem())); 116 | System.out.println("Directed by : " + MovieDatabase.getDirector(rating.getItem())); 117 | } 118 | } 119 | 120 | public void printAverageRatingsByYearAfterAndGenre () { 121 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 122 | MovieDatabase.initialize("ratedmoviesfull"); 123 | 124 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 125 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 126 | 127 | int year = 1990; // variable 128 | YearAfterFilter yaf = new YearAfterFilter (year); 129 | 130 | String genre = "Drama"; // variable 131 | GenreFilter gf = new GenreFilter (genre); 132 | 133 | AllFilters af = new AllFilters(); 134 | af.addFilter(yaf); 135 | af.addFilter(gf); 136 | 137 | int minNumOfRatings = 8; // variable 138 | ArrayList avgRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, af); 139 | System.out.println("There is(are) " + avgRatings.size() + " movie(s) in genre of \"" 140 | + genre + "\" that was(were) directed after " + year + " with " + minNumOfRatings 141 | + " or more rating(s) :"); 142 | 143 | Collections.sort(avgRatings); 144 | for (Rating rating : avgRatings) { 145 | System.out.println(rating.getValue() + " " + MovieDatabase.getYear(rating.getItem()) 146 | + " " + MovieDatabase.getTitle(rating.getItem())); 147 | System.out.println("Genre : " + MovieDatabase.getGenres(rating.getItem())); 148 | } 149 | } 150 | 151 | public void printAverageRatingsByDirectorsAndMinutes () { 152 | ThirdRatings thirdRatings = new ThirdRatings ("ratings"); 153 | MovieDatabase.initialize("ratedmoviesfull"); 154 | 155 | System.out.println("Read data for " + thirdRatings.getRaterSize() + " raters"); 156 | System.out.println("Read data for " + MovieDatabase.size() + " movies"); 157 | 158 | String directorsList = "Clint Eastwood,Joel Coen,Tim Burton,Ron Howard,Nora Ephron,Sydney Pollack"; // variable 159 | DirectorsFilter df = new DirectorsFilter (directorsList); 160 | 161 | int minMinutes = 90; // variable 162 | int maxMinutes = 180; // variable 163 | MinutesFilter mf = new MinutesFilter (minMinutes, maxMinutes); 164 | 165 | AllFilters af = new AllFilters(); 166 | af.addFilter(df); 167 | af.addFilter(mf); 168 | 169 | int minNumOfRatings = 3; // variable 170 | ArrayList avgRatings = thirdRatings.getAverageRatingsByFilter(minNumOfRatings, af); 171 | System.out.println("There is(are) " + avgRatings.size() + " movie(s) that were filmed by" 172 | + " either one of these directors : " + directorsList + "; and between " 173 | + minMinutes + " and " + maxMinutes + " in length, with " + minNumOfRatings 174 | + " or more rating(s) :"); 175 | 176 | Collections.sort(avgRatings); 177 | for (Rating rating : avgRatings) { 178 | System.out.println(rating.getValue() + " Time: " + MovieDatabase.getMinutes(rating.getItem()) 179 | + " " + MovieDatabase.getTitle(rating.getItem())); 180 | System.out.println("Directed by : " + MovieDatabase.getDirector(rating.getItem())); 181 | } 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /Week 4/PlainRater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/PlainRater.class -------------------------------------------------------------------------------- /Week 4/PlainRater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=PlainRater 3 | comment1.params=id 4 | comment1.target=PlainRater(java.lang.String) 5 | comment2.params=item\ rating 6 | comment2.target=void\ addRating(java.lang.String,\ double) 7 | comment3.params=item 8 | comment3.target=boolean\ hasRating(java.lang.String) 9 | comment4.params= 10 | comment4.target=java.lang.String\ getID() 11 | comment5.params=item 12 | comment5.target=double\ getRating(java.lang.String) 13 | comment6.params= 14 | comment6.target=int\ numRatings() 15 | comment7.params= 16 | comment7.target=java.util.ArrayList\ getItemsRated() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 4/PlainRater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * PlainRater class contains various methods that are used to add and extract ratings information. 4 | * 5 | */ 6 | 7 | import java.util.*; 8 | 9 | public class PlainRater implements Rater { 10 | private String myID; 11 | private ArrayList myRatings; 12 | 13 | public PlainRater(String id) { 14 | myID = id; 15 | myRatings = new ArrayList(); 16 | } 17 | 18 | public void addRating(String item, double rating) { 19 | myRatings.add(new Rating(item,rating)); 20 | } 21 | 22 | public boolean hasRating(String item) { 23 | for(int k=0; k < myRatings.size(); k++){ 24 | if (myRatings.get(k).getItem().equals(item)){ 25 | return true; 26 | } 27 | } 28 | 29 | return false; 30 | } 31 | 32 | public String getID() { 33 | return myID; 34 | } 35 | 36 | public double getRating(String item) { 37 | for(int k=0; k < myRatings.size(); k++){ 38 | if (myRatings.get(k).getItem().equals(item)){ 39 | return myRatings.get(k).getValue(); 40 | } 41 | } 42 | 43 | return -1; 44 | } 45 | 46 | public int numRatings() { 47 | return myRatings.size(); 48 | } 49 | 50 | public ArrayList getItemsRated() { 51 | ArrayList list = new ArrayList(); 52 | for(int k=0; k < myRatings.size(); k++){ 53 | list.add(myRatings.get(k).getItem()); 54 | } 55 | 56 | return list; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Week 4/Rater.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/Rater.class -------------------------------------------------------------------------------- /Week 4/Rater.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rater 3 | comment1.params=item\ rating 4 | comment1.target=void\ addRating(java.lang.String,\ double) 5 | comment2.params=item 6 | comment2.target=boolean\ hasRating(java.lang.String) 7 | comment3.params= 8 | comment3.target=java.lang.String\ getID() 9 | comment4.params=item 10 | comment4.target=double\ getRating(java.lang.String) 11 | comment5.params= 12 | comment5.target=int\ numRatings() 13 | comment6.params= 14 | comment6.target=java.util.ArrayList\ getItemsRated() 15 | numComments=7 16 | -------------------------------------------------------------------------------- /Week 4/Rater.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Rater is an interface for PlainRater and EfficientRater classes. 4 | * 5 | * @ Konstantin Krumin 6 | * @ Version: 1.0 (February 19, 2020) 7 | */ 8 | 9 | import java.util.*; 10 | 11 | public interface Rater { 12 | public void addRating(String item, double rating); 13 | 14 | public boolean hasRating(String item); 15 | 16 | public String getID(); 17 | 18 | public double getRating(String item); 19 | 20 | public int numRatings(); 21 | 22 | public ArrayList getItemsRated(); 23 | } 24 | -------------------------------------------------------------------------------- /Week 4/RaterDatabase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/RaterDatabase.class -------------------------------------------------------------------------------- /Week 4/RaterDatabase.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=RaterDatabase 3 | comment1.params= 4 | comment1.target=void\ initialize() 5 | comment2.params=filename 6 | comment2.target=void\ initialize(java.lang.String) 7 | comment3.params=filename 8 | comment3.target=void\ addRatings(java.lang.String) 9 | comment4.params=raterID\ movieID\ rating 10 | comment4.target=void\ addRaterRating(java.lang.String,\ java.lang.String,\ double) 11 | comment5.params=id 12 | comment5.target=Rater\ getRater(java.lang.String) 13 | comment6.params= 14 | comment6.target=java.util.ArrayList\ getRaters() 15 | comment7.params= 16 | comment7.target=int\ size() 17 | numComments=8 18 | -------------------------------------------------------------------------------- /Week 4/RaterDatabase.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * RaterDatabase class sets a HashMap with data from the corresponding csv table and contains 4 | * several methods to add and extract data from this HashMap. 5 | * 6 | */ 7 | 8 | import edu.duke.*; 9 | import java.util.*; 10 | import org.apache.commons.csv.*; 11 | 12 | public class RaterDatabase { 13 | private static HashMap ourRaters; 14 | 15 | private static void initialize() { 16 | // this method is only called from addRatings 17 | if (ourRaters == null) { 18 | ourRaters = new HashMap(); 19 | } 20 | } 21 | 22 | public static void initialize(String filename) { 23 | if (ourRaters == null) { 24 | ourRaters= new HashMap(); 25 | addRatings("data/" + filename + ".csv"); 26 | } 27 | } 28 | 29 | public static void addRatings(String filename) { 30 | initialize(); 31 | FileResource fr = new FileResource(filename); 32 | CSVParser csvp = fr.getCSVParser(); 33 | for(CSVRecord rec : csvp) { 34 | String id = rec.get("rater_id"); 35 | String item = rec.get("movie_id"); 36 | String rating = rec.get("rating"); 37 | addRaterRating(id,item,Double.parseDouble(rating)); 38 | } 39 | } 40 | 41 | public static void addRaterRating(String raterID, String movieID, double rating) { 42 | initialize(); 43 | Rater rater = null; 44 | if (ourRaters.containsKey(raterID)) { 45 | rater = ourRaters.get(raterID); 46 | } else { 47 | rater = new EfficientRater(raterID); 48 | ourRaters.put(raterID,rater); 49 | } 50 | rater.addRating(movieID,rating); 51 | } 52 | 53 | public static Rater getRater(String id) { 54 | initialize(); 55 | 56 | return ourRaters.get(id); 57 | } 58 | 59 | public static ArrayList getRaters() { 60 | initialize(); 61 | ArrayList list = new ArrayList(ourRaters.values()); 62 | 63 | return list; 64 | } 65 | 66 | public static int size() { 67 | return ourRaters.size(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Week 4/Rating.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/Rating.class -------------------------------------------------------------------------------- /Week 4/Rating.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Rating 3 | comment1.params=anItem\ aValue 4 | comment1.target=Rating(java.lang.String,\ double) 5 | comment2.params= 6 | comment2.target=java.lang.String\ getItem() 7 | comment3.params= 8 | comment3.target=double\ getValue() 9 | comment4.params= 10 | comment4.target=java.lang.String\ toString() 11 | comment5.params=other 12 | comment5.target=int\ compareTo(Rating) 13 | numComments=6 14 | -------------------------------------------------------------------------------- /Week 4/Rating.java: -------------------------------------------------------------------------------- 1 | // An immutable passive data object (PDO) to represent the rating data 2 | public class Rating implements Comparable { 3 | private String item; 4 | private double value; 5 | 6 | public Rating (String anItem, double aValue) { 7 | item = anItem; 8 | value = aValue; 9 | } 10 | 11 | // Returns item being rated 12 | public String getItem () { 13 | return item; 14 | } 15 | 16 | // Returns the value of this rating (as a number so it can be used in calculations) 17 | public double getValue () { 18 | return value; 19 | } 20 | 21 | // Returns a string of all the rating information 22 | public String toString () { 23 | return "[" + getItem() + ", " + getValue() + "]"; 24 | } 25 | 26 | public int compareTo(Rating other) { 27 | if (value < other.value) { 28 | return -1; 29 | } 30 | if (value > other.value) { 31 | return 1; 32 | } 33 | return 0; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Week 4/RecommendationRunner.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/RecommendationRunner.class -------------------------------------------------------------------------------- /Week 4/RecommendationRunner.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=RecommendationRunner 3 | comment1.params= 4 | comment1.target=java.util.ArrayList\ getItemsToRate() 5 | comment2.params=webRaterID 6 | comment2.target=void\ printRecommendationsFor(java.lang.String) 7 | numComments=3 8 | -------------------------------------------------------------------------------- /Week 4/RecommendationRunner.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * getItemsToRate method is used to provide users with 20 randomly selected MovieDatabase entries. 4 | * The following information is used to compare user's ratings with ratings provided by other 5 | * users and to give the personalized recommendations. 6 | * 7 | * printRecommendationsFor method is used to print a table with recommended movies based on 8 | * user's ratings. 9 | * 10 | * @ Konstantin Krumin 11 | * @ Version: 1.0 (February 24, 2020) 12 | */ 13 | 14 | import java.util.*; 15 | 16 | public class RecommendationRunner implements Recommender { 17 | 18 | public ArrayList getItemsToRate () { 19 | ArrayList itemsToRate = new ArrayList (); 20 | ArrayList movies = MovieDatabase.filterBy(new TrueFilter()); 21 | 22 | for (int i=0; i < 20; i++) { 23 | Random rand = new Random(); 24 | int random = rand.nextInt(movies.size()); 25 | if (! itemsToRate.contains(movies.get(random))) { 26 | itemsToRate.add(movies.get(random)); 27 | } 28 | } 29 | 30 | return itemsToRate; 31 | } 32 | 33 | public void printRecommendationsFor (String webRaterID) { 34 | FourthRatings fourthRatings = new FourthRatings (); 35 | MovieDatabase.initialize("ratedmoviesfull"); 36 | RaterDatabase.initialize("ratings"); 37 | 38 | System.out.println("

Read data for " + Integer.toString(RaterDatabase.size()) + " raters

"); 39 | System.out.println("

Read data for " + Integer.toString(MovieDatabase.size()) + " movies

"); 40 | 41 | int numSimilarRaters = 50; // variable 42 | int minNumOfRatings = 5; // variable 43 | ArrayList similarRatings = fourthRatings.getSimilarRatings(webRaterID, numSimilarRaters, minNumOfRatings); 44 | 45 | if (similarRatings.size() == 0) { 46 | System.out.println("No matching movies were found"); 47 | } else { 48 | String header = (""); 49 | String body = ""; 50 | for (Rating rating : similarRatings) { 51 | body += " "; 54 | } 55 | System.out.println(header + body + "
Movie Title Rating Value Genres
" + MovieDatabase.getTitle(rating.getItem()) + " " 52 | + Double.toString(rating.getValue()) + " " + MovieDatabase.getGenres(rating.getItem()) 53 | + "
"); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Week 4/Recommender.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/Recommender.class -------------------------------------------------------------------------------- /Week 4/Recommender.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=Recommender 3 | comment0.text=\r\n\ Implement\ this\ interface\ to\ allow\ your\ code\ to\ be\ integrated\ with\ our\r\n\ web\ site.\r\n\ \r\n\ When\ users\ first\ visit\ the\ recommender\ website,\ our\ code\ will\ call\ the\r\n\ method\ getItemsToRate()\ to\ get\ a\ list\ of\ movies\ to\ display\r\n\ on\ the\ web\ page\ for\ users\ to\ rate.\r\n\ \r\n\ When\ a\ user\ submits\ their\ ratings,\ our\ code\ will\ call\ the\ method\ \r\n\ printRecommendationsFor\ to\ get\ your\ recommendations\ based\ on\ the\r\n\ user's\ ratings.\ The\ ID\ given\ to\ this\ method\ is\ for\ a\ new\ Rater\ that\ we\ \r\n\ have\ already\ added\ to\ the\ RaterDatabase\ with\ ratings\ for\ the\ movies\ \r\n\ returned\ by\ the\ first\ method.\ \ Whatever\ is\ printed\ from\ that\ method\ will\ \r\n\ be\ displayed\ on\ the\ web\ page\:\ HTML,\ plain\ text,\ or\ debugging\ information.\r\n\ \r\n 4 | comment1.params= 5 | comment1.target=java.util.ArrayList\ getItemsToRate() 6 | comment1.text=\r\n\ This\ method\ returns\ a\ list\ of\ movie\ IDs\ that\ will\ be\ used\ to\ look\ up\ \r\n\ the\ movies\ in\ the\ MovieDatabase\ and\ present\ them\ to\ users\ to\ rate.\ \r\n\ \ \r\n\ The\ movies\ returned\ in\ the\ list\ will\ be\ displayed\ on\ a\ web\ page,\ so\r\n\ the\ number\ you\ choose\ may\ affect\ how\ long\ the\ page\ takes\ to\ load\ and\r\n\ how\ willing\ users\ are\ to\ rate\ the\ movies.\ \ For\ example,\ 10-20\ should\r\n\ be\ fine,\ 50\ or\ more\ would\ be\ too\ many.\r\n\ \r\n\ There\ are\ no\ restrictions\ on\ the\ method\ you\ use\ to\ generate\ this\ list\r\n\ of\ movies\:\ the\ most\ recent\ movies,\ movies\ from\ a\ specific\ genre,\ \r\n\ randomly\ chosen\ movies,\ or\ simply\ your\ favorite\ movies.\r\n\ \r\n\ The\ ratings\ for\ these\ movies\ will\ make\ the\ profile\ for\ a\ new\ Rater\ \r\n\ that\ will\ be\ used\ to\ compare\ to\ for\ finding\ recommendations.\r\n 7 | comment2.params=webRaterID 8 | comment2.target=void\ printRecommendationsFor(java.lang.String) 9 | comment2.text=\r\n\ This\ method\ returns\ nothing,\ but\ prints\ out\ an\ HTML\ table\ of\ the\ \r\n\ movies\ recommended\ for\ the\ given\ rater.\r\n\ \r\n\ The\ HTML\ printed\ will\ be\ displayed\ on\ a\ web\ page,\ so\ the\ number\ you\r\n\ choose\ to\ display\ may\ affect\ how\ long\ the\ page\ takes\ to\ load.\ \ For\ \r\n\ example,\ you\ may\ want\ to\ limit\ the\ number\ printed\ to\ only\ the\ top\ \r\n\ 20-50\ movies\ recommended\ or\ to\ movies\ not\ rater\ by\ the\ given\ rater.\r\n\ \r\n\ You\ may\ also\ include\ CSS\ styling\ for\ your\ table\ using\ the\ <style>\r\n\ tag\ before\ you\ print\ the\ table.\ \ There\ are\ no\ restrictions\ on\ which\ \r\n\ movies\ you\ print,\ what\ order\ you\ print\ them\ in,\ or\ what\ information\r\n\ you\ include\ about\ each\ movie.\ \r\n\ \r\n\ @param\ webRaterID\ the\ ID\ of\ a\ new\ Rater\ that\ has\ been\ already\ added\ to\ \r\n\ \ \ \ \ \ \ \ the\ RaterDatabase\ with\ ratings\ for\ the\ movies\ returned\ by\ the\ \r\n\ \ \ \ \ \ \ \ method\ getItemsToRate\r\n 10 | numComments=3 11 | -------------------------------------------------------------------------------- /Week 4/Recommender.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | /** 5 | * Implement this interface to allow your code to be integrated with our 6 | * web site. 7 | * 8 | * When users first visit the recommender website, our code will call the 9 | * method getItemsToRate() to get a list of movies to display 10 | * on the web page for users to rate. 11 | * 12 | * When a user submits their ratings, our code will call the method 13 | * printRecommendationsFor to get your recommendations based on the 14 | * user's ratings. The ID given to this method is for a new Rater that we 15 | * have already added to the RaterDatabase with ratings for the movies 16 | * returned by the first method. Whatever is printed from that method will 17 | * be displayed on the web page: HTML, plain text, or debugging information. 18 | * 19 | */ 20 | public interface Recommender { 21 | /** 22 | * This method returns a list of movie IDs that will be used to look up 23 | * the movies in the MovieDatabase and present them to users to rate. 24 | * 25 | * The movies returned in the list will be displayed on a web page, so 26 | * the number you choose may affect how long the page takes to load and 27 | * how willing users are to rate the movies. For example, 10-20 should 28 | * be fine, 50 or more would be too many. 29 | * 30 | * There are no restrictions on the method you use to generate this list 31 | * of movies: the most recent movies, movies from a specific genre, 32 | * randomly chosen movies, or simply your favorite movies. 33 | * 34 | * The ratings for these movies will make the profile for a new Rater 35 | * that will be used to compare to for finding recommendations. 36 | */ 37 | public ArrayList getItemsToRate (); 38 | 39 | /** 40 | * This method returns nothing, but prints out an HTML table of the 41 | * movies recommended for the given rater. 42 | * 43 | * The HTML printed will be displayed on a web page, so the number you 44 | * choose to display may affect how long the page takes to load. For 45 | * example, you may want to limit the number printed to only the top 46 | * 20-50 movies recommended or to movies not rater by the given rater. 47 | * 48 | * You may also include CSS styling for your table using the <style> 49 | * tag before you print the table. There are no restrictions on which 50 | * movies you print, what order you print them in, or what information 51 | * you include about each movie. 52 | * 53 | * @param webRaterID the ID of a new Rater that has been already added to 54 | * the RaterDatabase with ratings for the movies returned by the 55 | * method getItemsToRate 56 | */ 57 | public void printRecommendationsFor (String webRaterID); 58 | } 59 | -------------------------------------------------------------------------------- /Week 4/SecondRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/SecondRatings.class -------------------------------------------------------------------------------- /Week 4/SecondRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=SecondRatings 3 | comment1.params= 4 | comment1.target=SecondRatings() 5 | comment2.params=moviefile\ ratingsfile 6 | comment2.target=SecondRatings(java.lang.String,\ java.lang.String) 7 | comment3.params= 8 | comment3.target=int\ getMovieSize() 9 | comment4.params= 10 | comment4.target=int\ getRaterSize() 11 | comment5.params=id\ minimalRaters 12 | comment5.target=double\ getAverageByID(java.lang.String,\ int) 13 | comment6.params=minimalRaters 14 | comment6.target=java.util.ArrayList\ getAverageRatings(int) 15 | comment7.params=id 16 | comment7.target=java.lang.String\ getTitle(java.lang.String) 17 | comment8.params=title 18 | comment8.target=java.lang.String\ getID(java.lang.String) 19 | numComments=9 20 | -------------------------------------------------------------------------------- /Week 4/SecondRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * SecondRatings class contains various methods to extract information from the corresponding 4 | * ArrayLists. 5 | * 6 | * @ Konstantin Krumin 7 | * @ Version: 1.0 (February 18, 2020) 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class SecondRatings { 13 | private ArrayList myMovies; 14 | private ArrayList myRaters; 15 | 16 | public SecondRatings() { 17 | // default constructor 18 | this("ratedmoviesfull.csv", "ratings.csv"); 19 | } 20 | 21 | public SecondRatings (String moviefile, String ratingsfile) { 22 | FirstRatings firstRatings = new FirstRatings (); 23 | myMovies = firstRatings.loadMovies(moviefile); 24 | myRaters = firstRatings.loadRaters(ratingsfile); 25 | } 26 | 27 | public int getMovieSize () { 28 | return myMovies.size(); 29 | } 30 | 31 | public int getRaterSize () { 32 | return myRaters.size(); 33 | } 34 | 35 | private double getAverageByID (String id, int minimalRaters) { 36 | double sum = 0.0; 37 | int count = 0; 38 | 39 | for (Rater rater : myRaters) { 40 | if (rater.hasRating(id)) { 41 | sum += rater.getRating(id); 42 | count += 1; 43 | } 44 | } 45 | 46 | if (count >= minimalRaters) { 47 | return sum / count; 48 | } else { 49 | return 0.0; 50 | } 51 | } 52 | 53 | public ArrayList getAverageRatings (int minimalRaters) { 54 | ArrayList averageRatings = new ArrayList (); 55 | 56 | for (Movie movie : myMovies) { 57 | String movieID = movie.getID(); 58 | double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 59 | if (average != 0.0) { 60 | Rating rating = new Rating (movieID, average); 61 | averageRatings.add(rating); 62 | } 63 | } 64 | 65 | return averageRatings; 66 | } 67 | 68 | public String getTitle (String id) { 69 | String title = null; 70 | 71 | for (Movie movie : myMovies) { 72 | if (movie.getID().equals(id)) { 73 | title = movie.getTitle(); 74 | } 75 | } 76 | 77 | if (title != null) { 78 | return title; 79 | } else { 80 | return "No movie with such ID was found."; 81 | } 82 | } 83 | 84 | public String getID (String title) { 85 | String movieID = null; 86 | 87 | for (Movie movie : myMovies) { 88 | if (movie.getTitle().equals(title)) { 89 | movieID = movie.getID(); 90 | } 91 | } 92 | 93 | if (movieID != null) { 94 | return movieID; 95 | } else { 96 | return "NO SUCH TITLE."; 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Week 4/ThirdRatings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/ThirdRatings.class -------------------------------------------------------------------------------- /Week 4/ThirdRatings.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=ThirdRatings 3 | comment1.params= 4 | comment1.target=ThirdRatings() 5 | comment2.params=ratingsfile 6 | comment2.target=ThirdRatings(java.lang.String) 7 | comment3.params= 8 | comment3.target=int\ getRaterSize() 9 | comment4.params=id\ minimalRaters 10 | comment4.target=double\ getAverageByID(java.lang.String,\ int) 11 | comment5.params=minimalRaters 12 | comment5.target=java.util.ArrayList\ getAverageRatings(int) 13 | comment6.params=minimalRaters\ filterCriteria 14 | comment6.target=java.util.ArrayList\ getAverageRatingsByFilter(int,\ Filter) 15 | numComments=7 16 | -------------------------------------------------------------------------------- /Week 4/ThirdRatings.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * ThirdRatings class contains 2 methods that can be used to get average ratings 4 | * (with/without filters) as well as additional helper methods. 5 | * 6 | * * getAverageRatings and getAverageRatingsByFilter are used to find movies that have required 7 | * * number of ratings, and in case with getAverageRatingsByFilter can be filtered using a range 8 | * * of filters provided in this program. 9 | * 10 | * @ Konstantin Krumin 11 | * @ Version: 1.0 (February 19, 2020) 12 | */ 13 | 14 | import java.util.*; 15 | 16 | public class ThirdRatings { 17 | private ArrayList myRaters; 18 | 19 | public ThirdRatings () { 20 | // default constructor 21 | this("ratings.csv"); 22 | } 23 | 24 | public ThirdRatings (String ratingsfile) { 25 | FirstRatings firstRatings = new FirstRatings (); 26 | myRaters = firstRatings.loadRaters(ratingsfile); 27 | } 28 | 29 | public int getRaterSize () { 30 | return myRaters.size(); 31 | } 32 | 33 | private double getAverageByID (String id, int minimalRaters) { 34 | double sum = 0.0; 35 | int count = 0; 36 | 37 | for (Rater rater : myRaters) { 38 | if (rater.hasRating(id)) { 39 | sum += rater.getRating(id); 40 | count += 1; 41 | } 42 | } 43 | 44 | if (count >= minimalRaters) { 45 | return sum / count; 46 | } else { 47 | return 0.0; 48 | } 49 | } 50 | 51 | public ArrayList getAverageRatings (int minimalRaters) { 52 | ArrayList movies = MovieDatabase.filterBy(new TrueFilter()); 53 | ArrayList averageRatings = new ArrayList (); 54 | 55 | for (String movieID : movies) { 56 | double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 57 | if (average != 0.0) { 58 | Rating rating = new Rating (movieID, average); 59 | averageRatings.add(rating); 60 | } 61 | } 62 | 63 | return averageRatings; 64 | } 65 | 66 | public ArrayList getAverageRatingsByFilter (int minimalRaters, Filter filterCriteria) { 67 | ArrayList averageRatings = new ArrayList (); 68 | ArrayList filteredMovies = MovieDatabase.filterBy(filterCriteria); 69 | 70 | for (String movieID : filteredMovies) { 71 | double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0; 72 | if (average != 0.0) { 73 | Rating rating = new Rating (movieID, average); 74 | averageRatings.add(rating); 75 | } 76 | } 77 | 78 | return averageRatings; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Week 4/TrueFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/TrueFilter.class -------------------------------------------------------------------------------- /Week 4/TrueFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=TrueFilter 3 | comment1.params=id 4 | comment1.target=boolean\ satisfies(java.lang.String) 5 | numComments=2 6 | -------------------------------------------------------------------------------- /Week 4/TrueFilter.java: -------------------------------------------------------------------------------- 1 | 2 | public class TrueFilter implements Filter { 3 | @Override 4 | public boolean satisfies(String id) { 5 | return true; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Week 4/YearAfterFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konstantinkrumin/Java-Programming-Build-a-Recommendation-System/9d86511fb0d2460e89767022cf88b628c91842e4/Week 4/YearAfterFilter.class -------------------------------------------------------------------------------- /Week 4/YearAfterFilter.ctxt: -------------------------------------------------------------------------------- 1 | #BlueJ class context 2 | comment0.target=YearAfterFilter 3 | comment0.text=\r\n\ \r\n\ YearAfterFilter\ can\ be\ used\ to\ extract\ only\ movies\ released\ after\ a\ particular\ year\ specified\r\n\ in\ the\ method\ parameter.\r\n\ \r\n 4 | comment1.params=year 5 | comment1.target=YearAfterFilter(int) 6 | comment2.params=id 7 | comment2.target=boolean\ satisfies(java.lang.String) 8 | numComments=3 9 | -------------------------------------------------------------------------------- /Week 4/YearAfterFilter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * YearAfterFilter can be used to extract only movies released after a particular year specified 4 | * in the method parameter. 5 | * 6 | */ 7 | 8 | public class YearAfterFilter implements Filter { 9 | private int myYear; 10 | 11 | public YearAfterFilter(int year) { 12 | myYear = year; 13 | } 14 | 15 | @Override 16 | public boolean satisfies(String id) { 17 | return MovieDatabase.getYear(id) >= myYear; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Week 4/data/ratedmovies_short.csv: -------------------------------------------------------------------------------- 1 | id,title,year,country,genre,director,minutes,poster 2 | 0006414,"Behind the Screen",1916,"USA","Short, Comedy, Romance","Charles Chaplin",30,"http://ia.media-imdb.com/images/M/MV5BMTkyNDYyNTczNF5BMl5BanBnXkFtZTgwMDU2MzAwMzE@._V1_SX300.jpg" 3 | 0068646,"The Godfather",1972,"USA","Crime, Drama","Francis Ford Coppola",175,"http://ia.media-imdb.com/images/M/MV5BMjEyMjcyNDI4MF5BMl5BanBnXkFtZTcwMDA5Mzg3OA@@._V1_SX300.jpg" 4 | 0113277,"Heat",1995,"USA","Action, Crime, Drama","Michael Mann",170,"http://ia.media-imdb.com/images/M/MV5BMTM1NDc4ODkxNV5BMl5BanBnXkFtZTcwNTI4ODE3MQ@@._V1_SX300.jpg" 5 | 1798709,"Her",2013,"USA","Drama, Romance, Sci-Fi","Spike Jonze",126,"http://ia.media-imdb.com/images/M/MV5BMjA1Nzk0OTM2OF5BMl5BanBnXkFtZTgwNjU2NjEwMDE@._V1_SX300.jpg" 6 | 0790636,"Dallas Buyers Club",2013,"USA","Biography, Drama","Jean-Marc Vallée",117,"N/A" 7 | -------------------------------------------------------------------------------- /Week 4/data/ratings_short.csv: -------------------------------------------------------------------------------- 1 | rater_id,movie_id,rating,time 2 | 1,0068646,10,1381620027 3 | 1,0113277,10,1379466669 4 | 2,1798709,10,1389948338 5 | 2,0790636,7,1389963947 6 | 2,0068646,9,1382460093 7 | 3,1798709,9,1388641438 8 | 4,0068646,8,1362440416 9 | 4,1798709,6,1398043318 10 | 5,0068646,9,1364834910 11 | 5,1798709,8,1404338202 12 | --------------------------------------------------------------------------------