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

MovieRecommender Class finished #65

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
37 changes: 35 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<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>
Expand All @@ -12,8 +11,9 @@

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

<dependencies>
<dependency>
<groupId>org.apache.mahout</groupId>
Expand All @@ -26,5 +26,38 @@
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta2</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.8.0-beta2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*_UT.java</include>
<include>**/*_FT.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
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.net.URL;
import java.util.*;
import java.nio.file.Files;

public class MovieRecommender {

public File resource;
public File data;
private String pathToResources = "src/main/resources/data.txt";

public int totalReviews;
public long totalProducts;
public long totalUsers;

private HashMap<String,Long> userHash = new HashMap<String, Long>();
private HashMap<String, Long> productHash = new HashMap<String, Long>();
private HashMap<Long, String> productHashStringId = new HashMap<Long, String>();


public int getTotalReviews() {
return totalReviews;
}

public void setTotalReviews(int totalReviews) {
this.totalReviews = totalReviews;
}

public long getTotalProducts() {
return totalProducts;
}

public void setTotalProducts(int totalProducts) {
this.totalProducts = totalProducts;
}

public long getTotalUsers() {
return totalUsers;
}

public void setTotalUsers(int totalUsers) {
this.totalUsers = totalUsers;
}


public List getRecommendationsForUser(String user) throws TasteException, IOException {

DataModel model = new FileDataModel(data);

UserSimilarity similarity = new PearsonCorrelationSimilarity(model);

UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);

UserBasedRecommender recommender = new GenericUserBasedRecommender(model,neighborhood,similarity);

List<RecommendedItem> recommendations = recommender.recommend(userHash.get(user), 3);
List<String> recommendationIds = new ArrayList<String>();

for (RecommendedItem recommendation : recommendations) {
long itemId = recommendation.getItemID();
recommendationIds.add(productHashStringId.get(itemId));
}

return recommendationIds;

}

private URL getURL(String filename){
ClassLoader classLoader = getClass().getClassLoader();
URL url = classLoader.getResource(filename);
return url;
}

private File getFileFromResources(String fileName) {
URL resource = getURL(fileName);
if (resource == null) {
throw new IllegalArgumentException("file is not found!");
} else {
return new File(resource.getFile());
}
}

public void setInfo() throws IOException {

if (resource != null) {
FileReader reader = new FileReader(resource);
BufferedReader br = new BufferedReader(reader);

data = new File(pathToResources);

Files.deleteIfExists(data.toPath());
FileWriter fw = new FileWriter(data,true);
BufferedWriter nbr = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(nbr);

String line;

String userId = "";
String productId = "";
String score = "";

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

if (line.startsWith("product/productId")) {
totalReviews++;
productId = line.split(" ")[1];

if (!productHash.containsKey(productId)) {
totalProducts++;
productHash.put(productId, totalProducts);
productHashStringId.put(totalProducts,productId);
}
}
if (line.startsWith("review/userId")) {
userId = line.split(" ")[1];

if (!userHash.containsKey(userId)) {
totalUsers++;
userHash.put(userId, totalUsers);
}
}
if (line.startsWith("review/score:")) {
score = line.split(" ")[1];
pw.println(userHash.get(userId) + "," + productHash.get(productId) + "," + score);
}
}

totalProducts = productHash.size();
totalUsers = userHash.size();
br.close();
pw.close();
}
}

public MovieRecommender(String path) throws IOException, TasteException {
this.resource = getFileFromResources(path);
setInfo();
}

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

System.out.println("REVIEWS " + movie.totalReviews);
System.out.println("PRODUCTS " + movie.totalProducts);
System.out.println("USERS " + movie.totalUsers);


}
}