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

Solution big-data-exercise lvazquez #89

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
27 changes: 27 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target/
/src/data.csv
/src/movies.txt
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>big-data-exercises</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
3 changes: 3 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eclipse.preferences.version=1
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
15 changes: 15 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.7
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,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-simple</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
</project>
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.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.apache.commons.collections.map.HashedMap;

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;

public class MovieRecommender {
// variables
private int totalReviews, totalProducts, totalUsers;
private HashMap<String, Integer> mapUsers = new HashMap<>();
private HashMap<String, Integer> mapProducts = new HashMap<>();
private DataModel dataModel;
private UserSimilarity similarity;
private UserNeighborhood neighborhood;
private UserBasedRecommender recommender;
private String inputPath, outputPath;

//constructor
public MovieRecommender(String inputPath) throws IOException, TasteException {
this.outputPath = "src/data.csv";

this.inputPath = inputPath;
this.totalReviews = 0;
this.totalProducts = 0;
this.totalUsers = 0;

this.loadData();


this.dataModel = new FileDataModel(new File(this.outputPath));
this.similarity = new PearsonCorrelationSimilarity(this.dataModel);
this.neighborhood = new ThresholdUserNeighborhood(0.1, this.similarity, this.dataModel);
this.recommender = new GenericUserBasedRecommender(this.dataModel, this.neighborhood, this.similarity);

}

public int getTotalReviews() {
return totalReviews;
}

public int getTotalProducts() {
return totalProducts;
}

public int getTotalUsers() {
return totalUsers;
}

public void loadData() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(this.inputPath));
BufferedWriter bw = new BufferedWriter(new FileWriter("src/data.csv"));
String line, productId = "", userId = "", score = "";

//counters for translation
int currentIdUser = 0, currentIdProduct = 0;

line = br.readLine();
while(line != null) {

if(line.contains("product/productId")) {
productId = line.substring(18).trim();
if(!mapProducts.containsKey(productId)) {
currentIdProduct++;
mapProducts.put(productId, currentIdProduct);
// increase total products
this.totalProducts++;
} else {
currentIdProduct = mapProducts.get(productId);
}
// increase total reviews
this.totalReviews++;
}

if(line.contains("review/userId")) {
userId = line.substring(14).trim();
if(!mapUsers.containsKey(userId)) {
currentIdUser++;
mapUsers.put(userId, currentIdUser);
// increase total users
this.totalUsers++;
} else {
currentIdUser = mapUsers.get(userId);
}
}

if(line.contains("review/score")) {
score = String.valueOf( (int) Float.parseFloat(line.substring(13).trim()));
bw.write(currentIdUser+","+currentIdProduct+","+score+"\n");
}

line = br.readLine();
}

br.close();
bw.close();
}

public List<String> getRecommendationsForUser(String userId) throws TasteException {
List<String> list = new ArrayList<String>();

int translatedId = mapUsers.get(userId);
List<RecommendedItem> recommendations = recommender.recommend(translatedId, 3);

for (RecommendedItem recommendation: recommendations) {
list.add(MapUtils.getKey(this.mapProducts, (int) recommendation.getItemID()));
}

return list;
}

/*public static void main(String[] args) throws IOException, TasteException {
MovieRecommender movieRecommender = new MovieRecommender("src/movies.txt");

System.out.println(" my reviews " + movieRecommender.getTotalReviews());
System.out.println(" my products " + movieRecommender.getTotalProducts());
System.out.println(" my users " + movieRecommender.getTotalUsers());

List<String> recommendations = movieRecommender.getRecommendationsForUser("A141HP4LYPWMSR");
for (String recommendation : recommendations) {
System.out.println(recommendation);
}


}*/

}


// method to get Map's key from value in java
class MapUtils {
public static <K, V> K getKey(Map<K, V> map, V value) {
for (K key : map.keySet()) {
if (value.equals(map.get(key))) {
return key;
}
}
return null;
}
}
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("src/movies.txt");
assertEquals(7911684, recommender.getTotalReviews());
assertEquals(253059, recommender.getTotalProducts());
assertEquals(889176, recommender.getTotalUsers());
Expand Down