Skip to content

Solucion Problema BigData #66

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
wants to merge 7 commits 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
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

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

<maven.compiler.source>7</maven.compiler.source>
<maven.compiler.target>7</maven.compiler.target>

</properties>

<dependencies>
Expand All @@ -26,5 +30,15 @@
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

public class MovieRecommender {

private static final String CSV_FILE_PATH = "reviews.csv";
private long totalUsers=0, totalProducts=0, totalReviews=0;
public BiMap<String, Long> hashUsers = HashBiMap.create();
public BiMap<String, Long> hashProducts = HashBiMap.create();
private String filePath="", score="", reviewId="", userId="";
private File newCSV;

public MovieRecommender(String resourceFile) throws IOException, TasteException {
this.filePath = resourceFile;
getTotals(filePath);
}

public long getTotalProducts() {
return totalProducts;
}

public long getTotalReviews() {
return totalReviews;
}

public long getTotalUsers() {
return totalUsers;
}

public void getTotals(String resourceFile) throws IOException, TasteException{

BufferedReader in = new BufferedReader(new InputStreamReader(
new GZIPInputStream(new FileInputStream(resourceFile))));

newCSV = new File(CSV_FILE_PATH);
Files.deleteIfExists(newCSV.toPath());
FileWriter fileWriter = new FileWriter(CSV_FILE_PATH);
PrintWriter printWriter = new PrintWriter(fileWriter);
String actualLine;
while ((actualLine = in.readLine()) != null){
if(actualLine.contains("review/userId")){
userId=actualLine.substring(15);
if(!hashUsers.containsKey(userId)){
totalUsers++;
hashUsers.put(userId, totalUsers);
}
}
if(actualLine.contains("product/productId:")){
totalReviews++;
reviewId=actualLine.substring(19);
if(!hashProducts.containsKey(reviewId)){
hashProducts.put(reviewId, totalProducts);
totalProducts++;
}
}
if (actualLine.contains("review/score:")) {
score = actualLine.substring(14);
printWriter.printf(hashUsers.get(userId) + "," +hashProducts.get(reviewId)+ "," + score+"\n");
}
}
printWriter.close();
}

public List<String> getRecommendationsForUser(String userId) throws IOException, TasteException {
List<String> recommendationsUser = new ArrayList<>();
DataModel model = new FileDataModel(newCSV);
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
List<RecommendedItem> recommendations = recommender.recommend(hashUsers.get(userId), 3);
for (RecommendedItem recommendation : recommendations) {
recommendationsUser.add(hashProducts.inverse().get(recommendation.getItemID()));
}
return recommendationsUser;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
import static org.junit.matchers.JUnitMatchers.hasItem;

public class MovieRecommenderTest {

@Test
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");
assertEquals(7911684, recommender.getTotalReviews());
assertEquals(253059, recommender.getTotalProducts());
assertEquals(889176, recommender.getTotalUsers());
MovieRecommender recommender = new MovieRecommender("/home/fernanda/Documentos/Nearsoft/week3/movies.txt.gz");
assertEquals(7911684, recommender.getTotalReviews());
assertEquals(253059, recommender.getTotalProducts());
assertEquals(889176, recommender.getTotalUsers());

List<String> recommendations = recommender.getRecommendationsForUser("A141HP4LYPWMSR");
assertThat(recommendations, hasItem("B0002O7Y8U"));
assertThat(recommendations, hasItem("B00004CQTF"));
assertThat(recommendations, hasItem("B000063W82"));

}

}