-
Notifications
You must be signed in to change notification settings - Fork 97
Big Data Problem Tests Passed #71
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
Kazhuru
wants to merge
4
commits into
rilopez:master
Choose a base branch
from
Kazhuru: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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,7 @@ | ||
| .project | ||
| .classpath | ||
| .settings/ | ||
| .vscode/ | ||
| target/ | ||
| fileDataModel.csv | ||
| movies.csv |
163 changes: 163 additions & 0 deletions
163
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,163 @@ | ||
| package nearsoft.academy.bigdata.recommendation; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.BufferedWriter; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileOutputStream; | ||
| import java.io.FileReader; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| 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; | ||
|
|
||
| import com.google.common.collect.BiMap; | ||
| import com.google.common.collect.HashBiMap; | ||
|
|
||
| public class MovieRecommender { | ||
| private String path; | ||
| private String csvPath; | ||
| private String dataFileM; | ||
| private UserBasedRecommender recommender; | ||
| BiMap<String, Long> users; | ||
| BiMap<String, Integer> movies; | ||
| BiMap<Integer, String> invertedMovies; | ||
| private int totalReviews; | ||
|
|
||
| public MovieRecommender(String in_path) throws IOException, TasteException { | ||
| path = in_path; | ||
| users = HashBiMap.create(); | ||
| movies = HashBiMap.create(); | ||
| invertedMovies = HashBiMap.create(); | ||
| totalReviews = 0; | ||
|
|
||
| csvPath = createCSVFile(path); | ||
| dataFileM = parseCSVFile(); | ||
| } | ||
|
|
||
| public List<String> getRecommendationsForUser(String in_user) throws IOException, TasteException { | ||
| Long indexUser = users.get(in_user); | ||
| List<String> outputList = new ArrayList<String>(); | ||
| System.out.println("Generation recommendations, please wait:"); | ||
| DataModel model = new FileDataModel(new File(dataFileM)); | ||
| UserSimilarity similarity = new PearsonCorrelationSimilarity(model); | ||
| UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model); | ||
| recommender = new GenericUserBasedRecommender(model, neighborhood, similarity); | ||
|
|
||
| List<RecommendedItem> recommendations = recommender.recommend(indexUser, 3); | ||
|
|
||
| for (RecommendedItem recommendation : recommendations) { | ||
| Integer i = (int) (long) recommendation.getItemID(); | ||
| String movieID = invertedMovies.get(i); | ||
| System.out.println(movieID); | ||
| outputList.add(movieID); | ||
| } | ||
| return outputList; | ||
| } | ||
|
|
||
| private String createCSVFile(String in_path) throws IOException, TasteException { | ||
|
|
||
| File csv = new File("movies.csv"); | ||
| if (!csv.exists()) | ||
Kazhuru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| csv.createNewFile(); | ||
| FileInputStream fileInput = new FileInputStream(path); | ||
| GZIPInputStream gzis = new GZIPInputStream(fileInput); | ||
| FileOutputStream fileOutput = new FileOutputStream(csv); | ||
|
|
||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = gzis.read(buffer)) > 0) | ||
| fileOutput.write(buffer, 0, length); | ||
|
|
||
| fileInput.close(); | ||
| gzis.close(); | ||
| fileOutput.close(); | ||
| } | ||
| return csv.getAbsolutePath(); | ||
| } | ||
|
|
||
| private String parseCSVFile() throws IOException, TasteException { | ||
|
|
||
| File fileDataModel = new File("fileDataModel.csv"); | ||
| if(fileDataModel.exists()) | ||
| fileDataModel.delete(); | ||
| fileDataModel.createNewFile(); | ||
| try { | ||
| FileWriter fileOutput = new FileWriter(fileDataModel.getAbsolutePath()); | ||
| BufferedWriter fileWriter = new BufferedWriter(fileOutput); | ||
| BufferedReader reader = new BufferedReader(new FileReader(csvPath)); | ||
| String writerString = ""; | ||
| long userCount = 0; | ||
| int moviesCount = 0; | ||
| String line = reader.readLine(); | ||
| while (line != null) { | ||
| String dataLine; | ||
| if(line.startsWith(("product/productId"))) | ||
| { | ||
| dataLine = line.split(" ")[1]; | ||
| if(!movies.containsKey(dataLine)) | ||
| { | ||
| movies.put(dataLine, moviesCount); | ||
| invertedMovies.put(moviesCount,dataLine); | ||
| writerString = moviesCount + ","; | ||
| moviesCount++; | ||
| } | ||
| else | ||
| writerString = (movies.get(dataLine) + ","); | ||
| } | ||
| else if(line.startsWith(("review/userId"))) | ||
| { | ||
| dataLine = line.split(" ")[1]; | ||
| if(!users.containsKey(dataLine)) | ||
| { | ||
| users.put(dataLine, userCount); | ||
| writerString = userCount + "," + writerString; | ||
| userCount++; | ||
| } | ||
| else | ||
| writerString = users.get(dataLine) + "," + writerString; | ||
| } | ||
| else if(line.startsWith(("review/score"))) //score | ||
| { | ||
| totalReviews++; | ||
| String reviewScore = line.split(" ")[1]; | ||
| writerString += reviewScore + "\n"; | ||
| fileWriter.write(writerString); | ||
| } | ||
| line = reader.readLine(); | ||
| } | ||
| fileWriter.close(); | ||
| fileOutput.close(); | ||
| reader.close(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| return fileDataModel.getAbsolutePath(); | ||
| } | ||
|
|
||
| public int getTotalProducts() { | ||
| return movies.size(); | ||
| } | ||
|
|
||
| public int getTotalUsers() { | ||
| return users.size(); | ||
| } | ||
|
|
||
| public int getTotalReviews() { | ||
| return totalReviews; | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.