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

Added MovieRecommender class #75

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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
target/
*.iml
.DS_Store
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>

</dependencies>
</project>
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,53 @@
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.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BookRecommender {
List <String> RecommendedProducts ;
BiMap<String, Long> products ;
BiMap<String, Long> users ;

private String path;
public BookRecommender(String path, BiMap<String, Long> users, BiMap<String, Long> products)
{
this.RecommendedProducts = new ArrayList<String>();
this.path = path;
this.users = users;
this.products = products;
}

public List<String> getRecommendationsForUser( String userID) throws IOException, TasteException {
DataModel model = new FileDataModel(new File(this.path));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
List <String> RecommendedProducts = new ArrayList<String>();
List<RecommendedItem> recommendations = recommender.recommend(this.users.get(userID),3);
for (RecommendedItem recomendation:recommendations)
{
long value = (long) recomendation.getItemID();
this.RecommendedProducts.add(this.products.inverse().get(value) );
}

return this.RecommendedProducts;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package nearsoft.academy.bigdata.recommendation;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import org.apache.mahout.cf.taste.common.TasteException;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

public class MovieRecommender {
private String path;
int information;

BiMap<String, Long> products ;
BiMap<String, Long> users ;
long totalReviews = 0;
PrintWriter csvWriter ;
String product;
String user;

public static void main(String[] args) throws IOException, TasteException {

}
public MovieRecommender(String path) throws IOException, TasteException {
this.path = path;
this.totalReviews = 0;
this.information = 0;
this.users = HashBiMap.create();
this.products = HashBiMap.create();
this.csvWriter= new PrintWriter("dataset.csv");
BufferedReader reader;
InputStream stream = new GZIPInputStream(new FileInputStream(path));
reader = new BufferedReader(new InputStreamReader(stream, "US-ASCII"));
String line = reader.readLine();
long productID = 0;
long userID = 0;
while (line != null ) {
if (line.startsWith("product/productId:")) {
product = line.split(": ")[1];
if (!products.containsKey(product)) {
products.put(product, productID);
productID++;
}

}else if (line.startsWith("review/userId:")) {
user= line.split(": ")[1];
if (!users.containsKey(user)) {
users.put(user, userID);
userID++;
}

}else if (line.startsWith("review/score:")) {
this.totalReviews ++;
csvWriter.println(users.get(user) + "," + products.get(product) + "," + line.split(": ")[1]);

}
line = reader.readLine();
}
csvWriter.close();
}

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

public long getTotalProducts() {
return this.products.size();
}

public long getTotalUsers() {
return this.users.size();
}

public List<String> getRecommendationsForUser(String userID) throws IOException,TasteException {
BookRecommender bookRecommender = new BookRecommender("dataset.csv", this.users, this.products);

List <String> RecommendedProducts = new ArrayList<String>();
RecommendedProducts = bookRecommender.getRecommendationsForUser(userID);

return RecommendedProducts;
}
}
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("/Users/sandraherrera/Downloads/movies.txt.gz");
assertEquals(7911684, recommender.getTotalReviews());
assertEquals(253059, recommender.getTotalProducts());
assertEquals(889176, recommender.getTotalUsers());
Expand All @@ -24,7 +24,6 @@ public void testDataInfo() throws IOException, TasteException {
assertThat(recommendations, hasItem("B0002O7Y8U"));
assertThat(recommendations, hasItem("B00004CQTF"));
assertThat(recommendations, hasItem("B000063W82"));

}

}