-
Notifications
You must be signed in to change notification settings - Fork 97
Implemented MovieRecommender class, All Junit tests passed #73
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
Open
AlfredoGJ
wants to merge
6
commits into
rilopez:master
Choose a base branch
from
AlfredoGJ:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
80e52b3
1st solution approach
AlfredoGJ 6ee2d23
All Tests Passed
AlfredoGJ c95e935
Comments out in MovieRecommender.java
AlfredoGJ 13cc3c8
Refactorization: addPreferenceToUser method is added to handle the ne…
AlfredoGJ d329229
Comments out in addPreference function
AlfredoGJ daf7d1f
Defined missing acces modifiers on varoius functions and moved the d…
AlfredoGJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
src/test/java/nearsoft/academy/bigdata/recommendation/MovieRecommender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
|
||
package nearsoft.academy.bigdata.recommendation; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import com.google.common.collect.BiMap; | ||
import com.google.common.collect.HashBiMap; | ||
|
||
import org.apache.mahout.cf.taste.impl.model.GenericPreference; | ||
import org.apache.mahout.cf.taste.common.TasteException; | ||
import org.apache.mahout.cf.taste.impl.common.*; | ||
import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray; | ||
import org.apache.mahout.cf.taste.impl.model.GenericDataModel; | ||
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; | ||
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender; | ||
import org.apache.mahout.cf.taste.model.PreferenceArray; | ||
import org.apache.mahout.cf.taste.similarity.UserSimilarity; | ||
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; | ||
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood; | ||
import org.apache.mahout.cf.taste.recommender.RecommendedItem; | ||
|
||
public class MovieRecommender { | ||
|
||
private FastByIDMap<PreferenceArray> usersPreferences; | ||
private HashMap<String, Long> userIDs; | ||
private BiMap<String, Long> productIDsBi; | ||
|
||
private long userCount; | ||
private long productCount; | ||
private long reviewCount; | ||
private GenericDataModel model; | ||
private GenericUserBasedRecommender recommender; | ||
|
||
public MovieRecommender(String filePath) throws TasteException { | ||
|
||
productIDsBi = HashBiMap.create(); | ||
userIDs = new HashMap<String, Long>(); | ||
usersPreferences = new FastByIDMap<PreferenceArray>(); | ||
userCount = 0; | ||
productCount = 0; | ||
reviewCount = 0; | ||
|
||
getPreference(filePath); | ||
buildRecomender(); | ||
|
||
} | ||
|
||
GenericPreference getPreference(String filePath) { | ||
String productID = ""; | ||
String userID = ""; | ||
Float score = 0.0f; | ||
|
||
try { | ||
BufferedReader reader = new BufferedReader(new FileReader(filePath)); | ||
String line = reader.readLine(); | ||
while (line != null) { | ||
|
||
if (line.startsWith("product/productId:")) | ||
productID = line.split(" ")[1]; | ||
if (line.startsWith("review/userId:")) | ||
userID = line.split(" ")[1]; | ||
if (line.startsWith("review/score:")) { | ||
|
||
score = Float.parseFloat(line.split(" ")[1]); | ||
addPreference(userID, productID, score); | ||
|
||
} | ||
line = reader.readLine(); | ||
} | ||
reader.close(); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
private void addPreference(String userID, String productID, float score) { | ||
|
||
reviewCount++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part doesn't have any relationship with addPreference method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, Hayde, I split the function |
||
if (!userIDs.containsKey(userID)) | ||
userIDs.put(userID, ++userCount); | ||
|
||
if (!productIDsBi.containsKey(productID)) | ||
productIDsBi.put(productID, ++productCount); | ||
|
||
Long longUserID = userIDs.get(userID); | ||
Long longProductID = productIDsBi.get(productID); | ||
|
||
addPreferenceToUser((GenericUserPreferenceArray) usersPreferences.get(longUserID),new GenericPreference(longUserID, longProductID, score)); | ||
|
||
} | ||
private void addPreferenceToUser(GenericUserPreferenceArray existentPreferences, GenericPreference newPreference){ | ||
|
||
ArrayList preferencesList = new ArrayList(); | ||
|
||
if (existentPreferences != null) { | ||
for (Integer i = 0; i < existentPreferences.length(); i++) | ||
preferencesList.add(existentPreferences.get(i)); | ||
} | ||
|
||
preferencesList.add(newPreference); | ||
|
||
usersPreferences.put(newPreference.getUserID(), new GenericUserPreferenceArray(preferencesList)); | ||
|
||
} | ||
|
||
|
||
private void buildRecomender() throws TasteException { | ||
model = new GenericDataModel(usersPreferences); | ||
|
||
UserSimilarity similarity = new PearsonCorrelationSimilarity(model); | ||
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model); | ||
recommender = new GenericUserBasedRecommender(model, neighborhood, similarity); | ||
} | ||
|
||
public int getTotalReviews() { | ||
|
||
return (int) reviewCount; | ||
} | ||
|
||
public int getTotalProducts() { | ||
return model.getNumItems(); | ||
} | ||
|
||
public int getTotalUsers() { | ||
return model.getNumUsers(); | ||
} | ||
|
||
public List<String> getRecommendationsForUser(String user) throws TasteException { | ||
|
||
List<String> result = new ArrayList<String>(); | ||
long longUserID = userIDs.get(user); | ||
List<RecommendedItem>recomendations = recommender .recommend(longUserID,3); | ||
|
||
for(RecommendedItem item: recomendations ) | ||
{ | ||
result.add((String)productIDsBi.inverse().get(item.getItemID())); | ||
} | ||
return result; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.