Skip to content

Commit 76a8030

Browse files
committed
Week4. Additional files
1 parent cdaa3cd commit 76a8030

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Week4/Week4.iml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library" exported="">
11+
<library>
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/../../apache-csv.jar!/" />
14+
</CLASSES>
15+
<JAVADOC />
16+
<SOURCES>
17+
<root url="jar://$MODULE_DIR$/../../apache-csv.jar!/" />
18+
</SOURCES>
19+
</library>
20+
</orderEntry>
21+
<orderEntry type="module-library" exported="">
22+
<library>
23+
<CLASSES>
24+
<root url="jar://$MODULE_DIR$/../../courserajava.jar!/" />
25+
</CLASSES>
26+
<JAVADOC />
27+
<SOURCES>
28+
<root url="jar://$MODULE_DIR$/../../courserajava.jar!/" />
29+
</SOURCES>
30+
</library>
31+
</orderEntry>
32+
</component>
33+
</module>

Week4/src/RaterDatabase.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Write a description of RaterDatabase here.
3+
*
4+
* @author (your name)
5+
* @version (a version number or a date)
6+
*/
7+
import edu.duke.FileResource;
8+
import org.apache.commons.csv.CSVParser;
9+
import org.apache.commons.csv.CSVRecord;
10+
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
14+
public class RaterDatabase {
15+
private static HashMap<String, Rater> ourRaters;
16+
17+
private static void initialize() {
18+
// this method is only called from addRatings
19+
if (ourRaters == null) {
20+
ourRaters = new HashMap<String, Rater>();
21+
}
22+
}
23+
24+
public static void initialize(String filename) {
25+
if (ourRaters == null) {
26+
ourRaters = new HashMap<String, Rater>();
27+
addRatings("data/" + filename);
28+
}
29+
}
30+
31+
public static void addRatings(String filename) {
32+
initialize();
33+
FileResource fr = new FileResource(filename);
34+
CSVParser csvp = fr.getCSVParser();
35+
for (CSVRecord rec : csvp) {
36+
String id = rec.get("rater_id");
37+
String item = rec.get("movie_id");
38+
String rating = rec.get("rating");
39+
addRaterRating(id, item, Double.parseDouble(rating));
40+
}
41+
}
42+
43+
public static void addRaterRating(String raterID, String movieID, double rating) {
44+
initialize();
45+
Rater rater = null;
46+
if (ourRaters.containsKey(raterID)) {
47+
rater = ourRaters.get(raterID);
48+
} else {
49+
rater = new EfficientRater(raterID);
50+
ourRaters.put(raterID, rater);
51+
}
52+
rater.addRating(movieID, rating);
53+
}
54+
55+
public static Rater getRater(String id) {
56+
initialize();
57+
58+
return ourRaters.get(id);
59+
}
60+
61+
public static ArrayList<Rater> getRaters() {
62+
initialize();
63+
ArrayList<Rater> list = new ArrayList<Rater>(ourRaters.values());
64+
65+
return list;
66+
}
67+
68+
public static int size() {
69+
return ourRaters.size();
70+
}
71+
}

Week4/src/Recommender.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.ArrayList;
2+
3+
/**
4+
* Implement this interface to allow your code to be integrated with our web site.
5+
*
6+
* <p>When users first visit the recommender website, our code will call the method <code>
7+
* getItemsToRate()</code> to get a list of movies to display on the web page for users to rate.
8+
*
9+
* <p>When a user submits their ratings, our code will call the method <code>
10+
* printRecommendationsFor</code> to get your recommendations based on the user's ratings. The ID
11+
* given to this method is for a new Rater that we have already added to the RaterDatabase with
12+
* ratings for the movies returned by the first method. Whatever is printed from that method will be
13+
* displayed on the web page: HTML, plain text, or debugging information.
14+
*/
15+
public interface Recommender {
16+
/**
17+
* This method returns a list of movie IDs that will be used to look up the movies in the
18+
* MovieDatabase and present them to users to rate.
19+
*
20+
* <p>The movies returned in the list will be displayed on a web page, so the number you choose
21+
* may affect how long the page takes to load and how willing users are to rate the movies. For
22+
* example, 10-20 should be fine, 50 or more would be too many.
23+
*
24+
* <p>There are no restrictions on the method you use to generate this list of movies: the most
25+
* recent movies, movies from a specific genre, randomly chosen movies, or simply your favorite
26+
* movies.
27+
*
28+
* <p>The ratings for these movies will make the profile for a new Rater that will be used to
29+
* compare to for finding recommendations.
30+
*/
31+
ArrayList<String> getItemsToRate();
32+
33+
/**
34+
* This method returns nothing, but prints out an HTML table of the movies recommended for the
35+
* given rater.
36+
*
37+
* <p>The HTML printed will be displayed on a web page, so the number you choose to display may
38+
* affect how long the page takes to load. For example, you may want to limit the number printed
39+
* to only the top 20-50 movies recommended or to movies not rater by the given rater.
40+
*
41+
* <p>You may also include CSS styling for your table using the &lt;style&gt; tag before you print
42+
* the table. There are no restrictions on which movies you print, what order you print them in,
43+
* or what information you include about each movie.
44+
*
45+
* @param webRaterID the ID of a new Rater that has been already added to the RaterDatabase with
46+
* ratings for the movies returned by the method getItemsToRate
47+
*/
48+
void printRecommendationsFor(String webRaterID);
49+
}

0 commit comments

Comments
 (0)