-
Notifications
You must be signed in to change notification settings - Fork 97
Solution for Movie Recommender 2019b #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package nearsoft.academy.bigdata.recommendation; | ||
|
|
||
| import java.io.*; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.*; | ||
| import java.util.zip.GZIPInputStream; | ||
|
|
||
| import org.apache.mahout.cf.taste.common.TasteException; | ||
| import org.apache.mahout.cf.taste.impl.model.file.FileDataModel; | ||
| import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; | ||
| import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender; | ||
| import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; | ||
| import org.apache.mahout.cf.taste.model.DataModel; | ||
| import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood; | ||
| import org.apache.mahout.cf.taste.recommender.RecommendedItem; | ||
| import org.apache.mahout.cf.taste.recommender.UserBasedRecommender; | ||
| import org.apache.mahout.cf.taste.similarity.UserSimilarity; | ||
|
|
||
| public class MovieRecommender { | ||
| private String file; | ||
| private Hashtable<String, Integer> HashProduct = new Hashtable<String, Integer>(); | ||
| private Hashtable<Integer, String> InvertedHashProduct = new Hashtable<Integer, String>(); | ||
| private Hashtable<String, Integer> HashUser = new Hashtable<String, Integer>(); | ||
| private int users =0, products =0, reviews = 0; | ||
|
||
|
|
||
| public MovieRecommender(String file) throws IOException{ | ||
| this.file = file; | ||
| getData(); | ||
| } | ||
|
|
||
| public String getData() throws IOException { | ||
|
||
| int thisProduct =0, thisUser =0; | ||
| Files.deleteIfExists(Paths.get("Result.csv")); | ||
| File result = new File("Result.csv"); | ||
| InputStream fileReader = new GZIPInputStream(new FileInputStream(this.file)); | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(fileReader)); | ||
| FileWriter fileWriter = new FileWriter(result); | ||
| BufferedWriter bw = new BufferedWriter(fileWriter); | ||
| String line; | ||
| String[] sp; | ||
| String key, value; | ||
|
|
||
| while((line = br.readLine()) != null) { | ||
| if (line.length() >= 0) { | ||
| sp = line.split(" "); | ||
|
||
| key = sp[0]; | ||
| if (key.equals("product/productId:")) { | ||
|
||
| value = sp[1]; | ||
| if (!HashProduct.containsKey(value)){ | ||
| HashProduct.put(value,products); | ||
| InvertedHashProduct.put(products,value); | ||
| thisProduct = HashProduct.get(value); | ||
| products++; | ||
| }else{ | ||
| thisProduct = HashProduct.get(value); | ||
| } | ||
| }else if (key.equals("review/userId:")){ | ||
| value = sp[1]; | ||
| if (!HashUser.containsKey(value)){ | ||
| HashUser.put(value, users); | ||
| thisUser = HashUser.get(value); | ||
| users++; | ||
| }else{ | ||
| thisUser = HashUser.get(value); | ||
| } | ||
| }else if (key.equals("review/score:")){ | ||
| String score = sp[1]; | ||
| bw.write(thisUser + "," + thisProduct + "," + score + "\n"); | ||
| reviews ++; | ||
| } | ||
| } | ||
| } | ||
| br.close(); | ||
|
||
| bw.close(); | ||
| return null; | ||
|
||
|
|
||
|
|
||
| } | ||
|
|
||
| public int getTotalReviews() { | ||
| return reviews; | ||
| } | ||
|
|
||
| public int getTotalProducts() { | ||
| return products; | ||
| } | ||
|
|
||
| public int getTotalUsers() { | ||
| return users; | ||
| } | ||
|
|
||
| public List<String> getRecommendationsForUser(String user) throws IOException, TasteException { | ||
| DataModel model = new FileDataModel(new File("Result.csv")); | ||
| UserSimilarity similarity = new PearsonCorrelationSimilarity(model); | ||
| UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model); | ||
| UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity); | ||
|
|
||
| int userValue = HashUser.get(user); | ||
|
|
||
| List RecommendedProducts = new ArrayList<String>(); | ||
|
||
| List<RecommendedItem> recommendations = recommender.recommend(userValue,3); | ||
| for (RecommendedItem recommendation : recommendations) { | ||
| RecommendedProducts.add(InvertedHashProduct.get((int)recommendation.getItemID())); | ||
| } | ||
| return RecommendedProducts; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,10 @@ | |
| public class MovieRecommenderTest { | ||
| @Test | ||
| public void testDataInfo() throws IOException, TasteException { | ||
| //download movies.txt.gz from | ||
| //download movies.txt.gz from | ||
| // http://snap.stanford.edu/data/web-Movies.html | ||
| MovieRecommender recommender = new MovieRecommender("/path/to/movies.txt.gz"); | ||
| MovieRecommender recommender = new MovieRecommender("/Users/alonso/Documents/big-data-exercises/movies.txt.gz"); | ||
|
||
|
|
||
| assertEquals(7911684, recommender.getTotalReviews()); | ||
| assertEquals(253059, recommender.getTotalProducts()); | ||
| assertEquals(889176, recommender.getTotalUsers()); | ||
|
|
@@ -27,4 +28,4 @@ public void testDataInfo() throws IOException, TasteException { | |
|
|
||
| } | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you choose Hashtable vs HashMap?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying different ways of handling bigs amounts of data to see which would give me faster results for the tests, and that's the way I found when working on the solution and thought was the one that would do better, but after doing some research, I understand now that there is nothing in the hashtable that can't be done using hashmap...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, just wanted you to investigate the difference, which is
synchronizationplease research on thatThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually was reading about that. I also read that Hashtables would be used for thread-safe applications, but now we can use
Collections.synchronizedMap()orConcurrentHashinstead.