Skip to content
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea/
*.iml
*.cvc
target/
*.DS_Store
96 changes: 96 additions & 0 deletions MovieRecommender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package nearsoft.academy.bigdata.recommendation;
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.util.*;


public class MovieRecommender {
private final String TEMP_FILE = "/Users/anarobles/Desktop/dataset.csv";
private int usersCount = 0, productsCount = 0, reviewsCount = 0, u=0, n=0;
private HashMap<String, Integer> users = new HashMap();
HashMap<String, Integer> products = new HashMap();
HashMap<Integer, String> inverseHash = new HashMap();
//private Set<String> users = new HashSet<>(); Set<String> products = new HashSet<>();

public MovieRecommender(String sourcePath) throws IOException {


String userId = "", productId = "", score = "";
String line;

BufferedReader bufferedReader = new BufferedReader(new FileReader(sourcePath));
File outputFileStream = new File(TEMP_FILE);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(outputFileStream));
System.out.println("Buffers inicializados");

while ((line = bufferedReader.readLine()) != null) {

if (line.contains("product/productId")) {
productId = line.substring(19, 29);
if (!products.containsKey(productId)) {
productsCount++;
products.put(productId, productsCount);
inverseHash.put(productsCount, productId);
n = productsCount;
} else{
n = products.get(productId);
}
}

if (line.contains("review/userId")) {
userId = line.substring(15);
if (!users.containsKey(userId)) {
usersCount++;
users.put(userId, usersCount);
u=usersCount;
}else{
u=users.get(userId);
}

}
if (line.contains("review/score")) {
score = line.substring(14);
reviewsCount++;
bufferedWriter.write(u + "," + n + "," + score + "\n");
}
}
bufferedWriter.close();
bufferedReader.close();
System.out.println("Archivo creado");

}

List<String> getRecommendationsForUser(String UserId) throws IOException, TasteException {
DataModel model = new FileDataModel(new File(TEMP_FILE));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
//System.out.println(Integer.parseInt(userId));
List<RecommendedItem> recommendations = recommender.recommend(users.get(UserId),3);
List<String> recommendationsForUser = new ArrayList<String>();
for (RecommendedItem recommendation : recommendations) {
recommendationsForUser.add(inverseHash.get((int)recommendation.getItemID()));
}
return recommendationsForUser;
}

public int getTotalUsers(){
return usersCount;
}
public int getTotalProducts(){
return productsCount;
}
public int getTotalReviews(){
return reviewsCount;
}

}
13 changes: 9 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>nearsoft.academy</groupId>
<artifactId>big-data</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>big-data</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>

<dependencies>
Expand All @@ -20,11 +19,17 @@
<artifactId>mahout-core</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-mr</artifactId>
<version>0.13.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
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/anarobles/Downloads/movies.txt");
assertEquals(7911684, recommender.getTotalReviews());
assertEquals(253059, recommender.getTotalProducts());
assertEquals(889176, recommender.getTotalUsers());
Expand All @@ -27,4 +27,4 @@ public void testDataInfo() throws IOException, TasteException {

}

}
}