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

Brandon Rodriguez's solve #44

Open
wants to merge 2 commits into
base: main
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
Binary file added .DS_Store
Binary file not shown.
35 changes: 35 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="module" value="true"/>
<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="lib" path="lib/commons-math3-3.2.jar"/>
<classpathentry kind="lib" path="lib/guava-15.0.jar"/>
<classpathentry kind="lib" path="lib/mahout-core-0.8.jar"/>
<classpathentry kind="lib" path="lib/mahout-integration-0.8.jar"/>
<classpathentry kind="lib" path="lib/mahout-math-0.8.jar"/>
<classpathentry kind="lib" path="lib/slf4j-api-1.7.5.jar"/>
<classpathentry kind="lib" path="lib/slf4j-nop-1.7.5.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target/
data/dataset.csv
data/movies.txt
data/movies.txt.gz
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>academy-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
8 changes: 8 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
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.5
Binary file added lib/commons-math3-3.2.jar
Binary file not shown.
Binary file added lib/guava-15.0.jar
Binary file not shown.
Binary file added lib/mahout-core-0.8.jar
Binary file not shown.
Binary file added lib/mahout-integration-0.8.jar
Binary file not shown.
Binary file added lib/mahout-math-0.8.jar
Binary file not shown.
Binary file added lib/slf4j-api-1.7.5.jar
Binary file not shown.
Binary file added lib/slf4j-nop-1.7.5.jar
Binary file not shown.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,17 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ This repo contains several common big data exercises.

* **MovieRecommender** Uses Amazon movie reviews sample data [stanford.edu/data/web-Movies.html](http://snap.stanford.edu/data/web-Movies.html) for a simple movie recommender




## Setup

1. Install the JDK 7.0
2. [Download & Install Maven](http://maven.apache.org/download.cgi)

-Import .jar files from lib/ folder to the project
3. The data file movies.txt.gz need to be in data/ folder
4. Execute MovieRecommenderTest to compile and generate the dataset.csv from movies.txt.gz
5. Wait

## How to run tests

#from the repository root
mvn test


Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/test/.DS_Store
Binary file not shown.
Binary file added src/test/java/.DS_Store
Binary file not shown.
Binary file added src/test/java/nearsoft/.DS_Store
Binary file not shown.
Binary file added src/test/java/nearsoft/academy/.DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package nearsoft.academy.bigdata.recommendation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

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 {
// Recommender model
private DataModel model;

// Totals
private int totalReviews = 0, totalProducts = 0, totalUsers = 0;

// HashMaps for objects
private Map<String, Integer> products = new HashMap<String, Integer>();
private Map<String, Integer> users = new HashMap<String, Integer>();
private Map<Integer, String> productsInverted = new HashMap<Integer, String>();

public MovieRecommender(String path) throws IOException {
// Get file
FileInputStream rawFile = new FileInputStream(path);
// Decompress file
GZIPInputStream decompressedFile = new GZIPInputStream(rawFile);
// Pass to stream
InputStreamReader streamFile = new InputStreamReader(decompressedFile);

// Read streamed file
BufferedReader txtFile = new BufferedReader(streamFile);
// Create CSV file
FileWriter fileWriter = new FileWriter(System.getProperty("user.dir")+"/data/dataset.csv");


String[] requiredFields = {"product/productId:", "review/userId:", "review/score:"};
String productId = "";
String userId = "";
String score = "";
String row;
while ((row = txtFile.readLine()) != null) {

if(row.contains(requiredFields[0])) {
productId = row.split(" ")[1];

if (this.products.get(productId) == null) {
this.totalProducts++;
this.products.put(productId, this.totalProducts);
this.productsInverted.put(this.totalProducts, productId);
}
} else if(row.contains(requiredFields[1])) {
userId = row.split(" ")[1];

if (this.users.get(userId) == null) {
this.totalUsers++;
this.users.put(userId, this.totalUsers);
}
} else if(row.contains(requiredFields[2])) {
score = row.split(" ")[1];
this.totalReviews++;
}

if ((productId != "") && (userId != "") && (score != "")) {
fileWriter.write(
this.users.get(userId) + "," +
this.products.get(productId) + "," +
score + "\n"
);
productId = "";
score = "";
userId = "";

}

}
fileWriter.close();
txtFile.close();
System.out.println("ALL OK");
}

public List<String> getRecommendationsForUser(String userId) throws IOException, TasteException {
this.model = new FileDataModel(new File("data/dataset.csv"));

UserSimilarity simalirity = new PearsonCorrelationSimilarity(this.model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, simalirity, this.model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(this.model, neighborhood, simalirity);

List<String> recommendations = new ArrayList<String>();

long user = users.get(userId);

List<RecommendedItem> recommendationsItems = recommender.recommend(user, 3);

for (RecommendedItem recommendation : recommendationsItems) {
int productId = (int) recommendation.getItemID();
recommendations.add(productsInverted.get(productId));
}

return recommendations;
}

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

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

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

}
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("data/movies.txt.gz");
assertEquals(7911684, recommender.getTotalReviews());
assertEquals(253059, recommender.getTotalProducts());
assertEquals(889176, recommender.getTotalUsers());
Expand Down