Skip to content

Adding implementation for movie recommender #77

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 3 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
61 changes: 48 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,51 @@
</properties>

<dependencies>
<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-core</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-core</artifactId>
<version>0.9</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.5.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.5.6</version>
<scope>test</scope>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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.nio.file.Paths;
import java.util.*;
import java.util.zip.GZIPInputStream;

public class MovieRecommender {

private int totalReviews = 0;
private int totalProducts = 0;
private int totalUsers = 0;
private float score = 0.0f;
private BiMap<String, Long> productsHash = HashBiMap.create();
private BiMap<String, Long> usersHash = HashBiMap.create();

public MovieRecommender(String path) throws IOException {
writeFile(path);
}

private void writeFile(String path) throws IOException {
boolean bandera = true;
Long thisProduct = 0l;
Long thisUser = 0l;
Files.deleteIfExists(Paths.get("Result.csv"));
File result = new File("Result.csv");
InputStream fileReader = new GZIPInputStream(new FileInputStream(path));
BufferedReader br = new BufferedReader(new InputStreamReader(fileReader));
FileWriter fileWriter = new FileWriter(result);
BufferedWriter bw = new BufferedWriter(fileWriter);
String line;
String[] sp;
Long identificadorUsuario = 0l;
Long identificadorProducto = 0l;
String key, value;
while((line = br.readLine()) != null) {
if (line.length() >= 0) {
sp = line.split(" ");
key = sp[0];
if (key.equals("product/productId:")) {
value = sp[1];
if (!productsHash.containsKey(value)){
productsHash.put(value,identificadorProducto++);
thisProduct = productsHash.get(value);
this.totalProducts++;
}else{
thisProduct = productsHash.get(value);
}
}else if (key.equals("review/userId:")){
value = sp[1];
if (!usersHash.containsKey(value)){
usersHash.put(value, identificadorUsuario++);
this.totalUsers++;
}
thisUser = usersHash.get(value);
}else if (key.equals("review/score:")){
String score = sp[1];
bw.write(thisUser + "," + thisProduct + "," + score + "\n");
this.totalReviews ++;
}
}
}
br.close();
bw.close();
}

public int getTotalReviews() {
return this.totalReviews;
}

public int getTotalProducts() {
return this.totalProducts;
}

public int getTotalUsers() {
return this.totalUsers;
}

public List<String> getRecommendationsForUser(String id) throws IOException, TasteException {
DataModel model = new FileDataModel(new File("result.csv"));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);

Long userLongId = usersHash.get(id);

List<RecommendedItem> recommendations = recommender.recommend(userLongId, 3);
List<String> recommendationsAsStr = new ArrayList<>();
productsHash.inverse();


for (RecommendedItem recommendation: recommendations) {
recommendationsAsStr.add(productsHash.inverse().get(recommendation.getItemID()));
}
return recommendationsAsStr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
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");
String test = System.getProperty("user.dir")+"/src/test/java/nearsoft/academy/bigdata/recommendation/movies.txt.gz";
MovieRecommender recommender = new MovieRecommender(test);
assertEquals(7911684, recommender.getTotalReviews());
assertEquals(253059, recommender.getTotalProducts());
assertEquals(889176, recommender.getTotalUsers());
Expand All @@ -27,4 +28,4 @@ public void testDataInfo() throws IOException, TasteException {

}

}
}