Skip to content
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

All test cases pass #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,32 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<dependencies>
<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-core</artifactId>
<version>0.9</version>
<artifactId>mahout-mr</artifactId>
<version>0.10.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>




Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package nearsoft.academy.bigdata.recommendation;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
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.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

import java.io.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.zip.GZIPInputStream;


public class MovieRecommender {

private static final String OUTPUT_FILEPATH = "movies.csv";
private static final String PRODUCT_KEY = "product/productId: ";
private static final String USER_KEY = "review/userId: ";
private static final String SCORE_KEY = "review/score: ";
private int totalReviews;
private HashMap<String, Integer> HMusers;
private HashBiMap<String, Integer> HMproducts;
private Recommender recommender;




public MovieRecommender (String filePath) throws IOException, TasteException {
this.HMusers = new HashMap<String, Integer>();
this.HMproducts = HashBiMap.create();
File csvFile = generateCSV(filePath);
createRecommender(csvFile);
}

private File generateCSV(String filePath) throws IOException {

BufferedReader reader = getGzipReader(new File(filePath));
FileWriter writer = new FileWriter(OUTPUT_FILEPATH);
String currentLine;
String csvLine = "";
int currentProduct = 0;

while((currentLine = reader.readLine()) != null) {
if (currentLine.startsWith(PRODUCT_KEY)) {
String productId = currentLine.substring(19);
if (!this.HMproducts.containsKey(productId)) {
this.HMproducts.put(productId, this.HMproducts.size());
}
currentProduct = this.HMproducts.get(productId);
}

else if (currentLine.startsWith(USER_KEY)) {
String userId = currentLine.substring(15);
if (!this.HMusers.containsKey(userId)){
this.HMusers.put(userId, HMusers.size());
}
this.totalReviews++;
csvLine = this.HMusers.get(userId) + "," + currentProduct + ",";
}

else if (currentLine.startsWith(SCORE_KEY)) {
double score = Double.parseDouble(currentLine.substring(14));
csvLine += score + "\n";
writer.write(csvLine);
writer.flush();
}



}

reader.close();
writer.close();



return new File(OUTPUT_FILEPATH);
}



public void createRecommender(File csvFile) throws TasteException, IOException {
DataModel model = new FileDataModel(csvFile);
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
this.recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
}

public List<String> getRecommendationsForUser (String userId) throws TasteException {
int userIdInt = this.HMusers.get(userId);
List recommendations = this.recommender.recommend(userIdInt, 3);
return getRecommendationsIds(recommendations);
}

private List<String> getRecommendationsIds(List<RecommendedItem> recommendations){
BiMap<Integer, String> invertedProducts = this.HMproducts.inverse();
ArrayList<String> productsIds = new ArrayList<String>();
for (RecommendedItem recommendation : recommendations){
int recommendationId = (int) recommendation.getItemID();
productsIds.add(invertedProducts.get(recommendationId));
}
return productsIds;
}




public int getTotalReviews(){
return totalReviews;
}

public int getTotalProducts(){
return HMproducts.size();
}

public int getTotalUsers(){
return HMusers.size();
}



private BufferedReader getGzipReader(File filePath) throws IOException {
InputStream fileStream = new FileInputStream(filePath);
InputStream gzipStream = new GZIPInputStream(fileStream);
Reader decoder = new InputStreamReader(gzipStream, "UTF-8");

return new BufferedReader(decoder);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MovieRecommenderTest {
public void testDataInfo() throws IOException, TasteException {
//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/hzamorano/big-data-exercises/src/test/java/nearsoft/academy/bigdata/recommendation/movies.txt.gz");
assertEquals(7911684, recommender.getTotalReviews());
assertEquals(253059, recommender.getTotalProducts());
assertEquals(889176, recommender.getTotalUsers());
Expand Down